TP12.2 - CI/CD Pipeline and Deployment
Objectives of the Lab
In this lab, you will:
- ✅ Create a complete GitHub Actions workflow
- ✅ Automate tests, build, and Docker publishing
- ✅ Deploy on Railway (or alternative)
- ✅ Configure automatic deployment
Estimated duration: 1h30
Prerequisites: TP12.1 completed, GitHub account, Railway account
Context
You are automating the deployment of the Todo application from TP12.1. Each push on main triggers:
- Automated tests
- Docker image build
- Publishing on Docker Hub
- Deployment on Railway
Part 1: Preparation
File Structure
.github/
└── workflows/
├── ci.yml # Tests on each PR
├── cd.yml # Build + Deploy on main
└── docker-publish.yml # Docker publishing
Create GitHub Secrets
Go to: Repository → Settings → Secrets and variables → Actions
# Docker Hub
DOCKERHUB_USERNAME=your-username
DOCKERHUB_TOKEN=your-token
# Railway
RAILWAY_TOKEN=your-token
# (Optional) Heroku
HEROKU_API_KEY=your-api-key
HEROKU_APP_NAME=your-app
HEROKU_EMAIL=your-email
Part 2: CI Workflow (Tests)
.github/workflows/ci.yml
name: CI - Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_DB: testdb
POSTGRES_USER: test
POSTGRES_PASSWORD: test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
- name: Run Unit Tests
run: mvn test -Dspring.profiles.active=test
- name: Run Integration Tests
run: mvn verify -Dspring.profiles.active=test
env:
SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/testdb
SPRING_DATASOURCE_USERNAME: test
SPRING_DATASOURCE_PASSWORD: test
- name: Upload Test Results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: target/surefire-reports/
- name: Generate Coverage Report
run: mvn jacoco:report
- name: Upload Coverage
uses: codecov/codecov-action@v3
with:
files: target/site/jacoco/jacoco.xml
fail_ci_if_error: false
Part 3: CD Workflow (Build & Deploy)
.github/workflows/cd.yml
name: CD - Build and Deploy
on:
push:
branches: [main]
release:
types: [published]
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/todo-app
jobs:
# ===== JOB 1: Test =====
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
- name: Run Tests
run: mvn test
# ===== JOB 2: Build Docker =====
build:
name: Build and Push Docker
runs-on: ubuntu-latest
needs: test
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
version: ${{ steps.meta.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,prefix=
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Print image info
run: |
echo "Image pushed: ${{ env.IMAGE_NAME }}"
echo "Tags: ${{ steps.meta.outputs.tags }}"
# ===== JOB 3: Deploy to Railway =====
deploy:
name: Deploy to Railway
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main'
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Railway CLI
run: npm i -g @railway/cli
- name: Deploy to Railway
run: railway up --detach
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
- name: Print deployment info
run: |
echo "✅ Deployed to Railway!"
echo "Image: ${{ needs.build.outputs.image-tag }}"
Part 4: Railway Configuration
Create a Railway Project
- Go to railway.app
- "New Project" → "Empty Project"
- "Add Service" → "GitHub Repo" → Select your repo
- "Add Database" → "PostgreSQL"
Obtain the Railway Token
- Account Settings → Tokens
- "Create Token"
- Copy the token into GitHub Secrets
Railway Environment Variables
In the Railway dashboard, configure:
SPRING_PROFILES_ACTIVE=prod
DATABASE_URL=${{Postgres.DATABASE_URL}}
# Railway automatically injects DATABASE_URL from PostgreSQL
railway.json (optional)
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "DOCKERFILE",
"dockerfilePath": "Dockerfile"
},
"deploy": {
"numReplicas": 1,
"healthcheckPath": "/actuator/health",
"healthcheckTimeout": 30,
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 3
}
}
Part 5: Heroku Alternative
.github/workflows/deploy-heroku.yml
name: Deploy to Heroku
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to Heroku
uses: akhileshns/heroku-deploy@v3.12.14
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
heroku_email: ${{ secrets.HEROKU_EMAIL }}
usedocker: true
Necessary Heroku Files
heroku.yml:
build:
docker:
web: Dockerfile
run:
web: java -Dserver.port=$PORT -jar app.jar
Part 6: Test the Pipeline
1. Initial Push
git add .
git commit -m "Add CI/CD pipeline"
git push origin main
2. Check GitHub Actions
- Go to Actions tab
- Verify the workflow is running
- Each job should pass ✅
3. Check Docker Hub
- Go to hub.docker.com
- Verify the image is published
- Check the tags
4. Check the Deployment
# Railway
curl https://your-app.railway.app/actuator/health
# Or Heroku
curl https://your-app.herokuapp.com/actuator/health
Part 7: Workflow with Notifications
Slack/Discord Notifications
notify:
name: Notify
runs-on: ubuntu-latest
needs: [deploy]
if: always()
steps:
- name: Send Slack notification
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
if: always()
Part 8: Status Badge
Add to README.md
# Todo App


## Description
Task management application with Spring Boot.
## Links
- Production: https://your-app.railway.app
- API Docs: https://your-app.railway.app/swagger-ui.html
Final Verification
Checklist
- Push triggers the CI workflow
- Tests pass in GitHub Actions
- Docker image published on Docker Hub
- Application deployed on Railway
-
/actuator/healthreturns UP - API works in production
URLs to Verify
# Health check
curl https://your-app.railway.app/actuator/health
# API
curl https://your-app.railway.app/api/todos
# Create a todo in prod
curl -X POST https://your-app.railway.app/api/todos \
-H "Content-Type: application/json" \
-d '{"title": "Test in production!"}'
🎯 Module Completed!
Congratulations! You have completed Module 12 - Deployment and Production.
Summary:
- Chapter 1: Preparation for production
- Chapter 2: Docker and containerization
- Chapter 3: CI/CD with GitHub Actions
- Chapter 4: Cloud deployment
- TP12.1: Complete Dockerization
- TP12.2: CI/CD Pipeline
Skills acquired:
- ✅ Spring Boot profiles (dev, prod, docker)
- ✅ Optimized multi-stage Dockerfile
- ✅ Docker Compose with PostgreSQL
- ✅ GitHub Actions workflows
- ✅ Automatic deployment on Railway
- ✅ Monitoring and health checks