Understanding Automated Production Deployment
Variables - in a real pipeline these are injected as environment variables
IMAGE_TAG="${CI_COMMIT_SHA:-latest}" NAMESPACE="production" DEPLOYMENT="web-app"
1. Update the image tag in the manifest (using yq for YAML manipulation)
yq e ".spec.template.spec.containers[0].image = "myrepo/web-app:${IMAGE_TAG}"" -i k8s/${DEPLOYMENT}.yaml
2. Apply the manifest
kubectl apply -f k8s/${DEPLOYMENT}.yaml --namespace ${NAMESPACE}
3. Wait for a successful rollout
kubectl rollout status deployment/${DEPLOYMENT} --namespace ${NAMESPACE}
echo "✅ Deployment ${DEPLOYMENT} updated to ${IMAGE_TAG}"</code></pre>
<p>While simplistic, the script illustrates the three pillars of automation: <em>parameterisation</em>, <em>infrastructure as code</em>, and <em>feedback</em>. In production‑grade pipelines these steps are expanded with testing, security scanning, and multi‑region promotion.</p>Core Components of a Robust Strategy
on: push: branches: [ main ] pull_request: branches: [ main ]
jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Lint Dockerfile
uses: hadolint/hadolint-action@v2
with:
dockerfile: Dockerfile
- name: Build and push image
id: docker_build
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: myrepo/web-app:${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max
- name: Trivy vulnerability scan
uses: aquasecurity/trivy-action@0.10.1
with:
image-ref: myrepo/web-app:${{ github.sha }}
format: table
exit-code: '1'
ignore-unfixed: true
deploy: needs: build runs-on: ubuntu-latest environment: production steps: - name: Checkout Helm chart uses: actions/checkout@v3 with: repository: org/helm-charts path: charts
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'v1.27.0'
- name: Deploy with Helm
env:
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
run: |
echo "$KUBE_CONFIG_DATA" | base64 -d > $HOME/.kube/config
helm upgrade --install web-app \
charts/web-app \
--namespace production \
--set image.tag=${{ github.sha }} \
--wait
</code></pre>
<p>This workflow demonstrates tight integration between CI (build, test, scan) and CD (Helm deployment). The <code>needs: build</code> dependency guarantees that only a successfully built image can progress to production.</p>