Skip to content

Commit 0caad38

Browse files
authored
Add deployment workflow (#9)
1 parent 5de0c72 commit 0caad38

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
.git/

.github/workflows/build-push.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: build and push tagged image
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*.*.*"
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
env:
17+
GAR_LOCATION: "europe-west1"
18+
GAR_PATH: "europe-west1-docker.pkg.dev/cs-host-c9f0ca592f814d028135fa/bitcr-devs-repo"
19+
IMAGE: "bcr-relay"
20+
21+
jobs:
22+
build-push:
23+
runs-on: ubuntu-latest
24+
permissions:
25+
contents: read
26+
id-token: write
27+
attestations: write
28+
29+
steps:
30+
- name: Checkout code
31+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
32+
33+
- name: Docker login (GAR) with JSON key
34+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
35+
with:
36+
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
37+
username: _json_key
38+
password: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
39+
40+
- name: Set up Docker Buildx
41+
id: buildx
42+
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
43+
with:
44+
driver: docker
45+
46+
- id: meta
47+
name: Metadata for ${{ env.IMAGE }}
48+
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0
49+
with:
50+
images: ${{ env.GAR_PATH }}/${{ env.IMAGE }}
51+
tags: |
52+
type=semver,pattern={{version}}
53+
54+
- id: push
55+
name: Build & push ${{ env.IMAGE }}
56+
uses: docker/build-push-action@1dc73863535b631f98b2378be8619f83b136f4a0 # v6.17.0
57+
with:
58+
context: .
59+
file: Dockerfile
60+
push: true
61+
tags: ${{ steps.meta.outputs.tags }}
62+
labels: ${{ steps.meta.outputs.labels }}
63+
# Uncomment for per-service cache
64+
# cache-from: type=registry,ref=${{ env.GAR_PATH }}/${{ env.IMAGE }}:buildcache
65+
# cache-to: type=registry,ref=${{ env.GAR_PATH }}/${{ env.IMAGE }}:buildcache,mode=max
66+
67+
- uses: actions/attest-build-provenance@db473fddc028af60658334401dc6fa3ffd8669fd # v2.3.0
68+
with:
69+
subject-name: ${{ env.GAR_PATH }}/${{ env.IMAGE }}
70+
subject-digest: ${{ steps.push.outputs.digest }}
71+
push-to-registry: true

.github/workflows/deploy.yml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
name: 'Deploy to Cloud Run'
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
environment:
7+
description: 'deploy tag to environment'
8+
required: true
9+
default: 'dev'
10+
type: choice
11+
options:
12+
- dev
13+
- prod
14+
15+
jobs:
16+
17+
######################################################################
18+
# env: prod (bcr-relay)
19+
deploy:
20+
name: deploy to prod
21+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'prod'
22+
23+
permissions:
24+
contents: read
25+
deployments: write
26+
id-token: write
27+
concurrency:
28+
group: ${{ github.workflow }}-${{ github.event.inputs.environment || github.event_name }}
29+
cancel-in-progress: true
30+
31+
runs-on: ubuntu-latest
32+
environment: prod
33+
34+
env:
35+
GCR_SERVICE: ${{ vars.GCR_SERVICE }}
36+
GCR_REGION: ${{ vars.GCR_REGION }}
37+
GAR_PATH: ${{ vars.GAR_PATH }}
38+
IMAGE_NAME: ${{ vars.IMAGE_NAME }}
39+
40+
steps:
41+
- name: Validate tag on dispatch
42+
run: |
43+
if [[ "${{ github.ref_type }}" != 'tag' ]]; then
44+
echo "::error::Manual deployments must be triggered from a tag."
45+
echo "::error::Please select a tag from the 'Use workflow from' dropdown, not a branch."
46+
exit 1
47+
fi
48+
echo "Validation successful: Running from tag '${{ github.ref_name }}'."
49+
50+
- name: Authenticate to GCP
51+
id: auth
52+
uses: google-github-actions/auth@ba79af03959ebeac9769e648f473a284504d9193 # v2.1.10
53+
with:
54+
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
55+
56+
- name: 'Set image tag to ${{ github.ref_name }}'
57+
id: tag
58+
run: |
59+
TAG=${{ github.ref_name }}
60+
SEMVER_TAG="${TAG#v}"
61+
echo "IMAGE_TAG=$SEMVER_TAG" >> "$GITHUB_OUTPUT"
62+
63+
- name: Deploy to Cloud Run
64+
uses: google-github-actions/deploy-cloudrun@2028e2d7d30a78c6910e0632e48dd561b064884d # v3
65+
with:
66+
service: ${{ env.GCR_SERVICE }}
67+
region: ${{ env.GCR_REGION }}
68+
image: ${{ env.GAR_PATH }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.IMAGE_TAG }}
69+
70+
######################################################################
71+
# env: dev (bcr-relay-dev)
72+
deploy-dev:
73+
name: deploy to dev
74+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'dev'
75+
76+
permissions:
77+
contents: read
78+
deployments: write
79+
id-token: write
80+
concurrency:
81+
group: ${{ github.workflow }}-${{ github.event.inputs.environment || github.event_name }}
82+
cancel-in-progress: true
83+
84+
runs-on: ubuntu-latest
85+
environment: dev
86+
87+
env:
88+
GCR_SERVICE: ${{ vars.GCR_SERVICE }}
89+
GCR_REGION: ${{ vars.GCR_REGION }}
90+
GAR_PATH: ${{ vars.GAR_PATH }}
91+
IMAGE_NAME: ${{ vars.IMAGE_NAME }}
92+
93+
steps:
94+
- name: Validate tag on dispatch
95+
run: |
96+
if [[ "${{ github.ref_type }}" != 'tag' ]]; then
97+
echo "::error::Manual deployments must be triggered from a tag."
98+
echo "::error::Please select a tag from the 'Use workflow from' dropdown, not a branch."
99+
exit 1
100+
fi
101+
echo "Validation successful: Running from tag '${{ github.ref_name }}'."
102+
103+
- name: Authenticate to GCP
104+
id: auth
105+
uses: google-github-actions/auth@ba79af03959ebeac9769e648f473a284504d9193 # v2.1.10
106+
with:
107+
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
108+
109+
- name: 'Set image tag to ${{ github.ref_name }}'
110+
id: tag
111+
run: |
112+
TAG=${{ github.ref_name }}
113+
SEMVER_TAG="${TAG#v}"
114+
echo "IMAGE_TAG=$SEMVER_TAG" >> "$GITHUB_OUTPUT"
115+
116+
- name: Deploy to Cloud Run
117+
uses: google-github-actions/deploy-cloudrun@2028e2d7d30a78c6910e0632e48dd561b064884d # v3
118+
with:
119+
service: ${{ env.GCR_SERVICE }}
120+
region: ${{ env.GCR_REGION }}
121+
image: ${{ env.GAR_PATH }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.IMAGE_TAG }}

Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
##############################
2+
## Build bcr-relay
3+
##############################
4+
FROM rust:latest AS rust-builder
5+
6+
WORKDIR /bcr-relay
7+
RUN update-ca-certificates
8+
COPY . .
9+
10+
RUN cargo build --release
11+
12+
##############################
13+
## Create image for docker compose
14+
##############################
15+
FROM ubuntu:22.04
16+
17+
RUN apt-get update && \
18+
apt-get install -y ca-certificates libpq5 && \
19+
apt-get clean
20+
21+
WORKDIR /relay
22+
23+
# Copy binary release
24+
COPY --from=rust-builder /bcr-relay/target/release/bcr-relay ./bcr-relay
25+
COPY --from=rust-builder /bcr-relay/static/ ./static/
26+
27+
RUN chmod +x /relay/bcr-relay
28+
29+
# Expose server port
30+
EXPOSE 8080
31+
32+
CMD ["/relay/bcr-relay", "--listen-address=0.0.0.0:8080"]

0 commit comments

Comments
 (0)