Skip to content

Create build.yml

Create build.yml #1

Workflow file for this run

name: CI Pipeline - Build, Test, Analyze and Deploy
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build, Test, and Analyze
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
microservice: ['discovery-service', 'hospital-service', 'emergency-service']
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 21
distribution: 'zulu'
- name: Cache SonarQube packages
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar-${{ matrix.microservice }}
restore-keys: ${{ runner.os }}-sonar-${{ matrix.microservice }}
- name: Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles(format('backend/{0}/pom.xml', matrix.microservice)) }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
# Run from repo root to properly reference the module path
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
-Dsonar.projectKey=swyth-dev_realtime-emergency-response-system-${{ matrix.microservice }} \
-f backend/${{ matrix.microservice }} \
-am
- name: Save build artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.microservice }}-build
path: backend/${{ matrix.microservice }}/target/*.jar
retention-days: 1
docker-build-push:
name: Build and Push Docker Images
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
strategy:
fail-fast: false
matrix:
microservice: ['discovery-service', 'hospital-service', 'emergency-service']
steps:
- uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: ${{ matrix.microservice }}-build
path: backend/${{ matrix.microservice }}/target/
- 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 for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.microservice }}
tags: |
type=ref,event=branch
type=sha,format=short
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: backend/${{ matrix.microservice }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}