|
1 | | -name: Deploy to Cloud Run |
| 1 | +name: Publish Docker to GitHub Container Registry |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: |
6 | 6 | - master |
7 | | - pull_request: |
8 | | - branches: |
9 | | - - master |
10 | 7 | workflow_dispatch: |
11 | 8 |
|
12 | | -env: |
13 | | - PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} |
14 | | - REGION: us-central1 |
15 | | - SERVICE_NAME: tax-declaration-app |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + packages: write |
16 | 12 |
|
17 | 13 | jobs: |
18 | | - terraform: |
19 | | - name: 'Terraform' |
20 | | - runs-on: ubuntu-latest |
21 | | - defaults: |
22 | | - run: |
23 | | - working-directory: ./terraform |
24 | | - |
25 | | - steps: |
26 | | - - name: Checkout |
27 | | - uses: actions/checkout@v4 |
28 | | - |
29 | | - - name: Setup Terraform |
30 | | - uses: hashicorp/setup-terraform@v3 |
31 | | - with: |
32 | | - terraform_version: 1.9.0 |
33 | | - |
34 | | - - name: Authenticate to Google Cloud |
35 | | - uses: google-github-actions/auth@v2 |
36 | | - with: |
37 | | - credentials_json: ${{ secrets.GCP_SA_KEY }} |
38 | | - |
39 | | - - name: Setup Google Cloud SDK |
40 | | - uses: google-github-actions/setup-gcloud@v2 |
41 | | - |
42 | | - - name: Terraform Init |
43 | | - run: terraform init |
44 | | - |
45 | | - - name: Terraform Format |
46 | | - run: terraform fmt -check |
47 | | - continue-on-error: true |
48 | | - |
49 | | - - name: Terraform Plan |
50 | | - run: terraform plan -var="project_id=${{ secrets.GCP_PROJECT_ID }}" -out=tfplan |
51 | | - if: github.event_name == 'pull_request' |
52 | | - |
53 | | - - name: Terraform Apply |
54 | | - run: terraform apply -var="project_id=${{ secrets.GCP_PROJECT_ID }}" -auto-approve |
55 | | - if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
56 | | - |
57 | | - build-and-deploy: |
58 | | - name: 'Build and Deploy' |
59 | | - needs: terraform |
| 14 | + publish: |
| 15 | + name: Build and publish to ghcr |
60 | 16 | runs-on: ubuntu-latest |
61 | 17 |
|
62 | 18 | steps: |
63 | | - - name: Checkout |
64 | | - uses: actions/checkout@v4 |
65 | | - |
66 | | - - name: Setup Node.js |
67 | | - uses: actions/setup-node@v4 |
68 | | - with: |
69 | | - node-version: '20' |
70 | | - |
71 | | - - name: Setup pnpm |
72 | | - uses: pnpm/action-setup@v3 |
73 | | - with: |
74 | | - version: 9 |
75 | | - |
76 | | - - name: Get pnpm store directory |
77 | | - id: pnpm-cache |
78 | | - shell: bash |
79 | | - run: | |
80 | | - echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT |
81 | | -
|
82 | | - - name: Setup pnpm cache |
83 | | - uses: actions/cache@v4 |
84 | | - with: |
85 | | - path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} |
86 | | - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} |
87 | | - restore-keys: | |
88 | | - ${{ runner.os }}-pnpm-store- |
89 | | -
|
90 | | - - name: Install dependencies |
91 | | - run: pnpm install --frozen-lockfile |
92 | | - |
93 | | - - name: Build application |
94 | | - run: pnpm run build |
95 | | - |
96 | | - - name: Authenticate to Google Cloud |
97 | | - uses: google-github-actions/auth@v2 |
98 | | - with: |
99 | | - credentials_json: ${{ secrets.GCP_SA_KEY }} |
100 | | - |
101 | | - - name: Set up Cloud SDK |
102 | | - uses: google-github-actions/setup-gcloud@v2 |
103 | | - |
104 | | - - name: Configure Docker |
105 | | - run: gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev |
106 | | - |
107 | | - - name: Build and Push Docker Image |
108 | | - run: | |
109 | | - docker build -t ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }} . |
110 | | - docker push ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }} |
111 | | -
|
112 | | - - name: Deploy to Cloud Run |
113 | | - run: | |
114 | | - gcloud run deploy ${{ env.SERVICE_NAME }} \ |
115 | | - --image ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE_NAME }}/${{ env.SERVICE_NAME }}:${{ github.sha }} \ |
116 | | - --region ${{ env.REGION }} \ |
117 | | - --allow-unauthenticated \ |
118 | | - --port 3000 \ |
119 | | - --memory 512Mi \ |
120 | | - --cpu 1 \ |
121 | | - --max-instances 10 \ |
122 | | - --min-instances 0 |
123 | | -
|
| 19 | + - name: Checkout repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Set up QEMU (for multi-arch, optional) |
| 23 | + uses: docker/setup-qemu-action@v2 |
| 24 | + |
| 25 | + - name: Set up Docker Buildx |
| 26 | + uses: docker/setup-buildx-action@v2 |
| 27 | + |
| 28 | + - name: Log in to GitHub Container Registry |
| 29 | + uses: docker/login-action@v2 |
| 30 | + with: |
| 31 | + registry: ghcr.io |
| 32 | + username: ${{ github.actor }} |
| 33 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + |
| 35 | + - name: Compute lowercase repo and image tags |
| 36 | + id: vars |
| 37 | + run: | |
| 38 | + repo_lower=$(echo "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]') |
| 39 | + image_sha="ghcr.io/${repo_lower}:${GITHUB_SHA}" |
| 40 | + image_latest="ghcr.io/${repo_lower}:latest" |
| 41 | + echo "repo_lower=${repo_lower}" >> $GITHUB_OUTPUT |
| 42 | + echo "image_sha=${image_sha}" >> $GITHUB_OUTPUT |
| 43 | + echo "image_latest=${image_latest}" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + - name: Build and push Docker image |
| 46 | + uses: docker/build-push-action@v4 |
| 47 | + with: |
| 48 | + context: . |
| 49 | + file: ./Dockerfile |
| 50 | + push: true |
| 51 | + tags: | |
| 52 | + ${{ steps.vars.outputs.image_sha }} |
| 53 | + ${{ steps.vars.outputs.image_latest }} |
| 54 | + cache-from: type=registry,ref=ghcr.io/${{ steps.vars.outputs.repo_lower }}:cache |
| 55 | + cache-to: type=inline |
| 56 | + |
| 57 | + - name: Output image information |
| 58 | + run: | |
| 59 | + echo "IMAGE=${{ steps.vars.outputs.image_sha }}" >> $GITHUB_OUTPUT |
| 60 | + echo "Image published to ${{ steps.vars.outputs.image_sha }}" |
0 commit comments