DevOps / GitHub Actions Interview Questions
How do you implement a complete CI/CD pipeline for a container image in GitHub Actions — build, push to a registry, and deploy?
A typical container CI/CD pipeline in GitHub Actions has three stages: build the image, push it to a registry, and trigger a deployment. Here is a production-ready example using GitHub Container Registry (GHCR):
name: Build and Deploy Container on: push: branches: [main] permissions: contents: read packages: write # needed to push to GHCR jobs: build-and-push: runs-on: ubuntu-latest outputs: image-tag: ${{ steps.meta.outputs.tags }} steps: - uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GHCR uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository }} tags: | type=sha,prefix=sha- type=ref,event=branch - name: Build and push uses: docker/build-push-action@v6 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} cache-from: type=gha cache-to: type=gha,mode=max deploy: needs: build-and-push runs-on: ubuntu-latest environment: production steps: - name: Deploy to Kubernetes run: | kubectl set image deployment/myapp myapp=ghcr.io/${{ github.repository }}:sha-${{ github.sha }} env: KUBECONFIG: ${{ secrets.KUBECONFIG }}
Notable patterns used here:
docker/setup-buildx-actionenables BuildKit for multi-platform builds and layer caching.type=ghacache inbuild-push-actionstores Docker build cache in GitHub Actions cache, dramatically speeding up incremental builds.docker/metadata-actiongenerates consistent image tags from git metadata.- The
deployjob uses a GitHub Environment (environment: production) which can require manual approval, environment-specific secrets, and deployment protection rules.
More Related questions...