Skip to content

Commit ad41893

Browse files
committed
library-api: add deployment workflow
1 parent d4bb4f9 commit ad41893

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# This workflow uses devbox for dependency management and builds/deploys the library API
2+
# to Cloud Run when a version tag is pushed (e.g., library-api-v1.0.0).
3+
4+
name: 'Build and Deploy Library API to Cloud Run'
5+
6+
on:
7+
push:
8+
tags:
9+
- 'library-api-v*'
10+
11+
env:
12+
PROJECT_ID: 'benefit-decision-toolkit-play'
13+
REGION: 'us-central1'
14+
SERVICE: 'benefit-decision-toolkit-play'
15+
API_NAME: 'library-api'
16+
WORKLOAD_IDENTITY_PROVIDER: 'projects/1034049717668/locations/global/workloadIdentityPools/github-actions-google-cloud/providers/github'
17+
18+
jobs:
19+
deploy:
20+
runs-on: 'ubuntu-latest'
21+
22+
permissions:
23+
contents: 'read'
24+
id-token: 'write'
25+
26+
steps:
27+
- name: 'Checkout'
28+
uses: 'actions/checkout@v4'
29+
30+
# Devbox needs a .env file to exist, even if it's empty
31+
- name: 'Create .env file'
32+
run: touch .env
33+
34+
# Setup devbox which includes all our dependencies: Maven, JDK 21, Quarkus, etc.
35+
- name: 'Install devbox'
36+
uses: 'jetify-com/devbox-install-action@v0.12.0'
37+
with:
38+
enable-cache: true
39+
40+
# Extract version from pom.xml (source of truth) using Maven
41+
- name: 'Extract version from pom.xml'
42+
id: extract_version
43+
working-directory: library-api
44+
run: |
45+
VERSION=$(devbox run -- mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
46+
echo "version=$VERSION" >> $GITHUB_OUTPUT
47+
# Create revision-safe version string (replace dots with dashes for Cloud Run)
48+
REVISION_VERSION=$(echo "$VERSION" | tr '.' '-')
49+
echo "revision_version=$REVISION_VERSION" >> $GITHUB_OUTPUT
50+
echo "Extracted version from pom.xml: $VERSION"
51+
echo "Revision version: $REVISION_VERSION"
52+
53+
# Validate that git tag exists for this pom.xml version
54+
- name: 'Validate git tag matches pom.xml version'
55+
run: |
56+
devbox run -- bin/validate-library-api-version
57+
58+
# Configure Workload Identity Federation and generate an access token
59+
- id: 'auth'
60+
name: 'Authenticate to Google Cloud'
61+
uses: 'google-github-actions/auth@v2'
62+
with:
63+
workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}'
64+
service_account: cicd-build-deploy-api@benefit-decision-toolkit-play.iam.gserviceaccount.com
65+
project_id: ${{ env.PROJECT_ID }}
66+
67+
# Configure Docker to use gcloud as a credential helper (using devbox gcloud)
68+
- name: 'Configure Docker'
69+
run: |
70+
devbox run -- gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev
71+
72+
# Build the Quarkus app with Maven using devbox environment
73+
- name: 'Build Quarkus App'
74+
working-directory: library-api
75+
run: |
76+
devbox run build-library-api-ci
77+
78+
- name: 'Build and Push Container'
79+
working-directory: library-api
80+
run: |-
81+
VERSION="${{ steps.extract_version.outputs.version }}"
82+
DOCKER_TAG_VERSIONED="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}/${{ env.API_NAME }}:v${VERSION}"
83+
DOCKER_TAG_LATEST="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}/${{ env.API_NAME }}:latest"
84+
85+
# Build and tag with version
86+
docker build -f src/main/docker/Dockerfile.jvm --tag "${DOCKER_TAG_VERSIONED}" --tag "${DOCKER_TAG_LATEST}" .
87+
88+
# Push both tags
89+
docker push "${DOCKER_TAG_VERSIONED}"
90+
docker push "${DOCKER_TAG_LATEST}"
91+
92+
echo "Pushed images:"
93+
echo " - ${DOCKER_TAG_VERSIONED}"
94+
echo " - ${DOCKER_TAG_LATEST}"
95+
96+
- name: 'Deploy to Cloud Run'
97+
id: deploy
98+
uses: 'google-github-actions/deploy-cloudrun@v2'
99+
with:
100+
service: '${{ env.API_NAME }}'
101+
region: '${{ env.REGION }}'
102+
image: '${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}/${{ env.API_NAME }}:v${{ steps.extract_version.outputs.version }}'
103+
tag: '${{ env.API_NAME }}-v${{ steps.extract_version.outputs.revision_version }}'
104+
service_account: 'library-api-service-account@${{ env.PROJECT_ID }}.iam.gserviceaccount.com'
105+
flags: '--allow-unauthenticated --max-instances=2'
106+
env_vars: |
107+
QUARKUS_GOOGLE_CLOUD_PROJECT_ID=${{ env.PROJECT_ID }}
108+
GCS_BUCKET_NAME=${{ env.PROJECT_ID }}.firebasestorage.app
109+
110+
# Show deployment output
111+
- name: 'Show deployment output'
112+
run: |
113+
echo "Deployment complete!"
114+
echo "Service URL: ${{ steps.deploy.outputs.url }}"
115+
echo "Version: v${{ steps.extract_version.outputs.version }}"
116+
echo "Revision: ${{ env.API_NAME }}-v${{ steps.extract_version.outputs.revision_version }}"

0 commit comments

Comments
 (0)