175 lines
5.3 KiB
YAML
175 lines
5.3 KiB
YAML
name: ci
|
|
|
|
on:
|
|
push:
|
|
workflow_dispatch:
|
|
inputs:
|
|
tag:
|
|
description: 'Tag to deploy'
|
|
required: false
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
- name: Set up JDK 17
|
|
uses: actions/setup-java@v3
|
|
with:
|
|
java-version: '17'
|
|
distribution: 'temurin'
|
|
cache: maven
|
|
|
|
- name: Build with Maven
|
|
run: ./mvnw clean package -DskipTests
|
|
|
|
run-tests:
|
|
runs-on: ubuntu-latest
|
|
needs: [ build ]
|
|
|
|
steps:
|
|
|
|
- uses: actions/checkout@v3
|
|
- name: Set up JDK 17
|
|
uses: actions/setup-java@v3
|
|
with:
|
|
java-version: '17'
|
|
distribution: 'temurin'
|
|
cache: maven
|
|
|
|
- name: Running Tests with Maven
|
|
run: ./mvnw test
|
|
|
|
docker:
|
|
needs: [ build, run-tests ]
|
|
if: github.ref_name == 'main' || github.ref_name == 'develop'
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
packages: write # required to push to ghcr.io
|
|
id-token: write # optional for OIDC if you use it
|
|
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
|
|
- name: Set up QEMU
|
|
uses: docker/setup-qemu-action@v3
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Log in to GHCR
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ghcr.io
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Prepare image tags
|
|
run: |
|
|
OWNER=$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')
|
|
REPO=$(echo "${GITHUB_REPOSITORY#*/}" | tr '[:upper:]' '[:lower:]')
|
|
|
|
# Determine tag
|
|
if [ "${GITHUB_REF_NAME}" = "main" ]; then
|
|
TAG="latest"
|
|
else
|
|
TAG="dev"
|
|
fi
|
|
|
|
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
|
|
IMAGE_BASE="ghcr.io/${OWNER}/${REPO}"
|
|
|
|
echo "IMAGE_LATEST=${IMAGE_BASE}:${TAG}" >> $GITHUB_ENV
|
|
echo "IMAGE_SHA=${IMAGE_BASE}:sha-${SHORT_SHA}" >> $GITHUB_ENV
|
|
|
|
- name: Build and push Docker image (with registry cache)
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
push: true
|
|
platforms: linux/amd64,linux/arm64
|
|
tags: |
|
|
${{ env.IMAGE_LATEST }}
|
|
${{ env.IMAGE_SHA }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
deploy:
|
|
needs: [docker]
|
|
runs-on: ubuntu-latest
|
|
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref_name == 'main')
|
|
environment:
|
|
name: ${{ github.ref_name == 'main' && 'production' || 'dev' }}
|
|
url: https://${{ vars.KUBE_DOMAIN }}
|
|
env:
|
|
# Kubernetes Specific
|
|
KUBE_NAMESPACE: ${{ vars.KUBE_NAMESPACE }}
|
|
KUBE_API_DOMAIN: ${{ vars.KUBE_API_DOMAIN }}
|
|
WORKER_NODE_LABEL: ${{ vars.WORKER_NODE_LABEL }}
|
|
# Application Specific
|
|
FRONTEND_PATH: ${{ vars.FRONTEND_PATH }}
|
|
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- uses: azure/setup-kubectl@v4
|
|
|
|
- name: Set Up Kubeconfig
|
|
uses: azure/k8s-set-context@v4
|
|
with:
|
|
kubeconfig: ${{ secrets.PORTFOLIO_KUBECONFIG }}
|
|
|
|
- name: Prepare Image Tag
|
|
run: |
|
|
OWNER=$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')
|
|
REPO=$(echo "${GITHUB_REPOSITORY#*/}" | tr '[:upper:]' '[:lower:]')
|
|
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
|
|
|
|
IMAGE_BASE="ghcr.io/${OWNER}/${REPO}"
|
|
IMAGE_TAG="${{ github.event.inputs.tag || '' }}"
|
|
if [ -z "$IMAGE_TAG" ]; then
|
|
IMAGE_TAG="sha-$SHORT_SHA"
|
|
fi
|
|
|
|
echo "IMAGE_BASE=${IMAGE_BASE}" >> $GITHUB_ENV
|
|
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
|
|
echo "" >> $GITHUB_ENV
|
|
|
|
- name: Import SOPS GPG Key
|
|
run: |
|
|
echo "${{ secrets.PORTFOLIO_GPG_PRIVATE_KEY }}" | gpg --import
|
|
|
|
- name: Install SOPS
|
|
run: |
|
|
curl -L https://github.com/mozilla/sops/releases/download/v3.9.1/sops-v3.9.1.linux.amd64 -o /usr/local/bin/sops
|
|
chmod +x /usr/local/bin/sops
|
|
|
|
- name: Decrypt SOPS Secrets Test
|
|
run: |
|
|
cd .k8s
|
|
sops -d secrets.enc.yml > secrets.yml
|
|
|
|
- name: Apply Kubernetes Manifests - Configuration
|
|
run: cat .k8s/config.template.yml | envsubst | kubectl apply -f -
|
|
|
|
- name: Apply Kubernetes Manifests - Secrets
|
|
run: cat .k8s/secrets.yml | envsubst | kubectl apply -f -
|
|
|
|
- name: Apply Kubernetes Manifests - Postgres Cluster
|
|
run: cat .k8s/postgres-cluster.template.yml | envsubst | kubectl apply -f -
|
|
|
|
- name: Apply Kubernetes Manifests - Redis Cluster
|
|
run: cat .k8s/redis.template.yml | envsubst | kubectl apply -f -
|
|
|
|
- name: Apply Kubernetes Manifests - Deployment
|
|
run: |
|
|
cat .k8s/deployment.template.yml | envsubst | kubectl apply -f -
|
|
cat .k8s/deployment.template.yml | envsubst | kubectl rollout status deployment/frontend-deployment -n ${KUBE_NAMESPACE} --timeout=120s
|
|
|
|
- name: Apply Kubernetes Manifests - Service
|
|
run: cat .k8s/service.template.yml | envsubst | kubectl apply -f -
|
|
|
|
- name: Apply Kubernetes Manifests - Ingress
|
|
run: cat .k8s/ingress.template.yml | envsubst | kubectl apply -f -
|