Maven / 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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
