From 08e5d5360bfa90092740151fbb1605e29f5de97f Mon Sep 17 00:00:00 2001 From: maxmwang Date: Fri, 16 Aug 2024 02:58:04 -0700 Subject: [PATCH] prod workflow --- .github/workflows/cd-dev.yaml | 4 +- .github/workflows/cd-prod.yaml | 125 ++++++++++++++++++++++++++++++++ .github/workflows/cd-stage.yaml | 6 +- 3 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/cd-prod.yaml diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 1e8aa9b13..eb93c2987 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -31,7 +31,7 @@ jobs: - name: Build Image with Tag run: | - docker build --target backend-dev --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ env.sha_short }}" . + docker build --target backend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ env.sha_short }}" . docker save "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ env.sha_short }}" --output "bt-backend-${{ env.sha_short }}.tar" - name: Upload Image as Artifact @@ -60,7 +60,7 @@ jobs: - name: Build Image with Tag run: | - docker build --target frontend-dev --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ env.sha_short }}" . + docker build --target frontend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ env.sha_short }}" . docker save "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ env.sha_short }}" --output "bt-frontend-${{ env.sha_short }}.tar" - name: Upload Image as Artifact diff --git a/.github/workflows/cd-prod.yaml b/.github/workflows/cd-prod.yaml new file mode 100644 index 000000000..1d231c5df --- /dev/null +++ b/.github/workflows/cd-prod.yaml @@ -0,0 +1,125 @@ +name: Deploy to Production + +concurrency: prod + +on: + workflow_dispatch: + +env: + artifact-retention-days: 7 + +jobs: + branch-check: + name: Check Branch + runs-on: ubuntu-latest + environment: prod + + build-backend: + name: Build Backend Image + needs: branch-check + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Build Image with Tag + run: | + docker build --target backend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:prod" . + docker save "${{ secrets.DOCKER_USERNAME }}/bt-backend:prod" --output "bt-backend-prod.tar" + + - name: Upload Image as Artifact + uses: actions/upload-artifact@v4 + with: + name: "bt-backend-prod.tar" + path: "bt-backend-prod.tar" + retention-days: ${{ env.artifact-retention-days }} + overwrite: true + + build-frontend: + name: Build Frontend Image + needs: branch-check + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Build Image with Tag + run: | + docker build --target frontend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:prod" . + docker save "${{ secrets.DOCKER_USERNAME }}/bt-frontend:prod" --output "bt-frontend-prod.tar" + + - name: Upload Image as Artifact + uses: actions/upload-artifact@v4 + with: + name: "bt-frontend-prod.tar" + path: "bt-frontend-prod.tar" + retention-days: ${{ env.artifact-retention-days }} + overwrite: true + + push-backend: + name: Push Backend Image + needs: build-backend + runs-on: ubuntu-latest + + steps: + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Download Artifact as Image + uses: actions/download-artifact@v4 + with: + name: "bt-backend-prod.tar" + + - name: Push Image to Docker Hub + run: | + docker import "bt-backend-prod.tar" "${{ secrets.DOCKER_USERNAME }}/bt-backend:prod" + docker push "${{ secrets.DOCKER_USERNAME }}/bt-backend:prod" + + push-frontend: + name: Push Frontend Image + needs: build-frontend + runs-on: ubuntu-latest + + steps: + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Download Artifact as Image + uses: actions/download-artifact@v4 + with: + name: "bt-frontend-prod.tar" + + - name: Push Image to Docker Hub + run: | + docker import "bt-frontend-prod.tar" "${{ secrets.DOCKER_USERNAME }}/bt-frontend:prod" + docker push "${{ secrets.DOCKER_USERNAME }}/bt-frontend:prod" + + deploy: + name: Deploy with SSH + needs: [push-backend, push-frontend] + runs-on: ubuntu-latest + + steps: + - name: SSH and Helm Install + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.SSH_HOST }} + username: root + key: ${{ secrets.SSH_KEY }} + script: | + cd ./infra + if helm status bt-prod-app ; then + kubectl rollout restart bt-prod-app-backend + kubectl rollout restart bt-prod-app-frontend + else + helm install bt-prod-app ./app --namespace=bt \ + --set host=stanfurdtime.com + fi diff --git a/.github/workflows/cd-stage.yaml b/.github/workflows/cd-stage.yaml index 8c9f0c704..4e7326c31 100644 --- a/.github/workflows/cd-stage.yaml +++ b/.github/workflows/cd-stage.yaml @@ -1,5 +1,7 @@ name: Deploy to Staging +concurrency: stage + on: push: branches: [master] @@ -18,7 +20,7 @@ jobs: - name: Build Image with Tag run: | - docker build --target backend-dev --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:latest" . + docker build --target backend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:latest" . docker save "${{ secrets.DOCKER_USERNAME }}/bt-backend:latest" --output "bt-backend-latest.tar" - name: Upload Image as Artifact @@ -39,7 +41,7 @@ jobs: - name: Build Image with Tag run: | - docker build --target frontend-dev --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:latest" . + docker build --target frontend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:latest" . docker save "${{ secrets.DOCKER_USERNAME }}/bt-frontend:latest" --output "bt-frontend-latest.tar" - name: Upload Image as Artifact