SkillSprout
Chapter 2 · Lesson 5
Advanced
2 min read

Build and Deploy Images in CI/CD

Overview

Continuous Integration automatically tests and builds every change. Continuous Delivery prepares verified releases for deployment; Continuous Deployment may deploy them automatically. Docker provides an immutable artifact that can move through a pipeline.

Detailed Notes

GitHub Actions workflow

yaml
name: Build and Push

on:
  push:
    branches: [main]

permissions:
  contents: read
  packages: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build image
        run: docker build -t ghcr.io/example/orders-api:${GITHUB_SHA} .

      - name: Scan image
        run: docker scout cves ghcr.io/example/orders-api:${GITHUB_SHA}

      - name: Log in to registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin

      - name: Push image
        run: docker push ghcr.io/example/orders-api:${GITHUB_SHA}
  • on.push: start on pushes to main.
  • permissions: grant only required workflow access.
  • checkout: download the source commit.
  • GITHUB_SHA: immutable commit identifier.
  • secrets.GITHUB_TOKEN: short-lived Actions credential.
  • docker push: publish the verified image.

Add tests before build:

yaml
- name: Test
  run: npm test
- name: Build
  run: docker build -t app:${GITHUB_SHA} .

For pull requests, build and test but usually avoid pushing production credentials or deploying.

Docker CI/CD pipeline
Architecture
Git pushTestBuild imageScanPushDeploy
Key point. Source push triggers CI.

One image moves through testing, scanning and promotion.

Common Interview Questions

Finished this lesson?

0 of 27 lessons done · 0% of the course