This commit is contained in:
hiondal 2025-03-03 21:55:38 +09:00
parent 3301d09288
commit 76a9bcf18b

View File

@ -1,140 +1,202 @@
name: Frontend CI/CD Pipeline name: Frontend CI/CD Pipeline
# Temporarily disabled on:
# on: push:
# push: branches: [ main ]
# branches: [ main ] paths:
env: - '**'
PROJECT_FOLDER: "." - '!.github/**'
BUILD_FOLDER: "deployment" - '!**.md'
jobs: jobs:
# Build stage
build: build:
runs-on: ubuntu-latest name: Build
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: build-files
path: build/
retention-days: 1
# Release stage
release:
needs: build
runs-on: ubuntu-latest runs-on: ubuntu-latest
outputs: outputs:
image_tag: ${{ steps.set-tag.outputs.tag }} image_tag: ${{ steps.set_outputs.outputs.image_tag }}
steps: steps:
- uses: actions/checkout@v4 - name: Checkout repository
uses: actions/checkout@v4
- name: Load environment variables - name: Set up Node.js
run: | uses: actions/setup-node@v4
props=$(cat deployment/deploy_env_vars) with:
echo "$props" >> $GITHUB_ENV node-version: '20'
cache: 'npm'
- name: Set image tag - name: Install dependencies
id: set-tag run: npm ci
run: |
timestamp=$(date +'%Y%m%d%H%M%S')
echo "tag=${timestamp}" >> $GITHUB_OUTPUT
- name: Download build artifact - name: Run tests
uses: actions/download-artifact@v4 run: npm test -- --coverage --passWithNoTests
with:
name: build-files
path: build/
- name: Set up Docker Buildx - name: SonarQube Scan
uses: docker/setup-buildx-action@v3 uses: SonarSource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
with:
args: >
-Dsonar.projectKey=lifesub-web
-Dsonar.sources=src
-Dsonar.tests=src
-Dsonar.test.inclusions=src/**/*.test.js,src/**/*.test.jsx
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
- name: Log in to Azure Container Registry - name: SonarQube Quality Gate check
uses: azure/docker-login@v1 uses: SonarSource/sonarqube-quality-gate-action@master
with: timeout-minutes: 5
login-server: ${{ env.registry }} env:
username: ${{ secrets.ACR_USERNAME }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Build and push image - name: Build application
uses: docker/build-push-action@v5 run: npm run build
with:
context: .
file: deployment/Dockerfile-lifesub-web
push: true
tags: ${{ env.registry }}/${{ env.image_org }}/lifesub-web:${{ steps.set-tag.outputs.tag }}
build-args: |
PROJECT_FOLDER=${{ env.PROJECT_FOLDER }}
BUILD_FOLDER=${{ env.BUILD_FOLDER }}
EXPORT_PORT=${{ env.export_port }}
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 }}
# Deploy stage - name: Upload build artifact
deploy: uses: actions/upload-artifact@v4
needs: release with:
name: build
path: build/
- name: Load environment variables
run: |
env_vars=$(cat deployment/deploy_env_vars)
echo "$env_vars" >> $GITHUB_ENV
- 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:
name: Release
needs: build
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - name: Checkout repository
uses: actions/checkout@v4
- name: Load environment variables - name: Download build artifact
run: | uses: actions/download-artifact@v4
props=$(cat deployment/deploy_env_vars) with:
echo "$props" >> $GITHUB_ENV name: build
path: build/
- name: Azure login - name: Load environment variables
uses: azure/login@v1 run: |
with: env_vars=$(cat deployment/deploy_env_vars)
creds: ${{ secrets.AZURE_CREDENTIALS }} echo "$env_vars" >> $GITHUB_ENV
- name: Set AKS context - name: Set up Docker Buildx
uses: azure/aks-set-context@v3 uses: docker/setup-buildx-action@v3
with:
resource-group: ictcoe-edu
cluster-name: ${{ env.teamid }}-aks
- name: Create namespace - name: Login to ACR
run: | uses: docker/login-action@v3
kubectl create namespace ${{ env.teamid }}-${{ env.root_project }}-ns --dry-run=client -o yaml | kubectl apply -f - with:
registry: ${{ env.registry }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- name: Generate deployment manifest - name: Build and push image
run: | uses: docker/build-push-action@v5
export namespace=${{ env.teamid }}-${{ env.root_project }}-ns with:
export lifesub_web_image_path=${{ env.registry }}/${{ env.image_org }}/lifesub-web:${{ needs.release.outputs.image_tag }} context: .
export replicas=${{ env.replicas }} push: true
export export_port=${{ env.export_port }} tags: ${{ env.registry }}/${{ env.image_org }}/lifesub-web:${{ needs.build.outputs.image_tag }}
export resources_requests_cpu=${{ env.resources_requests_cpu }} build-args: |
export resources_requests_memory=${{ env.resources_requests_memory }} PROJECT_FOLDER=.
export resources_limits_cpu=${{ env.resources_limits_cpu }} REACT_APP_MEMBER_URL=${{ env.react_app_member_url }}
export resources_limits_memory=${{ env.resources_limits_memory }} 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 }}
file: deployment/Dockerfile-lifesub-web
envsubst < deployment/deploy.yaml.template > deployment/deploy.yaml deploy:
echo "Generated manifest:" name: Deploy
cat deployment/deploy.yaml needs: [build, release]
runs-on: ubuntu-latest
- name: Deploy to AKS steps:
run: | - name: Checkout repository
kubectl apply -f deployment/deploy.yaml uses: actions/checkout@v4
echo "Waiting for deployment to be ready..."
kubectl -n ${{ env.teamid }}-${{ env.root_project }}-ns wait --for=condition=available deployment/lifesub-web --timeout=300s
echo "Waiting for service external IP..." - name: Load environment variables
while [[ -z $(kubectl -n ${{ env.teamid }}-${{ env.root_project }}-ns get svc lifesub-web -o jsonpath='{.status.loadBalancer.ingress[0].ip}') ]]; do run: |
sleep 5 env_vars=$(cat deployment/deploy_env_vars)
done echo "$env_vars" >> $GITHUB_ENV
echo "Service external IP: $(kubectl -n ${{ env.teamid }}-${{ env.root_project }}-ns get svc lifesub-web -o jsonpath='{.status.loadBalancer.ingress[0].ip}')"
- 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