DEV Community

Cover image for Gitlab Pipeline With AWS Codedeploy
🚀 Vu Dao 🚀
🚀 Vu Dao 🚀

Posted on • Edited on

Gitlab Pipeline With AWS Codedeploy

Flow Chart

Alt Text

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 Alt Text

Alt Text

2. Gitlab pipeline jobs to build and deploy

Alt Text

  • image_version.txt is packed in deploy.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 &gt; 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>
    ---
    
    &lt;h3 align="center"&gt;
      &lt;a href="https://dev.to/vumdao"&gt;:stars: Blog&lt;/a&gt;
      &lt;span&gt; · &lt;/span&gt;
      &lt;a href="https://github.com/vumdao/"&gt;Github&lt;/a&gt;
      &lt;span&gt; · &lt;/span&gt;
      &lt;a href="https://vumdao.hashnode.dev/"&gt;Web&lt;/a&gt;
      &lt;span&gt; · &lt;/span&gt;
      &lt;a href="https://www.linkedin.com/in/vu-dao-9280ab43/"&gt;Linkedin&lt;/a&gt;
      &lt;span&gt; · &lt;/span&gt;
      &lt;a href="https://www.linkedin.com/groups/12488649/"&gt;Group&lt;/a&gt;
      &lt;span&gt; · &lt;/span&gt;
      &lt;a href="https://www.facebook.com/CloudOpz-104917804863956"&gt;Page&lt;/a&gt;
      &lt;span&gt; · &lt;/span&gt;
      &lt;a href="https://twitter.com/VuDao81124667"&gt;Twitter :stars:&lt;/a&gt;
    &lt;/h3&gt;
    
    
    Ref: https://github.com/vumdao/codedeploy
    </code></pre></div>
    
    Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
tuannguyen91vn profile image
Blake (Tuan Nguyen)

Very useful !