-
-
Notifications
You must be signed in to change notification settings - Fork 3
96 lines (81 loc) · 3.28 KB
/
build-image.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
name: Build and push Docker image
on:
workflow_call:
secrets:
GCP_SA_JSON:
required: true
inputs:
BRANCH:
description: 'The branch to build for staging deployments'
default: main
type: string
ENABLE_PRODUCTION:
description: 'Build production images based on tags'
default: true
type: boolean
env:
STG_PROJECT_ID: stg-pro-397609
# TODO: Replace with the prodcution project ID when that is available
PRD_PROJECT_ID: stg-pro-397609
ARTIFACT_REGISTRY_LOCATION: europe-west4-docker.pkg.dev
ARTIFACT_REGISTRY_NAME: pro-containers
jobs:
docker:
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/${{ github.event.inputs.BRANCH }}' || (${{ github.event.inputs.ENABLE_PRODUCTION }} && startsWith(github.ref, 'refs/tags/')))
steps:
- name: Set environment (Staging)
if: github.ref == 'refs/heads/${{ github.event.inputs.BRANCH }}'
run: |
echo "ENVIRONMENT=staging" >> $GITHUB_ENV
- name: Set environment (Production)
if: startsWith(github.ref, 'refs/tags/')
run: |
echo "ENVIRONMENT=production" >> $GITHUB_ENV
- name: Set project id
run: |
if [[ "${{ env.ENVIRONMENT }}" == "production" ]]; then
echo "PROJECT_ID=${{ env.PRD_PROJECT_ID }}" >> $GITHUB_ENV
elif [[ "${{ env.ENVIRONMENT }}" == "staging" ]]; then
echo "PROJECT_ID=${{ env.STG_PROJECT_ID }}" >> $GITHUB_ENV
fi
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/[email protected]
- name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: ${{ secrets.GCP_SA_JSON }}
- name: Set up Google Cloud SDK
uses: google-github-actions/[email protected]
with:
project_id: ${{ env.PROJECT_ID }}
- name: Configure Docker to use the gcloud command-line tool as a credential helper
run: gcloud --quiet auth configure-docker ${{ env.ARTIFACT_REGISTRY_LOCATION }}
- name: Extract repository name as safe container image name
run: |
raw_name=$(echo "${{ github.repository }}" | awk -F / '{print $2}')
clean_name=$(echo "$raw_name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_.-]//g')
echo "REPO_NAME=$clean_name" >> $GITHUB_ENV
- name: Build and push image (Staging)
if: env.ENVIRONMENT == 'staging'
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: |
${{ env.ARTIFACT_REGISTRY_LOCATION }}/${{ env.PROJECT_ID }}/${{ env.ARTIFACT_REGISTRY_NAME }}/${{ env.REPO_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push image (Production)
if: env.ENVIRONMENT == 'production'
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: |
${{ env.ARTIFACT_REGISTRY_LOCATION }}/${{ env.PROJECT_ID }}/${{ env.ARTIFACT_REGISTRY_NAME }}/${{ env.REPO_NAME }}:${{ github.ref_name }}
${{ env.ARTIFACT_REGISTRY_LOCATION }}/${{ env.PROJECT_ID }}/${{ env.ARTIFACT_REGISTRY_NAME }}/${{ env.REPO_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max