Merge pull request #74 from prgrms-web-devcourse-final-project/QUZ-12… #48
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: GCP Gateway Image Build | |
env: | |
SERVICE_NAME: gateway-service | |
BUILD_ID: ${{ github.sha }}-${{ github.run_id }} | |
HELM_VALUE: gateway-service-chart/values-dev.yaml | |
HELM_BRANCH: dev | |
on: | |
push: | |
branches: | |
- 'develop' | |
paths: | |
- 'gateway-service/**' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the repository | |
uses: actions/checkout@v4 | |
- name: Google Cloud Auth | |
uses: google-github-actions/auth@v1 | |
with: | |
credentials_json: ${{ secrets.GCP_SA_KEY }} | |
- name: Set up Cloud SDK | |
uses: google-github-actions/setup-gcloud@v1 | |
- name: Configure Docker Authentication | |
run: gcloud auth configure-docker ${{ secrets.GCP_REGION }}-docker.pkg.dev --quiet | |
- name: Build, tag, and push docker image to Google Artifact Registry | |
env: | |
IMAGE_TAG: ${{ secrets.GCP_REGION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.REPO_NAME }}/${{ env.SERVICE_NAME }}:${{ env.BUILD_ID }} | |
run: | | |
docker build -f ./gateway-service/Dockerfile -t $IMAGE_TAG . | |
docker push $IMAGE_TAG | |
- name: Checkout Helm Repository | |
uses: actions/checkout@v4 | |
with: | |
repository: quizy-web/quizy-helm-charts | |
path: helm-chart | |
ref: ${{ env.HELM_BRANCH }} | |
ssh-key: ${{ secrets.HELM_REPO_SSH_KEY }} | |
- name: Change Image tag of the Helm Chart | |
uses: mikefarah/[email protected] | |
with: | |
cmd: yq -i '.image.tag = "${{ env.BUILD_ID }}"' helm-chart/${{ env.HELM_VALUE }} | |
- name: Commit and Push Helm Chart Changes | |
run: | | |
cd helm-chart | |
git config --local user.email "[email protected]" | |
git config --local user.name "githubaction" | |
git add ${{ env.HELM_VALUE }} | |
git commit -m "Update ${SERVICE_NAME} image tag to ${{ env.BUILD_ID }}" || echo "No changes to commit" | |
attempts=0 | |
max_attempts=3 | |
while [ $attempts -lt $max_attempts ]; do | |
if git push --force origin ${{ env.HELM_BRANCH }}; then | |
echo "Push successful!" | |
break | |
else | |
echo "Push failed due to remote ref lock. Attempting rebase with remote branch." | |
# 원격 브랜치 최신 상태 반영 | |
git pull --rebase origin ${{ env.HELM_BRANCH }} | |
attempts=$((attempts+1)) | |
if [ $attempts -eq $max_attempts ]; then | |
echo "Push failed after $max_attempts attempts" | |
exit 1 | |
fi | |
echo "Retrying push..." | |
fi | |
done |