GitHub Actions Fundamentals interview question · Most asked

Write a GitHub Actions workflow that tests, builds and pushes a Docker image, and deploys only from main.

Hands-on Asked in DevOps Engineer interviews

Members can save this page, take notes and track progress. See membership · Sign in

Summary

Run tests on every push and pull request, build the image tagged with the commit SHA, push it only for main, and deploy in a separate job that needs the build and targets a protected production environment.

Longer Answer

Details worth explaining:

  • Tag with ${{ github.sha }} (immutable) rather than latest; promote that exact image.
  • docker/build-push-action with the GitHub Actions cache (cache-from: type=gha) speeds up rebuilds.
  • if: github.ref == 'refs/heads/main' && github.event_name == 'push' keeps pull requests from pushing or deploying.
  • The deploy job uses environment: production, so required reviewers can approve it, and gets cloud access through OIDC.
  • concurrency prevents two deploys running at once.

Example

name: ci-cd
on: { push: { branches: [main] }, pull_request: {} }
permissions: { contents: read, packages: write, id-token: write }
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: make test
  image:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with: { registry: ghcr.io, username: "${{ github.actor }}", password: "${{ secrets.GITHUB_TOKEN }}" }
      - uses: docker/build-push-action@v6
        with:
          push: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
  deploy:
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    needs: image
    runs-on: ubuntu-latest
    environment: production
    concurrency: production
    steps:
      - run: ./deploy.sh "ghcr.io/${{ github.repository }}:${{ github.sha }}"

Key Points

  • Test everything; push and deploy only from main.
  • Immutable SHA tags.
  • Build cache for speed.
  • Protected environment and OIDC for the deploy.
  • Concurrency group for deploys.

Explanation

What interviewers look for

A working, safe pipeline with gating, immutable tags and a protected deploy.

More GitHub Actions Fundamentals questions

← All GitHub Actions Fundamentals questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake