Deploy #29
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: Deploy | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- 'src-api/**/*' | |
- '.github/workflows/deploy.yml' | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: src-api | |
permissions: | |
contents: read | |
id-token: write # Required for Google Cloud | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Load target cache | |
uses: actions/cache/restore@v4 | |
id: cache-restore | |
with: | |
path: | | |
/home/runner/work/attendance/attendance/src-api/target | |
~/.cargo/registry | |
~/.cargo/git | |
key: ${{ runner.os }}-rust-cache | |
- name: Set up Rust | |
uses: actions-rs/toolchain@v1 | |
with: | |
toolchain: nightly | |
default: true | |
override: true | |
- name: Build | |
run: cargo build --release | |
env: | |
DATABASE_URL: ${{ secrets.DATABASE_URL }} | |
- name: Login to Google Cloud | |
id: gcp-auth | |
uses: google-github-actions/auth@v2 | |
with: | |
token_format: 'access_token' | |
project_id: ${{ secrets.GCP_PROJECT_ID }} | |
workload_identity_provider: ${{ secrets.GCP_WLIF_PROVIDER }} | |
service_account: ${{ secrets.GCP_SA }} | |
access_token_lifetime: 600s | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Login to Artifact Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: us-central1-docker.pkg.dev | |
username: oauth2accesstoken | |
password: ${{ steps.gcp-auth.outputs.access_token }} | |
- name: Push to GCR | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
file: /home/runner/work/attendance/attendance/src-api/Dockerfile | |
push: true | |
tags: | | |
us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/attendance/attendance:latest | |
onlycs/attendance:latest | |
- name: Deploy to Cloud Run | |
run: | | |
gcloud run deploy attendance \ | |
--image=us-central1-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/attendance/attendance:latest \ | |
--platform=managed \ | |
--region=us-central1 \ | |
--allow-unauthenticated | |
- name: Save target cache | |
if: always() && steps.cache-restore.outputs.cache-hit != 'true' | |
uses: actions/cache/save@v4 | |
with: | |
path: | | |
/home/runner/work/attendance/attendance/src-api/target | |
~/.cargo/registry | |
~/.cargo/git | |
key: ${{ steps.cache-restore.outputs.cache-primary-key }} |