Step 6 Setup Continuous Integration (CI) - truongnhatbui/techtrends GitHub Wiki

Continuous Integration (CI) implies the integration of code into a shared repository, where at each push to the main branch, the code is built and tested. Frequently, the result of CI represents an artifact or a Docker image.

In this project, you will use GitHub Actions to automate the packaging of an application.

Environment Setup

Prepare the GitHub repository and application to test GitHub actions:

  1. Create a new GitHub repository with the name techtrends
  2. Copy the techtrends application from the techtrends repository.
  3. Copy the techtrends application from the techtrends repository.

Exercise

Create a new GitHub Actions in the /.github/workflows/docker-build.yml that will build and push the Docker image for a techtrends web application, with the following requirements:

Image name: techtrends Tag: latest Platforms: platforms: linux/amd64,linux/arm64

The following snippet showcases the Docker build and push of the application, under .github/workflows/docker-build.yaml file:

# This is a basic workflow to help you get started with Actions

name: Docker Build and Push

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64
          push: true
          tags: buinhattruong/techtrends:latest

Encrypted secrets https://docs.github.com/en/actions/reference/encrypted-secrets

The Docker build and push workflow can be found in course repository. Make sure to move this file to .github/workflows/docker-build.yaml location to execute it.

GitHub marketplace has a rich suite of upstream actions that can be easily integrated within a repository. One of the upstream action is Build and Push Docker images, which can be used to implement the required CI task.

The above GitHub action uses DockerHub Tokens and encrypted GitHub secrets to login into DockerHub and to push new images. To set up these credentials refer to the following resources:

Create DockerHub Tokens https://www.docker.com/blog/docker-hub-new-personal-access-tokens/ Create GitHub encrypted secrets https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets

Note: you will need a Dockerfile to build the image.

⚠️ **GitHub.com Fallback** ⚠️