|
1 | | -name: Google Cloud Run Deploy |
2 | | -# https://github.com/google-github-actions/setup-gcloud |
| 1 | +name: GCP Deploy |
| 2 | + |
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: |
6 | | - - main |
7 | | - |
8 | | -env: |
9 | | - PROJECT_ID: ${{ secrets.GCR_PROJECT }} |
10 | | - RUN_REGION: us-central1 |
11 | | - SERVICE_NAME: medium-react-app |
| 6 | + - main # Change to your default branch if it's not main |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - main # Change to your default branch if it's not main |
12 | 10 |
|
13 | 11 | jobs: |
14 | 12 | build: |
15 | | - name: Build React App |
16 | 13 | runs-on: ubuntu-latest |
17 | 14 |
|
18 | 15 | steps: |
19 | | - - name: Checkout Repo |
20 | | - uses: actions/checkout@main |
21 | | - |
22 | | - - name: Setup Node.js (NPM) |
23 | | - uses: actions/setup-node@master |
24 | | - with: |
25 | | - node-version: "12.x" |
26 | | - |
27 | | - - name: Use cached node_modules |
28 | | - uses: actions/cache@master |
29 | | - with: |
30 | | - path: node_modules |
31 | | - key: nodeModules-${{ hashFiles('**/package-lock.json') }} |
32 | | - restore-keys: | |
33 | | - nodeModules- |
34 | | -
|
35 | | - - name: Install dependencies |
36 | | - run: npm ci |
37 | | - env: |
38 | | - CI: true |
39 | | - |
40 | | - - name: Build Development |
41 | | - run: npm run build |
42 | | - |
43 | | - - name: Archive Production Artifact |
44 | | - uses: actions/upload-artifact@main |
45 | | - with: |
46 | | - name: build |
47 | | - path: build |
48 | | - |
49 | | - deploy-gcr: |
50 | | - name: Deploy to GCR |
51 | | - needs: build |
52 | | - runs-on: ubuntu-latest |
53 | | - |
54 | | - steps: |
55 | | - - name: Checkout Repo |
56 | | - uses: actions/checkout@main |
57 | | - |
58 | | - - name: Download Artifact |
59 | | - uses: actions/download-artifact@main |
60 | | - with: |
61 | | - name: build |
62 | | - path: .docker/build |
63 | | - |
64 | | - - name: Setup GCloud Auth |
65 | | - id: auth |
66 | | - uses: google-github-actions/[email protected] |
67 | | - with: |
68 | | - credentials_json: ${{ inputs.GCR_SA_KEY }} |
69 | | - |
70 | | - - name: Set up Cloud SDK |
71 | | - uses: google-github-actions/[email protected] |
72 | | - |
73 | | - # Build and push image to Google Container Registry |
74 | | - - name: Build & Push |
75 | | - run: |- |
76 | | - gcloud builds submit \ |
77 | | - --quiet \ |
78 | | - --tag "gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA" |
79 | | - working-directory: .docker |
80 | | - |
81 | | - # Deploy image to Cloud Run |
82 | | - - name: Deploy GCR |
83 | | - run: |- |
84 | | - gcloud run deploy "$SERVICE_NAME" \ |
85 | | - --quiet \ |
86 | | - --region "$RUN_REGION" \ |
87 | | - --image "gcr.io/$PROJECT_ID/$SERVICE_NAME:$GITHUB_SHA" \ |
88 | | - --platform "managed" \ |
89 | | - --allow-unauthenticated |
| 16 | + - name: Checkout code |
| 17 | + uses: actions/checkout@v2 |
| 18 | + |
| 19 | + - name: Set up Docker Buildx |
| 20 | + uses: docker/setup-buildx-action@v1 |
| 21 | + |
| 22 | + - name: Cache Docker layers |
| 23 | + uses: actions/cache@v2 |
| 24 | + with: |
| 25 | + path: /tmp/.buildx-cache |
| 26 | + key: ${{ runner.os }}-buildx-${{ github.sha }} |
| 27 | + restore-keys: | |
| 28 | + ${{ runner.os }}-buildx- |
| 29 | +
|
| 30 | + - name: Log in to Google Container Registry |
| 31 | + env: |
| 32 | + GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} |
| 33 | + GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} |
| 34 | + run: | |
| 35 | + echo "${GCP_SA_KEY}" | base64 --decode > ${HOME}/gcp-key.json |
| 36 | + gcloud auth activate-service-account --key-file=${HOME}/gcp-key.json |
| 37 | + gcloud config set project $GCP_PROJECT_ID |
| 38 | + gcloud auth configure-docker |
| 39 | +
|
| 40 | + - name: Build Docker image |
| 41 | + run: | |
| 42 | + docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/gcp-automation:latest . |
| 43 | +
|
| 44 | + - name: Push Docker image |
| 45 | + run: | |
| 46 | + docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/gcp-automation:latest |
| 47 | +
|
| 48 | + - name: Deploy to Cloud Run |
| 49 | + env: |
| 50 | + GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} |
| 51 | + run: | |
| 52 | + gcloud run deploy gcp-automation \ |
| 53 | + --image gcr.io/${{ secrets.GCP_PROJECT_ID }}/gcp-automation:latest \ |
| 54 | + --platform managed \ |
| 55 | + --region us-central1 \ |
| 56 | + --allow-unauthenticated |
0 commit comments