Why I built this
Manual deployments are slow, risky and hard to repeat. I wanted one rule: if I push to main , the right thing happens in production, and bad code gets stopped before it gets there. This is the pipeline I built, why each stage exists, and the mistakes that cost me time, so you can skip them.
The architecture
- GitHub: source code and the trigger
- GitHub Actions: runs the whole workflow
- SonarCloud: code quality and security gate
- Amazon ECR: private Docker image registry
- Amazon ECS: runs the containers, behind an Application Load Balancer
- Amazon RDS: the managed database behind the app
Stage 1: Build and test
The workflow starts on every push to main . It checks out the code, sets up Java, and runs mvn test . If a single test fails, the pipeline stops here and nothing gets built.
yaml
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- run: mvn -B test
Stage 2: Quality gate with SonarCloud
Passing tests doesn’t mean healthy code. SonarCloud scans for bugs, code smells, security hotspots and coverage. I set a quality gate so the pipeline fails if new code drops below the standard. Store SONAR_TOKEN in GitHub Secrets, never in the repo.
Stage 3: Build the Docker image and push to ECR
Tag every image with the commit SHA, not just latest . This gives you traceability (which commit is running?) and instant rollbacks.
yaml
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- uses: aws-actions/amazon-ecr-login@v2
- run: |
docker build -t $ECR_REPO:${{ github.sha }} .
docker push $ECR_REPO:${{ github.sha }}
Stage 4: Deploy to ECS
The deploy step renders a new ECS task definition with the fresh image tag and updates the service. ECS performs a rolling deployment, starting new tasks and waiting for the load balancer health checks to pass before draining the old ones. Users see no downtime.
Lessons learned (the part that saves you hours)
- Health checks decide everything. A wrong health check path makes ECS kill healthy tasks in a loop. Test the path locally first.
- Security groups are the usual culprit. The ALB must reach the tasks, and the tasks must reach RDS. Check these before debugging code.
- Use IAM roles with least privilege. Give the pipeline user only ECR push and ECS deploy permissions.
- Better still, use OIDC. Replace long-lived access keys with GitHub’s OIDC provider so there are no static credentials to leak.
- Cache dependencies. Caching Maven packages cut my pipeline time noticeably.
Results
- Deployments went from a manual process to hands-off
- Every release is traceable to a commit
- Bad code is blocked by tests and the quality gate before it reaches production
What’s next
I plan to add Terraform to provision the infrastructure and a blue/green deployment strategy with CodeDeploy.
Over to you
Are you deploying to ECS, EKS or Beanstalk? What’s the one thing that broke your first pipeline? Share it in the comments, because I read and reply to every one.
Loading article