Add GitHub Action

This commit is contained in:
hiondal 2025-03-03 22:41:06 +09:00
parent b72d92d185
commit b99eb5c3a1

View File

@ -1,211 +1,191 @@
name: Frontend CI/CD Pipeline name: Frontend CI/CD Pipeline
on: on:
push: push:
branches: branches: [main]
- main
paths: paths:
- '**' - '**'
- '!.github/**'
- '!**.md' - '!**.md'
- '!.github/**'
- '.github/workflows/cicd.yaml'
jobs: jobs:
build: build:
name: Build name: Build
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
- name: Set up Node.js - name: Set up Node.js
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: '20' node-version: '20'
cache: 'npm' cache: 'npm'
- name: Install dependencies - name: Install dependencies
run: npm ci run: npm ci
- name: Run tests - name: Run tests
run: npm test -- --coverage --passWithNoTests run: npm test -- --coverage --passWithNoTests
- name: SonarQube Scan - name: SonarQube Scan
uses: SonarSource/sonarqube-scan-action@master uses: SonarSource/sonarqube-scan-action@v2
env: env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
with: with:
args: > args: >
-Dsonar.projectKey=lifesub-web -Dsonar.projectKey=lifesub-web
-Dsonar.sources=src -Dsonar.sources=src
-Dsonar.tests=src -Dsonar.tests=src
-Dsonar.test.inclusions=src/**/*.test.js,src/**/*.test.jsx -Dsonar.test.inclusions=src/**/*.test.js,src/**/*.test.jsx
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
- name: SonarQube Quality Gate check - name: SonarQube Quality Gate check
uses: SonarSource/sonarqube-quality-gate-action@master uses: SonarSource/sonarqube-quality-gate-action@v1
timeout-minutes: 5 timeout-minutes: 5
env: env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- name: Build application
run: npm run build - name: Build project
run: npm run build
- name: Upload build artifact
uses: actions/upload-artifact@v4 - name: Upload build artifact
with: uses: actions/upload-artifact@v4
name: build with:
path: build/ name: build
path: build/
- name: Load environment variables retention-days: 1
run: |
while IFS= read -r line || [ -n "$line" ]; do
# Skip comments and empty lines
if [[ "$line" =~ ^#.*$ ]] || [[ -z "$line" ]]; then
continue
fi
# Export the environment variable
echo "$line" >> $GITHUB_ENV
done < deployment/deploy_env_vars
- 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: Release
needs: build needs: build
runs-on: ubuntu-latest runs-on: ubuntu-latest
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 artifact - name: Set Image Tag
uses: actions/download-artifact@v4 id: set_image_tag
with: run: |
name: build IMAGE_TAG=$(date +'%Y%m%d%H%M%S')
path: build/ echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Load environment variables - name: Load environment variables
run: | run: |
env_vars=$(cat deployment/deploy_env_vars) if [[ -f deployment/deploy_env_vars ]]; then
echo "$env_vars" >> $GITHUB_ENV grep -v '^#' deployment/deploy_env_vars | while IFS= read -r line; do
[[ -z "$line" ]] && continue
- name: Set up Docker Buildx echo "$line" >> $GITHUB_ENV
uses: docker/setup-buildx-action@v3 done
fi
- name: Login to ACR
uses: docker/login-action@v3 - name: Set up Docker Buildx
with: uses: docker/setup-buildx-action@v3
registry: ${{ env.registry }}
username: ${{ secrets.ACR_USERNAME }} - name: Login to Azure Container Registry
password: ${{ secrets.ACR_PASSWORD }} uses: docker/login-action@v3
with:
- name: Build and push image registry: ${{ env.registry }}
uses: docker/build-push-action@v5 username: ${{ secrets.ACR_USERNAME }}
with: password: ${{ secrets.ACR_PASSWORD }}
context: .
push: true - name: Download build artifact
tags: ${{ env.registry }}/${{ env.image_org }}/lifesub-web:${{ needs.build.outputs.image_tag }} uses: actions/download-artifact@v4
build-args: | with:
PROJECT_FOLDER=. name: build
REACT_APP_MEMBER_URL=${{ env.react_app_member_url }} path: build/
REACT_APP_MYSUB_URL=${{ env.react_app_mysub_url }}
REACT_APP_RECOMMEND_URL=${{ env.react_app_recommend_url }} - name: Build and push Docker image
BUILD_FOLDER=deployment uses: docker/build-push-action@v5
EXPORT_PORT=${{ env.export_port }} with:
file: deployment/Dockerfile-lifesub-web context: .
file: deployment/Dockerfile-lifesub-web
push: true
tags: ${{ env.registry }}/${{ env.image_org }}/lifesub-web:${{ steps.set_image_tag.outputs.image_tag }}
build-args: |
PROJECT_FOLDER=.
REACT_APP_MEMBER_URL=${{ env.react_app_member_url }}
REACT_APP_MYSUB_URL=${{ env.react_app_mysub_url }}
REACT_APP_RECOMMEND_URL=${{ env.react_app_recommend_url }}
BUILD_FOLDER=deployment
EXPORT_PORT=${{ env.export_port }}
deploy: deploy:
name: Deploy name: Deploy
needs: [build, release] needs: release
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
IMAGE_TAG: ${{ needs.release.outputs.image_tag }}
steps: steps:
- name: Checkout repository - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Load environment variables
run: |
env_vars=$(cat deployment/deploy_env_vars)
echo "$env_vars" >> $GITHUB_ENV
- name: Set up kubectl
uses: azure/setup-kubectl@v3
- name: Set AKS context
uses: azure/aks-set-context@v3
with:
resource-group: ictcoe-edu
cluster-name: ${{ env.teamid }}-aks
admin: 'false'
use-kubelogin: 'true'
env:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
- name: Create namespace if not exists
run: |
kubectl create namespace ${{ env.namespace }} --dry-run=client -o yaml | kubectl apply -f -
- name: Install envsubst
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 lifesub_web_image_path=${{ env.registry }}/${{ env.image_org }}/lifesub-web:${IMAGE_TAG}
export replicas=${{ env.replicas }}
export export_port=${{ env.export_port }}
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 }}
# Generate deployment file
envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml
# For debugging
echo "Generated manifest:"
cat deployment/deploy.yaml
- name: Deploy to AKS
run: |
kubectl apply -f deployment/deploy.yaml
echo "Waiting for deployment to be ready..."
kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/lifesub-web --timeout=300s
echo "Service details:"
kubectl -n ${{ env.namespace }} get svc lifesub-web -o wide
- name: Wait for external IP
run: |
echo "Waiting for service external IP..."
for i in {1..30}; do
IP=$(kubectl -n ${{ env.namespace }} get svc lifesub-web -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
if [ -n "$IP" ]; then
echo "Service external IP: $IP"
break
fi
echo "Waiting for external IP... attempt $i/30"
sleep 10
done
if [ -z "$IP" ]; then
echo "Failed to get external IP after 5 minutes"
exit 1
fi
- name: Load environment variables
run: |
if [[ -f deployment/deploy_env_vars ]]; then
grep -v '^#' deployment/deploy_env_vars | while IFS= read -r line; do
[[ -z "$line" ]] && continue
echo "$line" >> $GITHUB_ENV
done
fi
- name: Install envsubst
run: |
sudo apt-get update
sudo apt-get install -y gettext-base
- name: Generate Kubernetes manifests
run: |
export namespace=${namespace}
export lifesub_web_image_path=${registry}/${image_org}/lifesub-web:${IMAGE_TAG}
export replicas=${replicas}
export export_port=${export_port}
export resources_requests_cpu=${resources_requests_cpu}
export resources_requests_memory=${resources_requests_memory}
export resources_limits_cpu=${resources_limits_cpu}
export resources_limits_memory=${resources_limits_memory}
envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml
echo "Generated Kubernetes manifest:"
cat deployment/deploy.yaml
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Azure login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Set up kubeconfig
uses: azure/aks-set-context@v3
with:
resource-group: ictcoe-edu
cluster-name: ${{ env.teamid }}-aks
- name: Create namespace if not exists
run: |
kubectl create namespace ${{ env.namespace }} --dry-run=client -o yaml | kubectl apply -f -
- name: Deploy to AKS
run: |
kubectl apply -f deployment/deploy.yaml
- name: Wait for deployment to be ready
run: |
kubectl -n ${{ env.namespace }} wait --for=condition=available deployment/lifesub-web --timeout=300s
echo "Waiting for service external IP..."
while [[ -z $(kubectl -n ${{ env.namespace }} get svc lifesub-web -o jsonpath='{.status.loadBalancer.ingress[0].ip}') ]]; do
sleep 5
done
echo "Service external IP: $(kubectl -n ${{ env.namespace }} get svc lifesub-web -o jsonpath='{.status.loadBalancer.ingress[0].ip}')"