Post

GitHub Actions for building Docker image

Introduction

You only need a GitHub repository to create and run a GitHub Actions workflow.

The following example shows you how GitHub Actions jobs can be automatically triggered, where they run, and how they can interact with the code in your repository.

Creating your workflow

Create a .github/workflows directory in your repository on GitHub if this directory does not already exist.

In the .github/workflows directory, create a file named like github-actions.yml.

Example YAML file:

The double curly braces are missing for the GitHub action, don’t forget to put them back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
name: 0.3 - Build and Push Backend Image

env:
  AWS_REGION: 'X-x-X'
  aws_env: 'dev'

on:
  #push:
  #  branches: [ dev ]
  workflow_dispatch:
  
jobs:
  Build_and_Deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${\{secrets.AWS_ACCESS_KEY_ID_DEV}} 
        aws-secret-access-key: ${\{secrets.AWS_SECRET_ACCESS_KEY_DEV}} 
        aws-region: ${\{env.AWS_REGION}} 

    - name: Retrieve an authentication token
      run: |
        aws ecr get-login-password \
        --region ${\{env.AWS_REGION}}  | docker login \
        --username AWS \
        --password-stdin $(aws sts get-caller-identity --query "Account" --output text).dkr.ecr.${\{env.AWS_REGION}} .amazonaws.com

    - name: Build docker image
      run: |
        docker build -t $(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" \
        --query "Parameter.Value" --output text) \
        -f ./Dockerfile .

    - name: Tag docker image
      run: |
        docker tag $(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text):latest $(aws sts get-caller-identity --query "Account" --output text).dkr.ecr.${\{env.AWS_REGION}} .amazonaws.com/$(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text):latest

    - name: Push docker image to ECR repository
      run: | 
        docker push $(aws sts get-caller-identity --query "Account" --output text).dkr.ecr.${\{env.AWS_REGION}} .amazonaws.com/$(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text):latest

Example Update docker image YAML file:

The double curly braces are missing for the GitHub action, don’t forget to put them back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: 0.4 - Push new Backend Image and Update ECS 

env:
  aws_env: 'dev'
  AWS_REGION: 'eu-west-1'

on:
  #push:
  #  branches: ['main']
  #  paths: ['backend/**']
  workflow_dispatch:
  
jobs:
  Build_and_Deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${\{secrets.AWS_ACCESS_KEY_ID_DEV}} 
        aws-secret-access-key: ${\{secrets.AWS_SECRET_ACCESS_KEY_DEV}} 
        aws-region: ${\{env.AWS_REGION}} 

    - name: Retrieve an authentication token
      run: |
        aws ecr get-login-password --region ${\{env.AWS_REGION}}  | docker login \
        --username AWS \
        --password-stdin $(aws sts get-caller-identity --query "Account" --output text).dkr.ecr.${\{env.AWS_REGION}} .amazonaws.com

    - name: Build new Backend docker image
      run: |
        docker build -t $(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text) \
        -f ./Dockerfile .

    - name: Tag docker image version
      run: |
        docker tag $(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text):latest $(aws sts get-caller-identity --query "Account" --output text).dkr.ecr.${\{env.AWS_REGION}} .amazonaws.com/$(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text):latest

    - name: Push docker image to ECR repository
      run: | 
        docker push $(aws sts get-caller-identity \
        --query "Account" \
        --output text).dkr.ecr.${\{env.AWS_REGION}} .amazonaws.com/$(aws ssm get-parameter --name "$ env.aws_env .ECRepo.App" --query "Parameter.Value" --output text):latest

    - name: Update ECS cluster with new Backend image
      run: | 
        aws ecs update-service \
        --cluster $(aws ssm get-parameter --name "$ env.aws_env .ECSCluster.App" --query "Parameter.Value" --output text) \
        --service App-service \
        --force-new-deployment

# Send notification to Slack private chanel.
  slack-workflow-status:
    if: always()
    name: Post Workflow Status To Slack
    needs:
      - Build_and_Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Slack Workflow Notification
        id: slack
        uses: slackapi/slack-github-action@v1.23.0
        with:
          # Optional Input
          name: 'Project - New Backend Version'
          # For posting a rich message using Block Kit
          payload: |
            {
              "text": "Project - New Backend Version GitHub Action build result: $ job.status \n$ github.event.pull_request.html_url || github.event.head_commit.url ",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "Project - New Backend Version GitHub Action build result: $ job.status \n$ github.event.pull_request.html_url || github.event.head_commit.url "
                  }
                }
              ]
            }
        env:
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
          SLACK_WEBHOOK_URL: ${\{secrets.SLACK_WEBHOOK_URL}} 
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.