add github aciton

This commit is contained in:
hiondal 2025-03-03 23:12:39 +09:00
parent 88ec08f7c1
commit 9c55bc6905

View File

@ -1,21 +1,15 @@
name: Backend CI/CD name: Backend Services CI/CD
on: on:
push: push:
branches: branches: [ cicd ]
- cicd
paths: paths:
- '**/*.java' - 'member/**'
- '**/*.gradle' - 'mysub/**'
- '**/build.gradle' - 'recommend/**'
- '**/settings.gradle' - 'common/**'
- '**/gradle.properties'
- 'deployment/**' - 'deployment/**'
- '.github/workflows/cicd.yaml' - '.github/workflows/**'
env:
REGISTRY: ghcr.io
RESOURCE_GROUP: ictcoe-edu
jobs: jobs:
build: build:
@ -33,87 +27,88 @@ jobs:
with: with:
java-version: '21' java-version: '21'
distribution: 'temurin' distribution: 'temurin'
cache: gradle cache: 'gradle'
- name: Load environment variables - name: Load environment variables
id: env_vars id: env_vars
run: | run: |
# Load environment variables from file # Read environment variables from file
while IFS= read -r line; do while IFS= read -r line || [[ -n "$line" ]]; do
# Skip comments and empty lines # Skip comments and empty lines
[[ "$line" =~ ^#.*$ ]] && continue [[ "$line" =~ ^#.*$ ]] && continue
[[ -z "$line" ]] && continue [[ -z "$line" ]] && continue
# Extract key and value # Extract key-value pairs
key=$(echo "$line" | cut -d= -f1) key=$(echo "$line" | cut -d '=' -f1)
value=$(echo "$line" | cut -d= -f2-) value=$(echo "$line" | cut -d '=' -f2-)
# Set GitHub environment variables
echo "$key=$value" >> $GITHUB_ENV echo "$key=$value" >> $GITHUB_ENV
done < deployment/deploy_env_vars done < deployment/deploy_env_vars
echo "teamid=$teamid" >> $GITHUB_OUTPUT
- name: Grant execute permission for gradlew - name: Grant execute permission for gradlew
run: chmod +x ./gradlew run: chmod +x gradlew
- name: Build with Gradle - name: Build with Gradle
run: | run: |
./gradlew :member:build :mysub-infra:build :recommend:build -x test ./gradlew :member:build :mysub-infra:build :recommend:build -x test
- name: Run tests - name: Test with Gradle
run: | run: |
./gradlew :member:test :mysub-infra:test :recommend:test ./gradlew :member:test :member:jacocoTestReport
./gradlew :mysub-infra:test :mysub-infra:jacocoTestReport
./gradlew :recommend:test :recommend:jacocoTestReport
- name: Run SonarQube Analysis - name: SonarQube Analysis
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: | run: |
./gradlew :member:jacocoTestReport :member:sonar \ ./gradlew :member:sonar \
-Dsonar.projectKey=lifesub-member \ -Dsonar.projectKey=lifesub-member \
-Dsonar.host.url=${{ secrets.SONAR_HOST_URL }} \ -Dsonar.projectName=lifesub-member \
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \ -Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.token=$SONAR_TOKEN \
-Dsonar.java.binaries=build/classes/java/main \ -Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml \ -Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
./gradlew :recommend:jacocoTestReport :recommend:sonar \ ./gradlew :mysub-infra:sonar \
-Dsonar.projectKey=lifesub-recommend \
-Dsonar.host.url=${{ secrets.SONAR_HOST_URL }} \
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \
-Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml \
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
./gradlew :mysub-infra:jacocoTestReport :mysub-infra:sonar \
-Dsonar.projectKey=lifesub-mysub \ -Dsonar.projectKey=lifesub-mysub \
-Dsonar.host.url=${{ secrets.SONAR_HOST_URL }} \ -Dsonar.projectName=lifesub-mysub \
-Dsonar.login=${{ secrets.SONAR_TOKEN }} \ -Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.token=$SONAR_TOKEN \
-Dsonar.java.binaries=build/classes/java/main \ -Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml \ -Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
- name: Generate timestamp for image tag ./gradlew :recommend:sonar \
id: set_outputs -Dsonar.projectKey=lifesub-recommend \
run: | -Dsonar.projectName=lifesub-recommend \
echo "image_tag=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT -Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.token=$SONAR_TOKEN \
-Dsonar.java.binaries=build/classes/java/main \
-Dsonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml
- name: Upload build artifacts - name: Upload build artifacts
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: backend-builds name: app-builds
path: | path: |
member/build/libs/ member/build/libs/*.jar
mysub-infra/build/libs/ mysub-infra/build/libs/*.jar
recommend/build/libs/ recommend/build/libs/*.jar
deployment/
retention-days: 1 - name: Set outputs
id: set_outputs
run: |
# Generate timestamp for image tag
IMAGE_TAG=$(date +%Y%m%d%H%M%S)
echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
release: release:
name: Build and Push Docker Images name: Build and Push Docker Images
needs: build needs: build
runs-on: ubuntu-latest runs-on: ubuntu-latest
outputs:
member_image: ${{ steps.push_images.outputs.member_image }}
mysub_image: ${{ steps.push_images.outputs.mysub_image }}
recommend_image: ${{ steps.push_images.outputs.recommend_image }}
steps: steps:
- name: Check out code - name: Check out code
@ -122,72 +117,69 @@ jobs:
- name: Download build artifacts - name: Download build artifacts
uses: actions/download-artifact@v4 uses: actions/download-artifact@v4
with: with:
name: backend-builds name: app-builds
- name: Load environment variables - name: Load environment variables
run: | run: |
# Load environment variables from file # Read environment variables from file
while IFS= read -r line; do while IFS= read -r line || [[ -n "$line" ]]; do
# Skip comments and empty lines # Skip comments and empty lines
[[ "$line" =~ ^#.*$ ]] && continue [[ "$line" =~ ^#.*$ ]] && continue
[[ -z "$line" ]] && continue [[ -z "$line" ]] && continue
# Extract key and value # Extract key-value pairs
key=$(echo "$line" | cut -d= -f1) key=$(echo "$line" | cut -d '=' -f1)
value=$(echo "$line" | cut -d= -f2-) value=$(echo "$line" | cut -d '=' -f2-)
# Set GitHub environment variables
echo "$key=$value" >> $GITHUB_ENV echo "$key=$value" >> $GITHUB_ENV
done < deployment/deploy_env_vars done < deployment/deploy_env_vars
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Build and push Docker images - name: Login to Azure Container Registry
id: push_images uses: docker/login-action@v3
run: | with:
IMAGE_TAG=${{ needs.build.outputs.image_tag }} registry: ${{ env.registry }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
# Member service - name: Build and push Member service image
docker build \ uses: docker/build-push-action@v5
--build-arg BUILD_LIB_DIR="member/build/libs" \ with:
--build-arg ARTIFACTORY_FILE="member.jar" \ context: .
-f deployment/Dockerfile \ file: deployment/Dockerfile
-t ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/member:${IMAGE_TAG} . push: true
tags: ${{ env.registry }}/${{ env.image_org }}/member:${{ needs.build.outputs.image_tag }}
build-args: |
BUILD_LIB_DIR=member/build/libs
ARTIFACTORY_FILE=member.jar
docker push ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/member:${IMAGE_TAG} - name: Build and push MySub service image
uses: docker/build-push-action@v5
with:
context: .
file: deployment/Dockerfile
push: true
tags: ${{ env.registry }}/${{ env.image_org }}/mysub:${{ needs.build.outputs.image_tag }}
build-args: |
BUILD_LIB_DIR=mysub-infra/build/libs
ARTIFACTORY_FILE=mysub.jar
# Mysub service - name: Build and push Recommend service image
docker build \ uses: docker/build-push-action@v5
--build-arg BUILD_LIB_DIR="mysub-infra/build/libs" \ with:
--build-arg ARTIFACTORY_FILE="mysub.jar" \ context: .
-f deployment/Dockerfile \ file: deployment/Dockerfile
-t ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/mysub:${IMAGE_TAG} . push: true
tags: ${{ env.registry }}/${{ env.image_org }}/recommend:${{ needs.build.outputs.image_tag }}
docker push ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/mysub:${IMAGE_TAG} build-args: |
BUILD_LIB_DIR=recommend/build/libs
# Recommend service ARTIFACTORY_FILE=recommend.jar
docker build \
--build-arg BUILD_LIB_DIR="recommend/build/libs" \
--build-arg ARTIFACTORY_FILE="recommend.jar" \
-f deployment/Dockerfile \
-t ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/recommend:${IMAGE_TAG} .
docker push ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/recommend:${IMAGE_TAG}
# Set outputs for next job
echo "member_image=${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/member:${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "mysub_image=${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/mysub:${IMAGE_TAG}" >> $GITHUB_OUTPUT
echo "recommend_image=${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.image_org }}/recommend:${IMAGE_TAG}" >> $GITHUB_OUTPUT
deploy: deploy:
name: Deploy to AKS name: Deploy to Kubernetes
needs: [build, release] needs: [build, release]
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -195,30 +187,28 @@ jobs:
- name: Check out code - name: Check out code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: backend-builds
- name: Load environment variables - name: Load environment variables
run: | run: |
# Load environment variables from file # Read environment variables from file
while IFS= read -r line; do while IFS= read -r line || [[ -n "$line" ]]; do
# Skip comments and empty lines # Skip comments and empty lines
[[ "$line" =~ ^#.*$ ]] && continue [[ "$line" =~ ^#.*$ ]] && continue
[[ -z "$line" ]] && continue [[ -z "$line" ]] && continue
# Extract key and value # Extract key-value pairs
key=$(echo "$line" | cut -d= -f1) key=$(echo "$line" | cut -d '=' -f1)
value=$(echo "$line" | cut -d= -f2-) value=$(echo "$line" | cut -d '=' -f2-)
# Set GitHub environment variables
echo "$key=$value" >> $GITHUB_ENV echo "$key=$value" >> $GITHUB_ENV
done < deployment/deploy_env_vars done < deployment/deploy_env_vars
- name: Install envsubst - name: Set image tag environment variable
run: | run: |
sudo apt-get update echo "IMAGE_TAG=${{ needs.build.outputs.image_tag }}" >> $GITHUB_ENV
sudo apt-get install -y gettext-base
- name: Install Azure CLI
uses: azure/setup-azure-cli@v1
- name: Azure Login - name: Azure Login
uses: azure/login@v1 uses: azure/login@v1
@ -230,15 +220,15 @@ jobs:
- name: Get AKS Credentials - name: Get AKS Credentials
run: | run: |
az aks get-credentials --resource-group ${{ env.RESOURCE_GROUP }} --name ${{ env.teamid }}-aks --overwrite-existing az aks get-credentials --resource-group ictcoe-edu --name ${{ env.teamid }}-aks --overwrite-existing
- name: Create namespace if not exists - name: Create namespace
run: | run: |
kubectl create namespace ${{ env.namespace }} --dry-run=client -o yaml | kubectl apply -f - kubectl create namespace ${{ env.namespace }} --dry-run=client -o yaml | kubectl apply -f -
- name: Generate Kubernetes manifests - name: Generate Kubernetes manifest
run: | run: |
# Set environment variables for the template # Set environment variables for the deployment template
export namespace=${{ env.namespace }} export namespace=${{ env.namespace }}
export allowed_origins=${{ env.allowed_origins }} export allowed_origins=${{ env.allowed_origins }}
export jwt_secret_key=${{ env.jwt_secret_key }} export jwt_secret_key=${{ env.jwt_secret_key }}
@ -250,29 +240,28 @@ jobs:
export resources_limits_cpu=${{ env.resources_limits_cpu }} export resources_limits_cpu=${{ env.resources_limits_cpu }}
export resources_limits_memory=${{ env.resources_limits_memory }} export resources_limits_memory=${{ env.resources_limits_memory }}
# Set image paths from previous job outputs # Set image paths with the dynamic tag
export member_image_path="${{ needs.release.outputs.member_image }}" export member_image_path=${{ env.registry }}/${{ env.image_org }}/member:${{ env.IMAGE_TAG }}
export mysub_image_path="${{ needs.release.outputs.mysub_image }}" export mysub_image_path=${{ env.registry }}/${{ env.image_org }}/mysub:${{ env.IMAGE_TAG }}
export recommend_image_path="${{ needs.release.outputs.recommend_image }}" export recommend_image_path=${{ env.registry }}/${{ env.image_org }}/recommend:${{ env.IMAGE_TAG }}
# Generate manifest from template # Generate the manifest file using envsubst
envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml
# Debug: Print the generated manifest # Print manifest for debugging
echo "===== Generated Kubernetes Manifest =====" echo "Generated Kubernetes manifest:"
cat deployment/deploy.yaml cat deployment/deploy.yaml
echo "========================================="
- name: Apply Kubernetes manifests - name: Apply Kubernetes manifest
run: | run: |
kubectl apply -f deployment/deploy.yaml kubectl apply -f deployment/deploy.yaml
echo "Waiting for deployments to be ready..." - name: Wait for deployments to be ready
run: |
kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/member --timeout=300s kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/member --timeout=300s
kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/mysub --timeout=300s kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/mysub --timeout=300s
kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/recommend --timeout=300s kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/recommend --timeout=300s
echo "Deployment completed successfully." - name: Get service information
run: |
# Get service IP for ingress
echo "Ingress IP: $(kubectl -n ${{ env.namespace }} get ingress lifesub -o jsonpath='{.status.loadBalancer.ingress[0].ip}')" echo "Ingress IP: $(kubectl -n ${{ env.namespace }} get ingress lifesub -o jsonpath='{.status.loadBalancer.ingress[0].ip}')"