Merge pull request #54 from RestTest-App/develop #42
This file contains hidden or 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: Deploy to GCP GCE | |
| on: | |
| push: | |
| branches: [ "release" ] | |
| workflow_dispatch: | |
| env: | |
| IMAGE_NAME: resttest-be | |
| GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | |
| GCP_REGION: ${{ secrets.GCP_REGION }} | |
| ARTIFACT_REPO: ${{ secrets.ARTIFACT_REPO }} | |
| ARTIFACT_REGISTRY: ${{ secrets.GCP_REGION }}-docker.pkg.dev | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout source | |
| uses: actions/checkout@v3 | |
| - name: Auth to GCP | |
| uses: google-github-actions/auth@v2 | |
| with: | |
| credentials_json: ${{ secrets.GCP_SA_KEY }} | |
| - name: Set up Cloud SDK | |
| uses: google-github-actions/setup-gcloud@v2 | |
| with: | |
| project_id: ${{ env.GCP_PROJECT_ID }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Cache Docker layers | |
| uses: actions/cache@v4 | |
| with: | |
| path: /tmp/.buildx-cache | |
| key: ${{ runner.os }}-buildx-${{ github.sha }} | |
| restore-keys: | | |
| ${{ runner.os }}-buildx- | |
| - name: Configure Docker for Artifact Registry | |
| run: | | |
| gcloud auth configure-docker ${{ env.ARTIFACT_REGISTRY }} --quiet | |
| - name: Build and Push Docker Image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| ${{ env.ARTIFACT_REGISTRY }}/${{ env.GCP_PROJECT_ID }}/${{ env.ARTIFACT_REPO }}/${{ env.IMAGE_NAME }}:latest | |
| cache-from: type=local,src=/tmp/.buildx-cache | |
| cache-to: type=local,dest=/tmp/.buildx-cache-new | |
| - name: Deploy to GCE via SSH | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.GCE_HOST }} | |
| username: ${{ secrets.GCE_SSH_USER }} | |
| key: ${{ secrets.GCE_SSH_KEY }} | |
| script: | | |
| cd ~/app-path | |
| cat << 'EOF' > .env | |
| ${{ secrets.ENV_PROD }} | |
| EOF | |
| gcloud auth configure-docker ${{ env.ARTIFACT_REGISTRY }} --quiet | |
| docker-compose -f docker-compose.prod.yml down | |
| docker-compose -f docker-compose.prod.yml pull | |
| docker-compose -f docker-compose.prod.yml run --rm -e MODE=prod fastapi alembic upgrade head | |
| docker-compose -f docker-compose.prod.yml up -d |