Skip to content

Update README

Update README #131

name: Build & publish PeerPrep images
on:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
push:
branches:
- main
env:
DOCKER_REGISTRY_USN: ay2425s1cs3219g16
USER_EXPRESS_PORT: 9001
QUESTION_EXPRESS_PORT: 9002
COLLAB_EXPRESS_PORT: 9003
MATCH_EXPRESS_PORT: 9004
CHAT_EXPRESS_PORT: 9005
FRONTEND_PORT: 3000
jobs:
changes:
if: ${{ !github.event.pull_request.draft && github.event.pull_request.title != 'Feedback' }}
runs-on: ubuntu-latest
# Required permissions
permissions:
pull-requests: read
# Set job outputs to values from filter step
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }} # Output the matrix as a JSON string
steps:
- uses: actions/checkout@v4
if: contains(github.ref, 'main')
# For pull requests it's not necessary to checkout the code
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
user:
- 'backend/user/**'
question:
- 'backend/question/**'
collaboration:
- 'backend/collaboration/**'
matching:
- 'backend/matching/**'
chat:
- 'backend/chat/**'
frontend:
- 'frontend/**'
- name: output-job-matrix
id: set-matrix
run: |
is_main=${{ contains(github.ref, 'main') }}
matrix=()
if [[ "${{ steps.filter.outputs.user }}" == "true" || "$is_main" == "true" ]]; then
config=$(jq -n \
--arg pkg "user" \
--arg img "$DOCKER_REGISTRY_USN/user-express" \
--arg ctx "./backend/user" \
--arg dkr "./backend/user/express.Dockerfile" \
--arg bag "port=$USER_EXPRESS_PORT" \
'{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}')
matrix+=("$config")
fi
if [[ "${{ steps.filter.outputs.question }}" == "true" || "$is_main" == "true" ]]; then
config=$(jq -n \
--arg pkg "question" \
--arg img "$DOCKER_REGISTRY_USN/question-express" \
--arg ctx "./backend/question" \
--arg dkr "./backend/question/express.Dockerfile" \
--arg bag "port=$QUESTION_EXPRESS_PORT" \
'{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}')
matrix+=("$config")
fi
if [[ "${{ steps.filter.outputs.collaboration }}" == "true" || "$is_main" == "true" ]]; then
config=$(jq -n \
--arg pkg "collaboration" \
--arg img "$DOCKER_REGISTRY_USN/collab-express" \
--arg ctx "./backend/collaboration" \
--arg dkr "./backend/collaboration/express.Dockerfile" \
--arg bag "port=$COLLAB_EXPRESS_PORT" \
'{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}')
matrix+=("$config")
fi
if [[ "${{ steps.filter.outputs.matching }}" == "true" || "$is_main" == "true" ]]; then
config=$(jq -n \
--arg pkg "matching" \
--arg img "$DOCKER_REGISTRY_USN/match-express" \
--arg ctx "./backend/matching" \
--arg dkr "./backend/matching/express.Dockerfile" \
--arg bag "port=$MATCH_EXPRESS_PORT" \
'{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}')
matrix+=("$config")
fi
if [[ "${{ steps.filter.outputs.chat }}" == "true" || "$is_main" == "true" ]]; then
config=$(jq -n \
--arg pkg "chat" \
--arg img "$DOCKER_REGISTRY_USN/chat-express" \
--arg ctx "./backend/chat" \
--arg dkr "./backend/chat/express.Dockerfile" \
--arg bag "port=$CHAT_EXPRESS_PORT" \
'{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}')
matrix+=("$config")
fi
if [[ "${{ steps.filter.outputs.frontend }}" == "true" || "$is_main" == "true" ]]; then
config=$(jq -n \
--arg pkg "frontend" \
--arg img "$DOCKER_REGISTRY_USN/frontend" \
--arg ctx "./frontend" \
--arg dkr "./frontend/frontend.Dockerfile" \
--arg bag "port=$FRONTEND_PORT" \
'{package: $pkg, image: $img, context: $ctx, dockerfile: $dkr, "build-args": $bag}')
matrix+=("$config")
fi
formatted_matrix=$(echo "${matrix[@]}" | jq -cs .)
echo "Outputs Generated: $formatted_matrix"
echo "matrix=$formatted_matrix" >> $GITHUB_OUTPUT
build-push-deploy-image:
needs: changes
if: ${{ fromJson(needs.changes.outputs.matrix)[0] != null }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.changes.outputs.matrix) }} # Use the matrix from the first job
# - package: user
# image: ay2425s1cs3219g16/user-express
# context: ./backend/user
# dockerfile: ./backend/user/express.Dockerfile
# build-args: |
# port=9001
# - package: question
# image: ay2425s1cs3219g16/question-express
# context: ./backend/question
# dockerfile: ./backend/question/express.Dockerfile
# build-args: |
# port=9002
# - package: collaboration
# image: ay2425s1cs3219g16/collab-express
# context: ./backend/collaboration
# dockerfile: ./backend/collaboration/express.Dockerfile
# build-args: |
# port=9003
# - package: matching
# image: ay2425s1cs3219g16/match-express
# context: ./backend/matching
# dockerfile: ./backend/matching/express.Dockerfile
# build-args: |
# port=9004
# - package: frontend
# image: ay2425s1cs3219g16/frontend
# context: ./frontend
# dockerfile: ./frontend/frontend.Dockerfile
# build-args: |
# port=3000
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- 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 the Container registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ matrix.image }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker images for PeerPrep Services
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
build-args: ${{ matrix.build-args }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Auth GCloud
id: gcloud-auth
uses: google-github-actions/auth@v2
if: ${{ contains(github.ref, 'main') && github.event.pull_request.title != 'Feedback' }}
with:
credentials_json: '${{ secrets.GKE_CJSON }}'
continue-on-error: true
- name: Detect GKE Cluster
id: gcloud-detect-gke
if: ${{ contains(github.ref, 'main') && github.event.pull_request.title != 'Feedback' }}
run: |-
prev_step_success="${{ steps.gcloud-auth.outcome }}"
if [[ "$prev_step_success" != "success" ]]; then
echo "is_up=false" >> $GITHUB_OUTPUT
else
if [[ -z "$(gcloud container clusters list | grep ${{ secrets.GKE_CLUSTER }})" ]]; then
echo "is_up=false" >> $GITHUB_OUTPUT
else
echo "is_up=true" >> $GITHUB_OUTPUT
fi
fi
- name: Get GKE creds
id: gcloud-get-gke-creds
uses: google-github-actions/get-gke-credentials@v2
if: ${{ contains(github.ref, 'main') && github.event.pull_request.title != 'Feedback' && steps.gcloud-detect-gke.outputs.is_up == 'true' }}
with:
cluster_name: ${{ secrets.GKE_CLUSTER }}
location: ${{ secrets.GKE_ZONE }}
- name: Deploy to GKE
id: gcloud-deploy-gke
if: ${{ contains(github.ref, 'main') && github.event.pull_request.title != 'Feedback' && steps.gcloud-detect-gke.outputs.is_up == 'true' }}
run: |-
service="${{ matrix.package }}"
deployment="frontend"
if [[ "$service" == "collaboration" ]]; then
deployment="collab-service"
elif [[ "$service" != "frontend" ]]; then
deployment="$service-service"
fi
kubectl -n peerprep rollout restart deployment "$deployment"
results:
if: ${{ always() && !github.event.pull_request.draft }}
runs-on: ubuntu-latest
name: Final Results
needs: build-push-deploy-image
steps:
- run: |
result="${{ needs.build-push-deploy-image.result }}"
if [[ $result == "success" || $result == "skipped" ]]; then
exit 0
else
exit 1
fi