AWS Docker Deploy
A pipeline that builds/pushes images to AWS ECR and deploys to EKS
A pipeline that builds Docker images and deploys them to an EKS cluster in an AWS environment.
Prerequisites
The following secrets and vars must be registered in advance:
| Type | Key | Description |
|---|---|---|
| Secret | AWS_ACCESS_KEY_ID | AWS Access Key |
| Secret | AWS_SECRET_ACCESS_KEY | AWS Secret Key |
| Var | AWS_REGION | AWS region (e.g., ap-northeast-2) |
| Var | AWS_ACCOUNT_ID | AWS account ID |
| Var | EKS_CLUSTER | EKS cluster name |
Full Code
name: aws-deploy
# Runs on main branch push
triggers:
push:
branches: [main]
paths: [src/**, Dockerfile, package.json]
env:
AWS_REGION: "${{ vars.AWS_REGION }}"
ECR_REGISTRY: "${{ vars.AWS_ACCOUNT_ID }}.dkr.ecr.${{ vars.AWS_REGION }}.amazonaws.com"
IMAGE_NAME: "my-app"
jobs:
# ─────────────────────────────────────────
# 1. Source code checkout
# ─────────────────────────────────────────
checkout:
phase: source
steps:
- name: checkout
uses: "collabops/checkout@v2"
with:
repo-url: "https://<collabops-host>/<workspace>/<repository>.git"
# ─────────────────────────────────────────
# 2. AWS authentication + ECR authentication
# ─────────────────────────────────────────
auth:
phase: source
needs: [checkout]
steps:
# AWS credentials setup
- name: aws-auth
uses: "collabops/aws-auth@v1"
with:
access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
region: ${{ vars.AWS_REGION }}
# ECR Docker authentication
- name: ecr-auth
uses: "collabops/aws-ecr-auth@v1"
with:
region: ${{ vars.AWS_REGION }}
# ─────────────────────────────────────────
# 3. Test
# ─────────────────────────────────────────
test:
phase: test
needs: [checkout]
steps:
- name: unit-test
image: node:18
run: |
cd /workspace/source
npm ci
npm test
env:
CI: "true"
# ─────────────────────────────────────────
# 4. Docker image build and ECR push
# ─────────────────────────────────────────
build:
phase: build
needs: [auth, test] # After both authentication + test complete
steps:
# Use commit SHA as image tag
- name: prepare-tag
id: tag
run: |
SHORT_SHA=$(echo "${{ collabops.sha }}" | cut -c1-7)
echo "image_tag=${SHORT_SHA}" >> $COLLABOPS_OUTPUT
- name: build-push
uses: "collabops/docker-build-push@v1"
with:
tags: |
${{ env.ECR_REGISTRY }}/${{ env.IMAGE_NAME }}:latest
${{ env.ECR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.image_tag }}
build-args: |
NODE_ENV=production
# ─────────────────────────────────────────
# 5. EKS deployment
# ─────────────────────────────────────────
deploy:
phase: deploy
needs: [build]
if: "collabops.ref == 'refs/heads/main' && collabops.event_name == 'push'"
steps:
# EKS cluster authentication and kubectl setup
- name: eks-setup
uses: "collabops/aws-eks-setup@v1"
with:
region: ${{ vars.AWS_REGION }}
cluster-name: ${{ vars.EKS_CLUSTER }}
# Deploy via kubectl
- name: rollout
image: amazon/aws-cli:2.22.35
run: |
SHORT_SHA=$(echo "${{ collabops.sha }}" | cut -c1-7)
IMAGE="${{ env.ECR_REGISTRY }}/${{ env.IMAGE_NAME }}:${SHORT_SHA}"
# Update Deployment image
kubectl set image deployment/${{ env.IMAGE_NAME }} \
app=${IMAGE} \
-n production
# Wait for rollout to complete (up to 5 minutes)
kubectl rollout status deployment/${{ env.IMAGE_NAME }} \
-n production \
--timeout=300s
echo "Deployed: ${IMAGE}"
# ─────────────────────────────────────────
# 6. Slack notification
# ─────────────────────────────────────────
notify:
needs: [deploy]
if: "always()"
steps:
- name: slack
uses: "collabops/slack-notify@v1"
with:
webhook-url: ${{ secrets.SLACK_WEBHOOK }}
message: "EKS deploy: ${{ env.IMAGE_NAME }} (${{ collabops.sha }})"
title: "Deploy to EKS"
color: goodExecution Flow
checkout
├── auth (AWS + ECR authentication)
└── test (unit tests)
└── build (Docker build & ECR push)
└── deploy (EKS deployment, main push only)
└── notify (Slack notification, always)