-
-
Notifications
You must be signed in to change notification settings - Fork 8
116 lines (101 loc) · 4.24 KB
/
deploy-image-to-gcp.yml
File metadata and controls
116 lines (101 loc) · 4.24 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
name: Build & Push Docker Image (GCP)
on:
workflow_call:
outputs:
image:
description: "The Docker image with SHA tag"
value: ${{ jobs.run.outputs.image }}
version:
description: "The Docker image version tag"
value: ${{ jobs.run.outputs.version }}
permissions:
id-token: write
jobs:
run:
name: Run
runs-on: ubuntu-latest
outputs:
image: ${{ vars.DOCKER_IMAGE }}:sha-${{ github.sha }}
version: ${{ steps.traffic-analytics-docker-metadata.outputs.version }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
fetch-tags: true
- name: Get tree SHA
id: tree
run: echo "sha=$(git rev-parse HEAD^{tree})" >> $GITHUB_OUTPUT
- name: Output tree SHA
run: |
echo "Tree SHA: ${{ steps.tree.outputs.sha }}"
- name: Pull already built docker image
id: pull-cache
env:
IMAGE_NAME: ghcr.io/${{ github.repository }}:tree-${{ steps.tree.outputs.sha }}
run: |
IMAGE_NAME_LOWER=$(echo "$IMAGE_NAME" | tr '[:upper:]' '[:lower:]')
if docker pull "$IMAGE_NAME_LOWER"; then
echo "cache-hit=true" >> $GITHUB_OUTPUT
echo "cached-image=$IMAGE_NAME_LOWER" >> $GITHUB_OUTPUT
else
echo "cache-hit=false" >> $GITHUB_OUTPUT
echo "Image not found, proceeding to build."
fi
- name: Extract node version
id: extract-node-version
if: steps.pull-cache.outputs.cache-hit != 'true'
uses: ./.github/actions/extract-node-version
- name: Build Docker Image
id: docker-build
if: steps.pull-cache.outputs.cache-hit != 'true'
uses: ./.github/actions/docker-build
with:
node-version: ${{ fromJSON(steps.extract-node-version.outputs.node-version) }}
- name: Lint & Test
if: steps.pull-cache.outputs.cache-hit != 'true'
uses: ./.github/actions/lint-and-test
with:
docker-image: ${{ steps.docker-build.outputs.base-image-name }}
- name: "Auth with Google Cloud"
id: gcp-auth
uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3
with:
project_id: ${{ vars.GCP_PROJECT_ID }}
token_format: access_token
workload_identity_provider: ${{ vars.GCP_WORKFLOW_IDENTITY_PROVIDER }}
service_account: gh-artifact-manager@ghost-traffic-analytics.iam.gserviceaccount.com
- name: "Login to GCP Artifact Registry"
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4
with:
registry: europe-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.gcp-auth.outputs.access_token }}
- name: Get Version Tag
id: get-version-tag
run: |
TAG=$(git tag --points-at HEAD | grep -E '^v[0-9.]+' | sort -r | head -n 1)
echo "version-tag=$TAG" >> $GITHUB_OUTPUT
- name: Generate Docker Tags
id: traffic-analytics-docker-metadata
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6
with:
images: |
${{ vars.DOCKER_IMAGE }}
tags: |
${{ github.ref == 'refs/heads/main' && 'type=edge,branch=main' || '' }}
type=semver,pattern={{version}},value=${{ steps.get-version-tag.outputs.version-tag }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.get-version-tag.outputs.version-tag }}
type=semver,pattern={{major}},value=${{ steps.get-version-tag.outputs.version-tag }}
type=sha,priority=1100
type=sha,format=long
- name: Retag & Push Docker Image
env:
SOURCE_IMAGE: ${{ steps.pull-cache.outputs.cache-hit == 'true' && steps.pull-cache.outputs.cached-image || steps.docker-build.outputs.production-image-name }}
run: |
for tag in $(echo "${{ steps.traffic-analytics-docker-metadata.outputs.tags }}"); do
echo "Tagging: $tag"
docker tag $SOURCE_IMAGE $tag
done
for tag in $(echo "${{ steps.traffic-analytics-docker-metadata.outputs.tags }}"); do
echo "Pushing: $tag"
docker push $tag
done