Skip to content

Commit

Permalink
Allow multiple services to be specified for each image
Browse files Browse the repository at this point in the history
  • Loading branch information
bbliem committed Nov 30, 2022
1 parent 8deca87 commit ed9792d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
14 changes: 7 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"staging": {
"docker.kausal.tech/watch": {
"service": "watch_app"
"services": ["watch_app", "watch_celery-beat", "watch_celery-worker"]
},
"docker.kausal.tech/watch-postgres": {
"service": "watch_db"
"services": ["watch_db"]
},
"docker.kausal.tech/watch-elasticsearch": {
"service": "watch_elasticsearch"
"services": ["watch_elasticsearch"]
},
"docker.kausal.tech/watch-ui": {
"service": "watch-ui_app"
"services": ["watch-ui_app"]
},
"docker.kausal.tech/paths": {
"service": "paths_app"
"services": ["paths_app"]
},
"docker.kausal.tech/paths-postgres": {
"service": "paths_db"
"services": ["paths_db"]
},
"docker.kausal.tech/paths-ui": {
"service": "paths-ui_app"
"services": ["paths-ui_app"]
}
}
}
46 changes: 24 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { IncomingWebhook } = require('@slack/webhook')

const app = express()
const environment = process.env.ENVIRONMENT || 'production'
const services = require(`./config.json`)[environment]
const repositories = require(`./config.json`)[environment]

const dockerCommand = process.env.DOCKER || '/usr/bin/docker'
const token = process.env.TOKEN_FILE ? fs.readFileSync(process.env.TOKEN_FILE, 'utf-8').trim() : process.env.TOKEN || ''
Expand All @@ -35,13 +35,13 @@ function tagIsRecognized(tag) {

function updateImage(repository, tag) {
const image = `${repository}:${tag}`
if (!services[repository] || !tagIsRecognized(tag)) {
if (!repositories[repository] || !tagIsRecognized(tag)) {
console.log(`Received update for "${image}" but not configured to handle updates for this image.`)
return
}
console.log(`Updating image "${image}"`)

const service = services[repository].service
const services = repositories[repository].services

// Make sure we are logged in to be able to pull the image
child_process.exec(`${dockerCommand} login -u "${username}" -p "${password}" ${registry}`,
Expand All @@ -51,30 +51,32 @@ function updateImage(repository, tag) {
return
}

// Deploy the image and force a restart of the associated service
console.log(`Deploying ${image} to ${service}...`)
child_process.exec(`${dockerCommand} service update ${service} --force --with-registry-auth --image=${image}`,
(error, stdout, stderr) => {
if (error) {
const message = `Failed to deploy ${image} to ${service}!`
console.error(message)
console.error(error)
// Deploy the image and force a restart of the associated services
for (const service of services) {
console.log(`Deploying ${image} to ${service}...`)
child_process.exec(`${dockerCommand} service update ${service} --force --with-registry-auth --image=${image}`,
(error, stdout, stderr) => {
if (error) {
const message = `Failed to deploy ${image} to ${service}!`
console.error(message)
console.error(error)
if (slackWebhook) {
slackWebhook.send({
text: message,
})
}
return
}

const message = `Deployed ${image} to ${service} successfully and restarted the service.`
console.log(message)
if (slackWebhook) {
slackWebhook.send({
text: message,
})
}
return
}

const message = `Deployed ${image} to ${service} successfully and restarted the service.`
console.log(message)
if (slackWebhook) {
slackWebhook.send({
text: message,
})
}
})
})
}
})
}

Expand Down

0 comments on commit ed9792d

Please sign in to comment.