Skip to main content

TP12.2 - CI/CD Pipeline and Deployment

Objectives of the Lab​

In this lab, you will:

  1. ✅ Create a complete GitHub Actions workflow
  2. ✅ Automate tests, build, and Docker publishing
  3. ✅ Deploy on Railway (or alternative)
  4. ✅ 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:

  1. Automated tests
  2. Docker image build
  3. Publishing on Docker Hub
  4. 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​

  1. Go to railway.app
  2. "New Project" → "Empty Project"
  3. "Add Service" → "GitHub Repo" → Select your repo
  4. "Add Database" → "PostgreSQL"

Obtain the Railway Token​

  1. Account Settings → Tokens
  2. "Create Token"
  3. 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​

  1. Go to Actions tab
  2. Verify the workflow is running
  3. Each job should pass ✅

3. Check Docker Hub​

  1. Go to hub.docker.com
  2. Verify the image is published
  3. 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

![CI Status](https://github.com/YOUR_USERNAME/todo-app/workflows/CI%20-%20Tests/badge.svg)
![CD Status](https://github.com/YOUR_USERNAME/todo-app/workflows/CD%20-%20Build%20and%20Deploy/badge.svg)

## 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/health returns 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

➡️ Back to Module 12


⬅️ Back to Course Plan