Last active
August 29, 2020 21:47
-
-
Save tonymadbrain/3ee976bff832c42400fd05b6bc9fa635 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const AWS = require('aws-sdk') | |
AWS.config.region = 'us-east-1' | |
const ecrRegistryId = process.env.ECR_REGISTRY_ID | |
const ecrRegistryName = process.env.ECR_REGISTRY_NAME | |
const ecsName = process.env.ECS_NAME | |
const ecs = new AWS.ECS() | |
const ecr = new AWS.ECR() | |
exports.handler = async (event) => { | |
const newVersion = event.version | |
if (newVersion == '') { | |
throw Error ('no version specified') | |
} | |
const image = await ecr.describeImages({ | |
registryId: ecrRegistryId, | |
repositoryName: ecrRegistryName, | |
imageIds: [ | |
{ imageTag: newVersion } | |
] | |
}).promise() | |
if (image.imageDetails[0].imageSizeInBytes == 0) { | |
throw Error ('no image in registry') | |
} | |
const cluster = await ecs.describeClusters({ | |
clusters: [ ecsName ] | |
}).promise() | |
if (cluster.clusters.length == 1) { | |
if (cluster.clusters[0].status !== 'ACTIVE') { | |
throw Error ('no ACTIVE cluster found') | |
} | |
} else { | |
throw Error ('cluster not found') | |
} | |
const service = await ecs.describeServices({ | |
cluster: ecsName, | |
services: [ ecsName ] | |
}).promise() | |
if (service.services.length == 1) { | |
if (service.services[0].status !== 'ACTIVE') { | |
throw Error ('no ACTIVE service found') | |
} | |
} else { | |
throw Error ('service not found') | |
} | |
const taskDefinitions = await ecs.listTaskDefinitions({ | |
familyPrefix: ecsName, | |
sort: 'DESC', | |
status: 'active' | |
}).promise() | |
const lastTaskDefinition = await ecs.describeTaskDefinition({ | |
taskDefinition: taskDefinitions['taskDefinitionArns'][0], | |
include: ['TAGS'] | |
}).promise() | |
const newTaskDefintion = lastTaskDefinition.taskDefinition | |
const newTaskDefintionTags = lastTaskDefinition.tags | |
const newTaskDefintionContainerImage = lastTaskDefinition.taskDefinition.containerDefinitions[0].image.split(":").splice(0,1).concat([newVersion]).join(":") | |
newTaskDefintion.containerDefinitions[0].image = newTaskDefintionContainerImage | |
const extra_keys = [ | |
'taskDefinitionArn', | |
'revision', | |
'status', | |
'requiresAttributes', | |
'compatibilities' | |
] | |
extra_keys.forEach(key => { | |
delete newTaskDefintion[key] | |
}) | |
for (const tag of newTaskDefintionTags) { | |
if (tag['key'] == 'Version') { | |
newTaskDefintion.tags.push({key: 'Version', value: newVersion}) | |
} else { | |
newTaskDefintion.tags.push(tag) | |
} | |
} | |
const registeredTaskDefinition = await ecs.registerTaskDefinition(newTaskDefintion).promise() | |
const serviceUpdate = await ecs.updateService({ | |
cluster: ecsName, | |
service: ecsName, | |
taskDefinition: registeredTaskDefinition.taskDefinition.taskDefinitionArn | |
}).promise() | |
console.log(serviceUpdate) | |
return { | |
statusCode: 200, | |
body: { | |
status: 'success', | |
version: newVersion | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment