<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Madan Kumar</title>
    <description>The latest articles on DEV Community by Madan Kumar (@smadan2703).</description>
    <link>https://dev.to/smadan2703</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1147714%2F9cd44036-7a9a-44de-92e5-19a4709acf26.jpg</url>
      <title>DEV Community: Madan Kumar</title>
      <link>https://dev.to/smadan2703</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smadan2703"/>
    <language>en</language>
    <item>
      <title>Use Github actions for Trunk Based Development to deploy AWS ECS Service</title>
      <dc:creator>Madan Kumar</dc:creator>
      <pubDate>Sun, 27 Aug 2023 22:34:48 +0000</pubDate>
      <link>https://dev.to/smadan2703/use-github-actions-for-trunk-based-development-to-deploy-aws-ecs-service-59n5</link>
      <guid>https://dev.to/smadan2703/use-github-actions-for-trunk-based-development-to-deploy-aws-ecs-service-59n5</guid>
      <description>&lt;p&gt;Trunk based development as a branching model is preferred compared to something like Git / GitHub Flow due its simplicity. But creating a CI/CD pipeline is more challenging since we deploy to every environment from the same branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Trunk-Based Development
&lt;/h2&gt;

&lt;p&gt;Trunk-based development involves frequent &amp;amp; small code check-in to a repository, typically known as the "trunk". This approach contrasts traditional development models (like Git Flow), which often involve long, isolated development cycles followed by large code merges. In trunk-based development, teams are encouraged to commit their code to the trunk frequently, often multiple times per day. This allows for continuous integration and continuous deployment (CI/CD), as well as easier collaboration and code reviews. You also don't have branches per environment like a development branch that is deploying to a development environment. You integrate all code on the "trunk".&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating GitHub Actions Workflows
&lt;/h2&gt;

&lt;p&gt;I have two environment development &amp;amp; Production&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezj3hiiuk2pp6os4tdva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezj3hiiuk2pp6os4tdva.png" alt="Image description" width="626" height="898"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configure Open ID Connect to get short-lived credentials for my AWS account and store the ARN in Action Secret as . To setup follow this &lt;a href="https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/"&gt;https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Create Personal Access Token for Git, this is used for creating tags &amp;amp; Release and store the value in Action Secret as &lt;/li&gt;
&lt;li&gt;create directory called deployments in the root folder of git. deployments (this will hold the task definition files required for AWS)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhuepvsh2zqtehrh402n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkhuepvsh2zqtehrh402n.png" alt="Image description" width="590" height="418"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  GitHub Action Workflow
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;dev-deploy-cicd.yml&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We trigger the pipeline on pushes to main (our "trunk"). Remember, we don't have different branches per environment. We deploy to all our environments from this branch.&lt;br&gt;
Below action will be triggered when a PR or Push happens to main branch and does the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build and push a new docker image to ECR&lt;/li&gt;
&lt;li&gt;update the task definition with the new image tag&lt;/li&gt;
&lt;li&gt;Update the services with the latest task definition and redeployment&lt;/li&gt;
&lt;li&gt;Create Draft Release and Tag it using semantic versioning
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: CICD API 

on:
  push:
    branches:
      - main

env:
  AWS_ROLE: ${{ secrets.DEV_AWS_GITHUB_ACTION_ROLE }}
  AWS_REGION:                    # set this to preferred AWS region, e.g. us-west-1
  ECR_REPOSITORY:                  # set this to Amazon ECR repository name
  ECS_SERVICE:                   # set this to Amazon ECS service name
  ECS_CLUSTER:                         # set this to Amazon ECS cluster name
  ECS_TASK_DEFINITION: deployments              # set this to the path Amazon ECS task definition  
  ECS_CLUSTER_ENV:                     # set this to Amazon ECS cluster Environment
  CONTAINER_NAME:                   # containerDefinitions section of your task definition

permissions:
  id-token: write
  contents: read

jobs:
  deploy-develop:
    name: Build &amp;amp; Deploy
    runs-on: ubuntu-latest
    environment: develop

    steps:
    - name: Source Code Checkout
      uses: actions/checkout@v3
      with:
          submodules: recursive
          token: ${{ secrets.ORG_GH_ACCESS_TOKEN }}

    - name: Configure AWS Credentials Using Role
      uses: aws-actions/configure-aws-credentials@v2
      with:
        role-to-assume: ${{ env.AWS_ROLE }}
        aws-region: ${{ env.AWS_REGION }}
        #mask-aws-account-id: 'false'

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1
      with:
        mask-password: 'true'

    - name: Generate Short GIT Commit HASH
      id: vars
      run: echo "sha_short=$(git rev-parse --short HEAD)" &amp;gt;&amp;gt; "$GITHUB_OUTPUT"

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        IMAGE_TAG: ${{ steps.vars.outputs.sha_short }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" &amp;gt;&amp;gt; "$GITHUB_OUTPUT"

    - name: Update Amazon ECS task definition
      id: task-def
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: ${{ env.ECS_TASK_DEFINITION }}/task-definitions-${{ env.ECS_CLUSTER_ENV }}.json
        container-name: ${{ env.CONTAINER_NAME }}
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: ${{ env.ECS_SERVICE }}
        cluster: ${{ env.ECS_CLUSTER }}-${{ env.ECS_CLUSTER_ENV}}
        #wait-for-service-stability: true

    - name: Bump version and push tag
      id: semantic
      uses: mathieudutour/github-tag-action@v6.1
      with:
        github_token: ${{ secrets.ORG_GH_ACCESS_TOKEN }}

    - name: Create Release
      uses: softprops/action-gh-release@v1
      #if: startsWith(github.ref, 'refs/tags/v*')
      with:
        token: ${{ secrets.ORG_GH_ACCESS_TOKEN }}
        tag_name: ${{ steps.semantic.outputs.new_tag }}
        draft: true
        prerelease: true
        generate_release_notes: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31q6h32fymfuvbjqrvak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31q6h32fymfuvbjqrvak.png" alt="Image description" width="800" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcketxhfquqfygr37psnp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcketxhfquqfygr37psnp.png" alt="Image description" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;prod-release.yml&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below action will be triggered when a Draft Release was changed to Publish.&lt;br&gt;
name: Production Release&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on:
  release:
    types: [published]

env:
  AWS_ROLE: ${{ secrets.PROD_AWS_GITHUB_ACTION_ROLE }}
  AWS_REGION:                 # set this to preferred AWS region, e.g. us-west-1
  ECR_REPOSITORY:                  # set this to Amazon ECR repository name
  ECS_SERVICE:                     # set this to Amazon ECS service name
  ECS_CLUSTER:                       # set this to Amazon ECS cluster name
  ECS_TASK_DEFINITION:              # set this to the path Amazon ECS task definition  
  ECS_CLUSTER_ENV:                      # set this to Amazon ECS cluster Environment
  CONTAINER_NAME:                 # containerDefinitions section of your task definition

permissions:
  id-token: write
  contents: read

jobs:
  deploy-production:
    name: Build &amp;amp; Deploy
    runs-on: ubuntu-latest
    environment: production

    steps:
    - name: Source Code Checkout
      uses: actions/checkout@v3
      with:
          submodules: recursive
          token: ${{ secrets.ORG_GH_ACCESS_TOKEN }}

    - name: Configure AWS Credentials Using Role
      uses: aws-actions/configure-aws-credentials@v2
      with:
        role-to-assume: ${{ env.AWS_ROLE }}
        aws-region: ${{ env.AWS_REGION }}
        #mask-aws-account-id: 'false'

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1
      with:
        mask-password: 'true'

    - name: Generate Short GIT Commit HASH
      id: vars
      run: echo "sha_short=$(git rev-parse --short HEAD)" &amp;gt;&amp;gt; "$GITHUB_OUTPUT"

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        IMAGE_TAG: ${{ steps.vars.outputs.sha_short }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" &amp;gt;&amp;gt; "$GITHUB_OUTPUT"

    - name: Update Amazon ECS task definition
      id: task-def
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: ${{ env.ECS_TASK_DEFINITION }}/task-definitions-${{ env.ECS_CLUSTER_ENV }}.json
        container-name: ${{ env.CONTAINER_NAME }}
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: ${{ env.ECS_SERVICE }}
        cluster: ${{ env.ECS_CLUSTER }}-${{ env.ECS_CLUSTER_ENV}}
        #wait-for-service-stability: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjcasew5p4xj5x8xhbey.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjcasew5p4xj5x8xhbey.png" alt="Image description" width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdbw513grpueygnta1qci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdbw513grpueygnta1qci.png" alt="Image description" width="800" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
