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
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 thanlatest; promote that exact image. docker/build-push-actionwith 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. concurrencyprevents 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
- Explain how a GitHub Actions workflow is structured: events, jobs, steps, runners and actions.
- How do you keep secrets safe in GitHub Actions, and why use OIDC to reach the cloud?
- How do you speed up slow GitHub Actions workflows?
- Reusable workflows or composite actions: what's the difference, and when do you use each?
- How do you add approval gates to deployments in GitHub Actions?
- Why should you pin third-party actions to a commit SHA?
- What are the security risks of pull_request_target and self-hosted runners?
- How does the GITHUB_TOKEN work, and how do you limit its permissions?