Setup CI for a containerized service

ยท

3 min read

In this blog we will setup a CI pipeline with the following stages

  • Build API

  • Run tests

  • Perform code linting

  • Docker login

  • Docker build and push

Steps to setup self-hosted Github Actions

  • Click on Actions dropdown and click on Runners.

  • Click on New self-hosted runner button

  • Follow the instructions on this page and setup a self-hosted runner and make sure to set runs-on: self-hosted

  • Finally let us start our self-hosted runner to run workflows on our system:

        cd actions-runner/
        ./run.sh
    
  • To setup github actions follow the below command from the root of the project folder and write your github action config on a .yaml file

        mkdir -p .github/workflows/
        cd .github/workflows/
        touch <github-action-filename>.yaml
    
name: Workflow for building API, testing, lint, build docker Image and pushing it to Docker Hub

# Define the events that trigger the workflow
on:
  push: # Workflow triggers on push events to specific branches
    branches:
      - main
      - milestone-* # Matches any branch starting with 'milestone-'
  pull_request: # Workflow also triggers on pull request events
    types: [opened] # Specifically when a pull request is opened
    branches:
      - milestone-* # Only for branches starting with 'milestone-'

# Define the jobs that make up the workflow
jobs:
  CI: # Job identifier
    name: CI for a web service # Job name
    runs-on: self-hosted # Specifies that the job runs on a self-hosted runner
    permissions: # Permissions for the job
      packages: write
      contents: read
      attestations: write
      id-token: write

    steps: # Steps to be executed in this job
      - name: pull code # Step to check out the repository code
        uses: actions/checkout@v4

      - name: Build and Test # Step to set up Node.js environment
        uses: actions/setup-node@v4
        with:
          node-version: "20.14" # Specifies the Node.js version to use

      - run: make ci # Run the 'make ci' command to build the project
      - run: make test # Run the 'make test' command to test the project
      - run: make lint # Run the 'make lint' command to lint the project

      - name: Log in to Docker Hub # Step to log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{secrets.DOCKER_USERNAME}} # Docker Hub username from secrets
          password: ${{secrets.DOCKER_PASSSWORD}} # Docker Hub password from secrets

      - name: Extract metadata (tags, labels) for Docker # Step to extract metadata for the Docker image
        id: meta
        uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
        with:
          images: ${{secrets.DOCKER_USERNAME}}/sre-bootcamp-web-server # Docker image name

      - name: Build and push Docker image # Step to build and push the Docker image
        id: push
        uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
        with:
          context: . # Build context
          file: ./Dockerfile # Path to the Dockerfile
          push: true # Push the image to Docker Hub
          tags: ${{ steps.meta.outputs.tags }} # Tags for the image from metadata extraction
          labels: ${{ steps.meta.outputs.labels }} # Labels for the image from metadata extraction

Make sure you pass DockerHub username and password via Github Secrets.

ย