1
votes

I am using jenkins for continuous deployment from gitlab into aws ecs ec2 container instance. I am using jenkins file for this purpose. For registering the task definition on each push I have placed the task definition json file in an aws folder in the gitlab. Is it possible to put the task definition json file in the jenkins so that we can need to keep only the jenkinsfile in the gitlab?

There is a workspace folder in jenkins /var/lib/jenkins/workspace/jobname which is created after first build. Can we place the task definition in that place?

My Jenkinsfile is pasted below

stage 'Checkout' 
 git '[email protected]/repo.git'


 stage ("Docker build") {
sh "docker build --no-cache -t xxxx:${BUILD_NUMBER}" ."
}

stage("Docker push") {
docker.withRegistry('https://xxxxxxxxxxxx.dkr.ecr.ap-southeast-1.amazonaws.com', 'ecr:regopm:ecr-credentials') {
    docker.image("xxxxx:${BUILD_NUMBER}").push(remoteImageTag) 
    }
}

stage ("Deploy") {
sh "sed -e 's;BUILD_TAG;${BUILD_NUMBER};g' aws/task-definition.json >            aws/task-definition-${remoteImageTag}.json"


sh  "                                                                     \
      aws ecs register-task-definition  --family ${taskFamily}                \
                                        --cli-input-json ${taskDefile}        \
    "


  def taskRevision = sh (
      returnStdout: true,
      script: " aws ecs describe-task-definition  --task-definition  ${taskFamily} | egrep 'revision' | tr ',' ' '| awk '{print \$2}' "
 ).trim()



 sh  "                                                                     \
      aws ecs update-service  --cluster ${clusterName}                        \
                              --service ${serviceName}                        \
                              --task-definition ${taskFamily}:${taskRevision} \
                              --desired-count 1                               \
    "
 }
1
Use a stack of cloudformation to raise your task in the cluster, then every time you execute it you will update / redeploy the task in ECS. You would use something like aws cloudformation create-stack or update-stack instead of aws ecs update-serviceMiguel Conde

1 Answers

0
votes

use esc-cli as an alternative incase if you do not want to use task definition , install esc-cli on jenkins node and run it, but that still need docker-compose on the git.