{{announcement.body}}
{{announcement.title}}

GitHub Actions: How to Improve Your CI/CD Workflow

DZone 's Guide to

GitHub Actions: How to Improve Your CI/CD Workflow

This article takes a look at how to improve your CI/CD workflow by using automated tasks generated by GitHub Actions.

· DevOps Zone ·
Free Resource

GitHub Actions is one of the most powerful tools that you can use if you are storing our code on this service repository. The idea is simple, you can design a complex workflow using automated tasks (called actions) in order to be activated and run when a commit is done in your repository.

The evolution of GitHub from a simple distributed git repository to something bigger is notorious. Tasks include cloud computing, so you can build images, push to different repositories, orchestrate cloud providers or whatever you need to do without any computing resources on your side, so any server or local machine is required. Even better, you can choose which kind of operating system will be used to run actions: Ubuntu Linux, Windows or Mac OS X.

GitHub has different official actions, but they also launched a Marketplace where you can find several actions created by the community, so the integration with your own workflow is quicker and easier than writing the whole code from the scratch. During the next steps will use both of them: official and community ones.

Building Docker Images, Send Them to Docker Hub and Deploy Them On AWS ECS.

Imagine this scenario: you are developing your app using Docker containers, storing your code on GitHub, pushing your image on Docker Hub and deploying the app on AWS ECS to be run on your cloud platform.

Initial tip: By default, GitHub is going to notify you all workflow errors by email. This could be something useful in production environments but I really recommended to disable them if you are doing some tests.  To do it, go to your GitHub account settings -> Notifications and uncheck the Email box.

Github Actions


First of all, upload a Dockerfile to your repository if you do not have one. Then, go to your GitHub repository and click on Actions. Depending on the content of your repository you will receive a different suggestion, build a Docker image, build a Go project or just the most popular continuous integration workflows. Skip the recommendations and go to Set up a workflow yourself.

Set up workflow


A new file will be created using the path your_repository/.github/workflows named by default main.yml but you can rename it as you prefer. Our first step will be publishing the image to Docker Hub so you can set dockerhub.yml.

publishing image to Docker Hub


The code is running a hello world sample, so you can remove all steps. As you can see, the right side window shows the marketplace and the documentation tabs. This will be there always you are editing an action file.

Go to the Search field box and type Docker and select Publish Docker by elgohr. This action is going to build our Dockerfile and push the built image to Docker Hub. You just need to copy the content, paste into your file and fill out the variables with your own values, to get something like that:

Shell
 




x
30


 
1
name: Build and Publish to Docker Hub
2
on: [push]
3
jobs:
4
  build:
5
    runs-on: ubuntu-latest
6
    steps:
7
    - uses: actions/checkout@master
8
    - name: Publish Docker
9
      uses: elgohr/Publish-Docker-Github-Action@2.11
10
      with:
11
        # The name of the image you would like to push
12
        name: cloudblog/githubactions:v7
13
        # The login username for the registry
14
        username: ${{ secrets.DOCKERHUB_USER }}
15
        # The login password for the registry
16
        password: ${{ secrets.DOCKERHUB_PASS }}
17
        # Use registry for pushing to a custom registry
18
        #registry: # optional
19
        # Use snapshot to push an additional image
20
        #snapshot: # optional
21
        # Use dockerfile when you would like to explicitly build a Dockerfile
22
        #dockerfile: Dockerfile
23
        # Use workdir when you would like to change the directory for building
24
        #workdir: # optional
25
        # Use buildargs when you want to pass a list of environment variables as build-args
26
        #buildargs: # optional
27
        # Use cache when you have big images, that you would only like to build partially
28
        #cache: # optional
29
        # Use tag_names when you want to push tags/release by their git name
30
        #tag_names: $(date +%s)


This action is going to get our Dockerfile, build the image cloudblog/githubactions:v7 and push it to Docker Hub.

Security tip: You should avoid writing your username and password on any file, even on private repositories, using secrets instead. You just need to store them on Settings → Secrets and reference them using the syntax:

 ${{ secrets.MYSERVICE_PASS }} 

Adding a new secret

Working With AWS ECR & ECS

Our second step will be to deploy the image to AWS ECR. We can add more steps on the same file or we can create a new file, depending on our preferences. As both processes are independent I prefer to create a different file to get two workflows, even the trigger will be the same (a commit), so both will be running in parallel.

Go to Actions again and click on New Workflow at your left. This time we are going to use an official action, so search for Deploy to Amazon ECS and click on Set up this workflow.

CI Workflows


You just need to replace your own values related to your AWS environment following the instructions of the text. Then you need to copy the task definition file in JSON format to your GitHub repository.  To get the required JSON code just open your task definition on AWS ECS and click on JSON tab:

JSON tab


The final action file should be something similar to this one:

Shell
 




xxxxxxxxxx
1
56


 
1
on:
2
  push:
3
    branches:
4
      - master
5
 
          
6
name: Deploy to Amazon ECS
7
 
          
8
jobs:
9
  deploy:
10
    name: Deploy
11
    runs-on: ubuntu-latest
12
 
          
13
steps:
14
- name: Checkout
15
  uses: actions/checkout@v1
16
 
          
17
- name: Configure AWS credentials
18
  uses: aws-actions/configure-aws-credentials@v1
19
  with:
20
    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
21
    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
22
    aws-region: eu-west-1
23
 
          
24
- name: Login to Amazon ECR
25
  id: login-ecr
26
  uses: aws-actions/amazon-ecr-login@v1
27
 
          
28
- name: Build, tag, and push image to Amazon ECR
29
  id: build-image
30
  env:
31
    ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
32
    ECR_REPOSITORY: githubactions
33
    IMAGE_TAG: latest
34
  run: |
35
    # Build a docker container and
36
    # push it to ECR so that it can
37
    # be deployed to ECS.
38
    docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
39
    docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
40
    echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
41
 
          
42
- name: Fill in the new image ID in the Amazon ECS task definition
43
  id: task-def
44
  uses: aws-actions/amazon-ecs-render-task-definition@v1
45
  with:
46
    task-definition: mytaskdef.json
47
    container-name: cloudblog
48
    image: ${{ steps.build-image.outputs.image }}
49
 
          
50
- name: Deploy Amazon ECS task definition
51
  uses: aws-actions/amazon-ecs-deploy-task-definition@v1
52
  with:
53
    task-definition: ${{ steps.task-def.outputs.task-definition }}
54
    service: myapp
55
    cluster: cloudblog
56
    wait-for-service-stability: true


You can see several actions are being called in the same workflow definition file. Most of the steps are references to another action like login or deploy the task definition but you can also run commands like the building images using the Docker CLI.

Just save the file with the default name and commit the changes. Go to Actions and you will be able to see the output. If everything was fine you should get something like this:

Actions output

If you get some errors, you can go to the details for every step and check the output:

Output


When everything is running fine, you should see your image on Docker Hub and AWS repositories and the app running in your AWS ECS environment:

App running in AWS ECS

Github Actions

To going further just edit your current action file and look for new actions (or create your own one). You can add more steps to your workflow like update a Lambda function, copy a file to a S3 bucket to generate a trigger on AWS or deploy to multiple cloud providers at the same time using Azure and GCP related actions.

Finishing Up

And that's about it! I hope you liked this post! More to come soon.

This article was previously published at https://appfleet.com/.

Topics:
automation, ci/cd, devops, docker, github, github actions

Published at DZone with permission of Sudip Sengupta . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}