Skip to content

[PE-4] Refactor workflow file #2

[PE-4] Refactor workflow file

[PE-4] Refactor workflow file #2

Workflow file for this run

name: Build and Deploy to Google Cloud Run
on:
pull_request:
branches: [main]
jobs:
setup-and-test:
name: Setup and Test
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
outputs:
image: us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/react-labs-test/react-labs:latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
authenticate-gcloud:
name: Authenticate to Google Cloud
runs-on: ubuntu-latest
needs: setup-and-test
steps:
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0.2.0
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCLOUD_SERVICE_KEY }}
export_default_credentials: true
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v0.5.0
with:
credentials_json: ${{ secrets.GCLOUD_SERVICE_KEY }}
build-and-push:
name: Build and Push Docker image
runs-on: ubuntu-latest
needs: authenticate-gcloud
steps:
- uses: actions/checkout@v2
- name: Configure Docker to use the gcloud command-line tool as a credential helper
run: gcloud auth configure-docker us-central1-docker.pkg.dev --quiet
- name: Build and Push Docker image to Google Artifact Registry
run: |
docker build -t us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/react-labs-test/react-labs:latest .
docker push us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/react-labs-test/react-labs:latest
deploy:
name: Deploy to Cloud Run
runs-on: ubuntu-latest
needs: build-and-push
steps:
- name: Deploy to Cloud Run (CLI)
run: |
gcloud run deploy react-labs \
--image=${{ needs.setup-and-test.outputs.image }} \
--project=${{ secrets.GCP_PROJECT_ID }} \
--service-account=gh-actions-pipeline@${{ secrets.GCP_PROJECT_ID }}.iam.gserviceaccount.com \
--region=us-central1 \
--platform=managed \
--quiet \
--allow-unauthenticated \
--verbosity=debug