.
This commit is contained in:
+61
-88
@@ -1,97 +1,70 @@
|
|||||||
# CI: Maven build + unit tests on every push/PR to main or master.
|
# CI: Maven build + unit tests on merge requests to main/master and on push to those branches.
|
||||||
# CD: Build Docker image and push to GitHub Container Registry (ghcr.io) on push to main/master.
|
# CD: Build Docker image and push to GitHub Container Registry (ghcr.io) on push to main/master only.
|
||||||
# Optional: SSH deploy when repository variable ENABLE_SSH_DEPLOY=true and deploy secrets are set.
|
# Optional: SSH deploy when CI/CD variable ENABLE_SSH_DEPLOY=true and deploy secrets are set.
|
||||||
#
|
#
|
||||||
# Repository variables (Settings → Secrets and variables → Actions → Variables):
|
# CI/CD variables (Settings → CI/CD → Variables):
|
||||||
|
# GHCR_USERNAME, GHCR_TOKEN — for docker push (GitHub user + PAT with write:packages)
|
||||||
# ENABLE_SSH_DEPLOY = true (optional; omit or false to skip deploy job)
|
# ENABLE_SSH_DEPLOY = true (optional; omit or false to skip deploy job)
|
||||||
#
|
# DEPLOY_HOST, DEPLOY_USER, DEPLOY_SSH_KEY, DEPLOY_SCRIPT — optional deploy
|
||||||
# Optional deploy secrets (Settings → Secrets and variables → Actions → Secrets):
|
|
||||||
# DEPLOY_HOST, DEPLOY_USER, DEPLOY_SSH_KEY, DEPLOY_SCRIPT
|
|
||||||
# DEPLOY_SCRIPT: multiline shell to run on the server (e.g. cd /opt/app && docker compose pull && docker compose up -d)
|
|
||||||
#
|
|
||||||
# Pull the image as: ghcr.io/<owner>/<repo>:latest or :<git-sha>
|
|
||||||
|
|
||||||
name: CI/CD
|
variables:
|
||||||
|
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
|
||||||
|
|
||||||
on:
|
workflow:
|
||||||
push:
|
rules:
|
||||||
branches: [main, master]
|
# Merge requests whose target branch is main or master
|
||||||
pull_request:
|
- if: $CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master")
|
||||||
branches: [main, master]
|
# Direct pushes to main or master
|
||||||
workflow_dispatch:
|
- if: $CI_PIPELINE_SOURCE == "push" && ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "master")
|
||||||
|
# Manual pipeline (Run pipeline in UI)
|
||||||
|
- if: $CI_PIPELINE_SOURCE == "web"
|
||||||
|
|
||||||
permissions:
|
cache:
|
||||||
contents: read
|
key: ${CI_COMMIT_REF_SLUG}
|
||||||
packages: write
|
paths:
|
||||||
|
- .m2/repository
|
||||||
|
|
||||||
env:
|
stages:
|
||||||
JAVA_VERSION: "21"
|
- test
|
||||||
|
- publish
|
||||||
|
- deploy
|
||||||
|
|
||||||
jobs:
|
build-and-test:
|
||||||
build-and-test:
|
stage: test
|
||||||
runs-on: ubuntu-latest
|
image: maven:3.9.9-eclipse-temurin-21
|
||||||
steps:
|
script:
|
||||||
- name: Checkout
|
- mvn -B -ntp verify
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up JDK
|
docker-build-push:
|
||||||
uses: actions/setup-java@v4
|
stage: publish
|
||||||
with:
|
needs: [build-and-test]
|
||||||
distribution: temurin
|
rules:
|
||||||
java-version: ${{ env.JAVA_VERSION }}
|
- if: $CI_PIPELINE_SOURCE == "push" && ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "master")
|
||||||
cache: maven
|
image: docker:24-cli
|
||||||
|
services:
|
||||||
|
- docker:24-dind
|
||||||
|
variables:
|
||||||
|
DOCKER_HOST: tcp://docker:2375
|
||||||
|
DOCKER_TLS_CERTDIR: ""
|
||||||
|
before_script:
|
||||||
|
- until docker info; do sleep 1; done
|
||||||
|
- echo "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USERNAME" --password-stdin
|
||||||
|
script:
|
||||||
|
- IMG=$(echo "$CI_PROJECT_PATH" | tr '[:upper:]' '[:lower:]')
|
||||||
|
- docker build -t "ghcr.io/${IMG}:latest" -t "ghcr.io/${IMG}:${CI_COMMIT_SHA}" .
|
||||||
|
- docker push "ghcr.io/${IMG}:latest"
|
||||||
|
- docker push "ghcr.io/${IMG}:${CI_COMMIT_SHA}"
|
||||||
|
|
||||||
# Unit tests only; @EnabledIfEnvironmentVariable(RUN_INTEGRATION_TESTS) tests stay skipped unless you add a separate job with secrets.
|
deploy:
|
||||||
- name: Maven verify
|
stage: deploy
|
||||||
run: mvn -B -ntp verify
|
needs: [docker-build-push]
|
||||||
|
rules:
|
||||||
docker-build-push:
|
- if: $CI_PIPELINE_SOURCE == "push" && ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "master") && $ENABLE_SSH_DEPLOY == "true"
|
||||||
needs: build-and-test
|
image: alpine:3.20
|
||||||
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
|
before_script:
|
||||||
runs-on: ubuntu-latest
|
- apk add --no-cache openssh-client
|
||||||
steps:
|
- mkdir -p ~/.ssh
|
||||||
- name: Checkout
|
- echo "$DEPLOY_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
|
||||||
uses: actions/checkout@v4
|
- chmod 600 ~/.ssh/id_rsa
|
||||||
|
script:
|
||||||
- name: Set up Docker Buildx
|
- ssh -o StrictHostKeyChecking=no "$DEPLOY_USER@$DEPLOY_HOST" "$DEPLOY_SCRIPT"
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Log in to GitHub Container Registry
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Image name (lowercase for GHCR)
|
|
||||||
id: image
|
|
||||||
run: |
|
|
||||||
IMG=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
|
|
||||||
echo "name=ghcr.io/${IMG}" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
- name: Build and push
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: |
|
|
||||||
${{ steps.image.outputs.name }}:latest
|
|
||||||
${{ steps.image.outputs.name }}:${{ github.sha }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
needs: docker-build-push
|
|
||||||
# Set ENABLE_SSH_DEPLOY=true in repo Variables to run this job; otherwise skipped.
|
|
||||||
if: vars.ENABLE_SSH_DEPLOY == 'true'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
# Optional: uncomment for manual approval gates (create "production" in Environments first).
|
|
||||||
# environment: production
|
|
||||||
steps:
|
|
||||||
- name: Deploy over SSH
|
|
||||||
uses: appleboy/ssh-action@v1.2.2
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.DEPLOY_HOST }}
|
|
||||||
username: ${{ secrets.DEPLOY_USER }}
|
|
||||||
key: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
||||||
script: ${{ secrets.DEPLOY_SCRIPT }}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user