ext {
clusterName = 'clusterName'
serviceName = 'serviceName'
taskName = 'taskName'
stackName = 'EC2ContainerService-now-web'
accountId = '8707500123456'
region = 'ap-northeast-1'
tagName = '870750012345.dkr.ecr.ap-northeast-1.amazonaws.com/web:latest'
instanceMinCnt = 2
instanceMaxCnt = 4
getInstanceCnt = 0
}
task createImageNpushToECR() {
group = 'docker'
dependsOn war
doFirst {
exec { commandLine 'cmd', '/c', 'aws ecr get-login --no-include-email --profile profileName > temp.cmd' }
exec { commandLine 'cmd', '/c', 'call temp.cmd' }
exec { commandLine 'cmd', '/c', 'del temp.cmd' }
}
doLast {
exec { commandLine 'cmd', '/c', 'docker build -t '+tags+' .' }
exec { commandLine 'docker', 'push', tags }
}
}
task ECSDeploy {
group = 'docker'
doLast {
exec { commandLine 'cmd', '/c', 'aws ecs update-service --force-new-deployment --profile profileName --cluster ' + clusterName + ' --service ' + serviceName + ' --task-definition ' + taskName }
}
}
task ECSInstanceCnt {
group = 'docker'
doLast {
new ByteArrayOutputStream().withStream { cnt ->
exec {
commandLine 'cmd', '/c', 'aws ecs list-container-instances --cluster ' + clusterName + ' | findstr /R /C:"[' + accountId + ']" | find /c /v ""'
standardOutput = cnt
}
getInstanceCnt = cnt.toString().toInteger()
}
println '# Current Web Instance Cnt : ' + getInstanceCnt
}
}
task ECSInstanceUp {
group = 'docker'
dependsOn ECSInstanceCnt
doLast {
if (instanceMaxCnt > getInstanceCnt) {
exec { commandLine 'cmd', '/c', 'aws ecs update-service --cluster ' + clusterName + ' --service ' + serviceName + ' --deployment-configuration minimumHealthyPercent=50,maximumPercent=200' }
exec { commandLine 'cmd', '/c', 'ecs-cli configure --cluster ' + clusterName + ' --default-launch-type EC2 --region ' + region + ' --cfn-stack-name ' + stackName + ' --config-name ' + clusterName }
exec { commandLine 'cmd', '/c', 'ecs-cli scale --capability-iam --size ' + instanceMaxCnt + ' --cluster ' + clusterName + ' --aws-profile profileName' }
}
}
}
task ECSInstanceDown {
group = 'docker'
dependsOn ECSInstanceCnt
doLast {
if (instanceMinCnt < getInstanceCnt) {
exec { commandLine 'cmd', '/c', 'aws ecs update-service --cluster ' + clusterName + ' --service ' + serviceName + ' --deployment-configuration minimumHealthyPercent=100,maximumPercent=200' }
exec { commandLine 'cmd', '/c', 'ecs-cli configure --cluster ' + clusterName + ' --default-launch-type EC2 --region ' + region + ' --cfn-stack-name ' + stackName + ' --config-name ' + clusterName }
exec { commandLine 'cmd', '/c', 'ecs-cli scale --capability-iam --size ' + instanceMinCnt + ' --cluster ' + clusterName + ' --aws-profile profileName' }
}
}
}