Merge pull request #28 from a-company-jp/intro-ci-nginx #1
This file contains 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: Build&Push Nginx image to GAR | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'docker/nginx/**' | |
- '.github/workflows/deploy-nginx.yaml' | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: 'read' | |
id-token: 'write' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Setup Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
cache: 'yarn' | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to Google Cloud | |
id: auth | |
uses: google-github-actions/auth@v2 | |
with: | |
token_format: access_token | |
workload_identity_provider: 'projects/228944830644/locations/global/workloadIdentityPools/gh-pool/providers/provider-github' | |
service_account: '[email protected]' | |
- name: Authorize Docker | |
id: docker-auth | |
uses: docker/login-action@v3 | |
with: | |
username: 'oauth2accesstoken' | |
password: ${{ steps.auth.outputs.access_token }} | |
registry: asia-northeast1-docker.pkg.dev | |
- name: Load .env file | |
run: | | |
echo "${{ secrets.ENV_FILE }}" > .env | |
- name: Install dependencies and build | |
run: | | |
yarn install | |
yarn build | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
push: true | |
tags: asia-northeast1-docker.pkg.dev/lgtmgen-project/dev-main/nginx:latest | |
file: ./docker/nginx/Dockerfile | |
context: ./ | |
- name: Download Cloud Run Service YAML | |
run: | | |
gcloud run services describe dev-main-backend --format yaml --region asia-northeast1 > ./service.yaml | |
# replace github_sha field to latest commit sha. Changing spec.template is required to deploy new revision. | |
# reference: https://cloud.google.com/run/docs/deploying?hl=ja#revision -- check yaml tab. | |
- name: Change some property of service.yaml | |
run: | | |
sed -i "s/github_sha: .*/github_sha: ${{ github.sha }}/g" ./service.yaml | |
- name: Deploy to Cloud Run | |
id: deploy | |
uses: google-github-actions/deploy-cloudrun@v2 | |
with: | |
region: "asia-northeast1" | |
metadata: "./service.yaml" | |
env_vars_file: ".env" |