Flow Chart
1. Create Codedeploy
- Follow the link to create lambda function which is trigger by S3 notification event and then create code deployment.
- Gitlab pipeline job will push deploy.zip to S3 so that lambda function will create code deployment for running install script on target instance by codedeploy-agent
2. Gitlab pipeline jobs to build and deploy
-
image_version.txt
is packed indeploy.zip
for codedeploy get version to deploy - Codedeploy structure:
- application: service_name (app, api, myweb, etc.)
- deployment group: branch_name (feature, develop, hotfix, master, integration, etc.), each one will have same or different target instancce
-
.gitlab-ci.yml
</li> </ul> <p>build:<br> stage: build<br> script:<br> - echo compile and package<br> - echo tag image version<br> - branch_name=$(echo $CI_COMMIT_REF_NAME | sed 's/\//-/g')<br> - version="$branch_name-$CI_PIPELINE_ID"<br> - echo login ECR and push image<br> - eval $(aws ecr get-login --no-include-email --region ap-northeast-1)<br> - docker tag app:latest myimage:${version}<br> - docker push myimage:${version}<br> only:<br> refs:<br> - feature<br> - develop<br> - integration<br> - hotfix<br> - master<br> changes:<br> - src/*<em>/</em><br> tags:<br> - build-runner</p> <p>deploy:<br> stage: deploy<br> script:<br> - echo "Deploy app"<br> - branch_name=$(echo $CI_COMMIT_REF_NAME | sed 's/\//-/g')<br> - version="$branch_name-$CI_PIPELINE_ID"<br> - echo $version > codedeploy/image_version.txt<br> - cd codedeploy<br> - zip -r deploy.zip appspec.yml image_version.txt scripts<br> - aws s3 cp deploy.zip s3://codedeploy/automation/${CI_COMMIT_REF_NAME}/app/deploy.zip --metadata x-amz-meta-application-name=app,x-amz-meta-deploymentgroup-name=${obj}<br> only:<br> refs:<br> - feature<br> - develop<br> - integration<br> - hotfix<br> - master<br> changes:<br> - src/*<em>/</em><br> tags:<br> - deploy-runner</p> <p></p> <div class="highlight"><pre class="highlight plaintext"><code> ###**3. Install script** ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/texdrp5op28irl7g35b2.png) - The script is run on target instance by codedeploy-agent [`install.sh`](https://github.com/vumdao/codedeploy/blob/master/cicd/codedeploy/scripts/install.sh) </code></pre></div> <p></p> <p>$ cat codedeploy/scripts/install.sh </p> <h1> <a name="binbash" href="#binbash"> </a> !/bin/bash </h1> <h1> <a name="script-is-run-on-instance" href="#script-is-run-on-instance"> </a> Script is run on instance </h1> <h1> <a name="get-app-version" href="#get-app-version"> </a> Get app version </h1> <p>dir=$(dirname "$0")<br> version=$(cat ${dir}/../image_version.txt)</p> <h1> <a name="tracking-version" href="#tracking-version"> </a> Tracking version </h1> <p>OPS_DIR="/ect/ops"<br> export APP_VERSION=${version}</p> <h1> <a name="compose-up" href="#compose-up"> </a> Compose up </h1> <p>docker-compose up -d app</p> <p></p> <div class="highlight"><pre class="highlight plaintext"><code> ###**4. [`appspec.yml`](https://github.com/vumdao/codedeploy/blob/master/cicd/codedeploy/appspec.yml)** </code></pre></div> <p></p> <p>version: 0.0<br> os: linux<br> hooks:<br> BeforeInstall:<br> - location: scripts/install.sh<br> timeout: 300<br> runas: root</p> <p></p> <div class="highlight"><pre class="highlight plaintext"><code> --- <h3 align="center"> <a href="https://dev.to/vumdao">:stars: Blog</a> <span> · </span> <a href="https://github.com/vumdao/">Github</a> <span> · </span> <a href="https://vumdao.hashnode.dev/">Web</a> <span> · </span> <a href="https://www.linkedin.com/in/vu-dao-9280ab43/">Linkedin</a> <span> · </span> <a href="https://www.linkedin.com/groups/12488649/">Group</a> <span> · </span> <a href="https://www.facebook.com/CloudOpz-104917804863956">Page</a> <span> · </span> <a href="https://twitter.com/VuDao81124667">Twitter :stars:</a> </h3> Ref: https://github.com/vumdao/codedeploy </code></pre></div>
Top comments (1)
Very useful !