mirror of
https://github.com/cna-bootcamp/lifesub.git
synced 2026-01-21 19:16:23 +00:00
add github aciton
This commit is contained in:
parent
6529422838
commit
69ca21bfdc
294
.github/workflows/cicd.yaml
vendored
294
.github/workflows/cicd.yaml
vendored
@ -5,238 +5,258 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- cicd
|
- cicd
|
||||||
paths:
|
paths:
|
||||||
- '**'
|
- 'member/**'
|
||||||
- '!.github/**'
|
- 'mysub/**'
|
||||||
- '!**.md'
|
- 'recommend/**'
|
||||||
|
- 'common/**'
|
||||||
|
- 'deployment/**'
|
||||||
|
- '.github/workflows/**'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build and Test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
outputs:
|
|
||||||
image_tag: ${{ steps.set_outputs.outputs.image_tag }}
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
|
||||||
fetch-depth: 0 # SonarQube 분석을 위해 전체 히스토리 가져오기
|
|
||||||
|
|
||||||
- name: Set up JDK 21
|
- name: Set up JDK 21
|
||||||
uses: actions/setup-java@v3
|
uses: actions/setup-java@v3
|
||||||
with:
|
with:
|
||||||
java-version: '21'
|
java-version: '21'
|
||||||
distribution: 'temurin'
|
distribution: 'temurin'
|
||||||
|
cache: gradle
|
||||||
- name: Setup Gradle
|
|
||||||
uses: gradle/gradle-build-action@v2
|
|
||||||
with:
|
|
||||||
gradle-version: '8.5'
|
|
||||||
|
|
||||||
- name: Grant execute permission for gradlew
|
- name: Grant execute permission for gradlew
|
||||||
run: chmod +x ./gradlew
|
run: chmod +x ./gradlew
|
||||||
|
|
||||||
- name: Load environment variables
|
- name: Build with Gradle
|
||||||
|
run: ./gradlew :member:build :mysub-infra:build :recommend:build -x test
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
while IFS= read -r line || [ -n "$line" ]; do
|
./gradlew :member:test :member:jacocoTestReport
|
||||||
# Skip comments and empty lines
|
./gradlew :mysub-infra:test :mysub-infra:jacocoTestReport
|
||||||
if [[ "$line" =~ ^#.*$ ]] || [[ -z "$line" ]]; then
|
./gradlew :recommend:test :recommend:jacocoTestReport
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Export the environment variable
|
- name: SonarQube Analysis - Member
|
||||||
echo "$line" >> $GITHUB_ENV
|
|
||||||
done < deployment/deploy_env_vars
|
|
||||||
|
|
||||||
- name: Build and test
|
|
||||||
run: ./gradlew clean build -x test
|
|
||||||
|
|
||||||
- name: Run tests with JaCoCo
|
|
||||||
run: ./gradlew test jacocoTestReport
|
|
||||||
|
|
||||||
- name: Build and analyze with SonarQube
|
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 필요한 경우 PR 데코레이션을 위해
|
|
||||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||||
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
|
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
|
||||||
run: |
|
run: |
|
||||||
./gradlew sonarqube \
|
./gradlew :member:sonar \
|
||||||
-Dsonar.projectKey=backend-service \
|
-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.sources=src/main/java \
|
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
|
||||||
-Dsonar.tests=src/test/java \
|
|
||||||
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.java
|
|
||||||
|
|
||||||
- name: Upload build artifact
|
- name: SonarQube Analysis - Recommend
|
||||||
|
env:
|
||||||
|
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||||
|
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
|
||||||
|
run: |
|
||||||
|
./gradlew :recommend:sonar \
|
||||||
|
-Dsonar.projectKey=lifesub-recommend \
|
||||||
|
-Dsonar.projectName=lifesub-recommend \
|
||||||
|
-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 \
|
||||||
|
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
|
||||||
|
|
||||||
|
- name: SonarQube Analysis - Mysub
|
||||||
|
env:
|
||||||
|
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||||
|
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
|
||||||
|
run: |
|
||||||
|
./gradlew :mysub-infra:sonar \
|
||||||
|
-Dsonar.projectKey=lifesub-mysub \
|
||||||
|
-Dsonar.projectName=lifesub-mysub \
|
||||||
|
-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 \
|
||||||
|
-Dsonar.exclusions=**/config/**,**/entity/**,**/dto/**,**/*Application.class,**/exception/**
|
||||||
|
|
||||||
|
- name: Upload artifacts
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: build-artifacts
|
name: build-artifacts
|
||||||
path: |
|
path: |
|
||||||
member/build/libs/*.jar
|
member/build/libs
|
||||||
mysub-infra/build/libs/*.jar
|
mysub-infra/build/libs
|
||||||
recommend/build/libs/*.jar
|
recommend/build/libs
|
||||||
|
deployment/
|
||||||
- name: Generate image tag
|
|
||||||
id: set_outputs
|
|
||||||
run: |
|
|
||||||
IMAGE_TAG=$(date '+%Y%m%d%H%M%S')
|
|
||||||
echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT
|
|
||||||
echo "Image tag: ${IMAGE_TAG}"
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
name: Release
|
name: Build and Push Container Images
|
||||||
needs: build
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
needs: build
|
||||||
|
outputs:
|
||||||
|
image_tag: ${{ steps.set-image-tag.outputs.image_tag }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Download build artifacts
|
- name: Download build artifacts
|
||||||
uses: actions/download-artifact@v4
|
uses: actions/download-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: build-artifacts
|
name: build-artifacts
|
||||||
path: artifacts/
|
path: .
|
||||||
|
|
||||||
- name: Load environment variables
|
- name: Read deployment variables
|
||||||
run: |
|
run: |
|
||||||
while IFS= read -r line || [ -n "$line" ]; do
|
source deployment/deploy_env_vars
|
||||||
# Skip comments and empty lines
|
echo "REGISTRY=$registry" >> $GITHUB_ENV
|
||||||
if [[ "$line" =~ ^#.*$ ]] || [[ -z "$line" ]]; then
|
echo "IMAGE_ORG=$image_org" >> $GITHUB_ENV
|
||||||
continue
|
echo "NAMESPACE=$namespace" >> $GITHUB_ENV
|
||||||
fi
|
|
||||||
|
|
||||||
# Export the environment variable
|
|
||||||
echo "$line" >> $GITHUB_ENV
|
|
||||||
done < deployment/deploy_env_vars
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Login to ACR
|
- name: Log in to Azure Container Registry
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
registry: ${{ env.registry }}
|
registry: ${{ env.REGISTRY }}
|
||||||
username: ${{ secrets.ACR_USERNAME }}
|
username: ${{ secrets.ACR_USERNAME }}
|
||||||
password: ${{ secrets.ACR_PASSWORD }}
|
password: ${{ secrets.ACR_PASSWORD }}
|
||||||
|
|
||||||
- name: Build and push Member service image
|
- name: Generate image tag
|
||||||
|
id: set-image-tag
|
||||||
|
run: |
|
||||||
|
IMAGE_TAG=$(date +'%Y%m%d%H%M%S')
|
||||||
|
echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
|
||||||
|
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Build and push Member image
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ env.registry }}/${{ env.image_org }}/member:${{ needs.build.outputs.image_tag }}
|
file: deployment/Dockerfile
|
||||||
build-args: |
|
build-args: |
|
||||||
BUILD_LIB_DIR=artifacts/member/build/libs
|
BUILD_LIB_DIR=member/build/libs
|
||||||
ARTIFACTORY_FILE=member.jar
|
ARTIFACTORY_FILE=member.jar
|
||||||
file: deployment/Dockerfile
|
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_ORG }}/member:${{ env.IMAGE_TAG }}
|
||||||
|
|
||||||
- name: Build and push MySubscription service image
|
- name: Build and push Mysub image
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ env.registry }}/${{ env.image_org }}/mysub:${{ needs.build.outputs.image_tag }}
|
file: deployment/Dockerfile
|
||||||
build-args: |
|
build-args: |
|
||||||
BUILD_LIB_DIR=artifacts/mysub-infra/build/libs
|
BUILD_LIB_DIR=mysub-infra/build/libs
|
||||||
ARTIFACTORY_FILE=mysub.jar
|
ARTIFACTORY_FILE=mysub.jar
|
||||||
file: deployment/Dockerfile
|
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_ORG }}/mysub:${{ env.IMAGE_TAG }}
|
||||||
|
|
||||||
- name: Build and push Recommend service image
|
- name: Build and push Recommend image
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ env.registry }}/${{ env.image_org }}/recommend:${{ needs.build.outputs.image_tag }}
|
|
||||||
build-args: |
|
|
||||||
BUILD_LIB_DIR=artifacts/recommend/build/libs
|
|
||||||
ARTIFACTORY_FILE=recommend.jar
|
|
||||||
file: deployment/Dockerfile
|
file: deployment/Dockerfile
|
||||||
|
build-args: |
|
||||||
|
BUILD_LIB_DIR=recommend/build/libs
|
||||||
|
ARTIFACTORY_FILE=recommend.jar
|
||||||
|
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_ORG }}/recommend:${{ env.IMAGE_TAG }}
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
name: Deploy
|
name: Deploy to Kubernetes
|
||||||
needs: [build, release]
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
needs: release
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Load environment variables
|
- name: Download build artifacts
|
||||||
run: |
|
uses: actions/download-artifact@v4
|
||||||
while IFS= read -r line || [ -n "$line" ]; do
|
with:
|
||||||
# Skip comments and empty lines
|
name: build-artifacts
|
||||||
if [[ "$line" =~ ^#.*$ ]] || [[ -z "$line" ]]; then
|
path: .
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Export the environment variable
|
- name: Setup envsubst
|
||||||
echo "$line" >> $GITHUB_ENV
|
run: sudo apt-get install gettext-base
|
||||||
done < deployment/deploy_env_vars
|
|
||||||
|
- name: Read deployment variables
|
||||||
|
run: |
|
||||||
|
source deployment/deploy_env_vars
|
||||||
|
echo "NAMESPACE=$namespace" >> $GITHUB_ENV
|
||||||
|
echo "REGISTRY=$registry" >> $GITHUB_ENV
|
||||||
|
echo "IMAGE_ORG=$image_org" >> $GITHUB_ENV
|
||||||
|
echo "ALLOWED_ORIGINS=$allowed_origins" >> $GITHUB_ENV
|
||||||
|
echo "JWT_SECRET_KEY=$jwt_secret_key" >> $GITHUB_ENV
|
||||||
|
echo "POSTGRES_USER=$postgres_user" >> $GITHUB_ENV
|
||||||
|
echo "POSTGRES_PASSWORD=$postgres_password" >> $GITHUB_ENV
|
||||||
|
echo "REPLICAS=$replicas" >> $GITHUB_ENV
|
||||||
|
echo "RESOURCES_REQUESTS_CPU=$resources_requests_cpu" >> $GITHUB_ENV
|
||||||
|
echo "RESOURCES_REQUESTS_MEMORY=$resources_requests_memory" >> $GITHUB_ENV
|
||||||
|
echo "RESOURCES_LIMITS_CPU=$resources_limits_cpu" >> $GITHUB_ENV
|
||||||
|
echo "RESOURCES_LIMITS_MEMORY=$resources_limits_memory" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Generate deployment manifest
|
||||||
|
env:
|
||||||
|
IMAGE_TAG: ${{ needs.release.outputs.image_tag }}
|
||||||
|
run: |
|
||||||
|
export namespace=${{ env.NAMESPACE }}
|
||||||
|
export allowed_origins=${{ env.ALLOWED_ORIGINS }}
|
||||||
|
export jwt_secret_key=${{ env.JWT_SECRET_KEY }}
|
||||||
|
export postgres_user=${{ env.POSTGRES_USER }}
|
||||||
|
export postgres_password=${{ env.POSTGRES_PASSWORD }}
|
||||||
|
export replicas=${{ env.REPLICAS }}
|
||||||
|
export resources_requests_cpu=${{ env.RESOURCES_REQUESTS_CPU }}
|
||||||
|
export resources_requests_memory=${{ env.RESOURCES_REQUESTS_MEMORY }}
|
||||||
|
export resources_limits_cpu=${{ env.RESOURCES_LIMITS_CPU }}
|
||||||
|
export resources_limits_memory=${{ env.RESOURCES_LIMITS_MEMORY }}
|
||||||
|
|
||||||
|
# Set image paths with the released tag
|
||||||
|
export member_image_path=${{ env.REGISTRY }}/${{ env.IMAGE_ORG }}/member:${{ env.IMAGE_TAG }}
|
||||||
|
export mysub_image_path=${{ env.REGISTRY }}/${{ env.IMAGE_ORG }}/mysub:${{ env.IMAGE_TAG }}
|
||||||
|
export recommend_image_path=${{ env.REGISTRY }}/${{ env.IMAGE_ORG }}/recommend:${{ env.IMAGE_TAG }}
|
||||||
|
|
||||||
|
# Generate manifest
|
||||||
|
envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml
|
||||||
|
|
||||||
|
# Print manifest for debugging
|
||||||
|
echo "Generated Kubernetes manifest:"
|
||||||
|
cat deployment/deploy.yaml
|
||||||
|
|
||||||
- name: Set up kubectl
|
- name: Set up kubectl
|
||||||
uses: azure/setup-kubectl@v3
|
uses: azure/setup-kubectl@v3
|
||||||
|
|
||||||
- name: Set AKS context
|
- name: Set up AKS context
|
||||||
uses: azure/aks-set-context@v3
|
uses: azure/aks-set-context@v3
|
||||||
with:
|
with:
|
||||||
resource-group: ictcoe-edu
|
resource-group: ictcoe-edu
|
||||||
cluster-name: ${{ env.teamid }}-aks
|
cluster-name: ${{ env.TEAMID }}-aks
|
||||||
admin: 'false'
|
admin: false
|
||||||
use-kubelogin: 'true'
|
use-kubelogin: true
|
||||||
env:
|
env:
|
||||||
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
|
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
|
||||||
|
|
||||||
- name: Create namespace if not exists
|
- name: Create namespace if not exists
|
||||||
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: Install envsubst
|
- name: Deploy to Kubernetes
|
||||||
run: |
|
|
||||||
sudo apt-get update
|
|
||||||
sudo apt-get install -y gettext-base
|
|
||||||
|
|
||||||
- name: Generate manifest
|
|
||||||
env:
|
|
||||||
IMAGE_TAG: ${{ needs.build.outputs.image_tag }}
|
|
||||||
run: |
|
|
||||||
# Export variables for envsubst
|
|
||||||
export namespace=${{ env.namespace }}
|
|
||||||
export allowed_origins=${{ env.allowed_origins }}
|
|
||||||
export jwt_secret_key=${{ env.jwt_secret_key }}
|
|
||||||
export postgres_user=${{ env.postgres_user }}
|
|
||||||
export postgres_password=${{ env.postgres_password }}
|
|
||||||
export replicas=${{ env.replicas }}
|
|
||||||
export resources_requests_cpu=${{ env.resources_requests_cpu }}
|
|
||||||
export resources_requests_memory=${{ env.resources_requests_memory }}
|
|
||||||
export resources_limits_cpu=${{ env.resources_limits_cpu }}
|
|
||||||
export resources_limits_memory=${{ env.resources_limits_memory }}
|
|
||||||
|
|
||||||
# 이미지 경로 환경변수 설정
|
|
||||||
export member_image_path=${{ env.registry }}/${{ env.image_org }}/member:${IMAGE_TAG}
|
|
||||||
export mysub_image_path=${{ env.registry }}/${{ env.image_org }}/mysub:${IMAGE_TAG}
|
|
||||||
export recommend_image_path=${{ env.registry }}/${{ env.image_org }}/recommend:${IMAGE_TAG}
|
|
||||||
|
|
||||||
# manifest 생성
|
|
||||||
envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml
|
|
||||||
|
|
||||||
# For debugging
|
|
||||||
echo "Generated manifest:"
|
|
||||||
cat deployment/deploy.yaml
|
|
||||||
|
|
||||||
- name: Deploy to AKS
|
|
||||||
run: |
|
run: |
|
||||||
kubectl apply -f deployment/deploy.yaml
|
kubectl apply -f deployment/deploy.yaml
|
||||||
|
|
||||||
echo "Waiting for deployments to be ready..."
|
echo "Waiting for deployments to be ready..."
|
||||||
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
|
||||||
|
|
||||||
- name: Get service details
|
- name: Verify deployment
|
||||||
run: |
|
run: |
|
||||||
echo "Ingress details:"
|
echo "Getting services:"
|
||||||
kubectl -n ${{ env.namespace }} get ingress -o wide
|
kubectl get svc -n ${{ env.NAMESPACE }}
|
||||||
|
|
||||||
|
echo "Getting ingress:"
|
||||||
|
kubectl get ingress -n ${{ env.NAMESPACE }}
|
||||||
Loading…
x
Reference in New Issue
Block a user