Skip to content

Commit 2d73838

Browse files
committed
Adopt the CDN deploy action from navikt/frontend
co-authored-by: @tronghn
1 parent 354b949 commit 2d73838

File tree

1 file changed

+56
-75
lines changed

1 file changed

+56
-75
lines changed

actions/cdn-upload/v2/action.yaml

+56-75
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,92 @@
11
name: "Upload to CDN"
22
description: "Upload static assets to CDN"
33
inputs:
4-
team-name:
5-
description: "CDN team name"
4+
team:
5+
description: "Team slug"
6+
required: true
7+
tenant:
8+
description: "Tenant slug"
9+
default: "nav"
610
required: true
711
source:
812
description: "Source directory"
913
required: true
1014
destination:
1115
description: "Destination directory"
1216
required: true
13-
cache-invalidation:
17+
source_keep_parent_name:
18+
description: "Keep parent directory name when uploading"
19+
required: false
20+
default: "true"
21+
cache_invalidation:
1422
description: "Cache invalidation"
1523
required: false
1624
default: "false"
17-
no-cache-paths:
25+
no_cache_paths:
1826
description: "Comma separated list of paths that should not be cached"
1927
required: false
2028
default: ""
21-
nais_management_project_id:
29+
project_id:
30+
description: "Google Cloud project ID where buckets are hosted"
2231
required: true
23-
nais_workload_identity_provider:
32+
identity_provider:
33+
description: "Google Workload Identity Provider"
2434
required: true
2535

2636
outputs:
2737
uploaded:
2838
description: "Uploaded files"
2939
value: ${{ steps.upload-file.outputs.uploaded }}
40+
3041
runs:
3142
using: "composite"
3243
steps:
3344
- id: "cdn"
3445
shell: bash
3546
run: |
36-
if [ -z "${{ inputs.team-name }}" ]; then
47+
if [ -z "${{ inputs.team }}" ]; then
3748
echo "::error ::team not set. Please provide as input."
3849
exit 1
3950
fi
4051
41-
function slugify() {
42-
slug=${{ inputs.team-name }}
43-
prefix="$1"
44-
maxLength=30
45-
46-
hash=$(echo -n "$slug" | sha256sum | cut -d ' ' -f 1)
47-
52+
function slug_hash_prefix_truncate() {
53+
# synopsis:
54+
#
55+
# slug_hash_prefix_truncate kimfoo nais-cdn 30
56+
# or
57+
# slug_hash_prefix_truncate nav-kimfoo cdn 30
58+
#
59+
# when editing this code, make sure its output corresponds with
60+
# SlugHashPrefixTruncate from the api-reconcilers project.
61+
62+
tenantTeam="$1"
63+
prefix="$2"
64+
maxLength="$3"
65+
66+
# hash is the first 4 characters of the sha256sum of the part that gets truncated.
67+
hash=$(echo -n "${tenantTeam}" | sha256sum | cut -d ' ' -f 1 | cut -b 1-4)
68+
69+
# truncate the middle part (not tenant nor prefix)
70+
# for a total output string length of $maxLength.
4871
prefixLength=${#prefix}
49-
hashLength=4
50-
slugLength=$((maxLength - prefixLength - hashLength - 2))
72+
maxLength=$((maxLength - prefixLength - hashLength - 2))
73+
truncatedTenantTeam=$(echo -n "${tenantTeam:0:$maxLength}")
5174
52-
# Remove hyphens only if they are at the end after truncation
53-
truncatedSlug=$(echo -n "${slug:0:$slugLength}" | sed 's/-\}$//')
54-
55-
# Generate a new truncated hash for each call
56-
truncatedHash=$(echo -n "$hash" | head -c $hashLength)
57-
58-
echo "$truncatedSlug-$truncatedHash"
75+
echo "$prefix-$truncatedTenantTeam-$hash"
5976
}
6077
78+
principal=$(slug_hash_prefix_truncate ${{ inputs.team }} "cdn" 30)
79+
bucket_name=$(slug_hash_prefix_truncate "${{ inputs.tenant }}-${{ inputs.team }}" "nais-cdn" 63)
6180
62-
function slugifyBucket() {
63-
slug="$1"
64-
65-
hash=$(echo -n "$slug" | sha256sum | cut -d ' ' -f 1)
66-
67-
hashLength=4
68-
69-
# Remove hyphens only if they are at the end after truncation
70-
truncatedSlug=$(echo -n $slug | sed 's/-\}$//')
81+
echo "SA_EMAIL=${principal}@${{ inputs.project_id }}.iam.gserviceaccount.com" >> $GITHUB_ENV
82+
echo "BUCKET_NAME=${bucket_name}" >> $GITHUB_ENV
7183
72-
# Generate a new truncated hash for each call
73-
truncatedHash=$(echo -n "$hash" | head -c $hashLength)
74-
75-
echo "$truncatedSlug-$truncatedHash"
76-
}
77-
78-
echo "LOGICAL_NAME=nais-cdn" >> $GITHUB_ENV
79-
slugify "cdn"
80-
slug_result=$(slugify "cdn")
81-
echo "SA_EMAIL=cdn-$slug_result@${{ inputs.nais_management_project_id }}.iam.gserviceaccount.com" >> $GITHUB_ENV
82-
83-
# TODO: tenant-domain is currently hard-coded
84-
# nais-cdn-{replace(tenant-domain, '.', '-')}-{team-name}-{hash}
85-
slugifyBucket "nav-${{ inputs.team-name }}"
86-
slug_result=$(slugifyBucket "nav-${{ inputs.team-name }}")
87-
echo "BUCKET_NAME=nais-cdn-$slug_result" >> $GITHUB_ENV
8884
# Authenticate with Google Cloud using Workload Identity Federation
8985
- id: "auth"
9086
name: "Authenticate to Google Cloud"
9187
uses: "google-github-actions/[email protected]"
9288
with:
93-
workload_identity_provider: ${{ inputs.nais_workload_identity_provider }}
89+
workload_identity_provider: ${{ inputs.identity_provider }}
9490
service_account: ${{ env.SA_EMAIL }}
9591
token_format: "access_token"
9692

@@ -102,7 +98,7 @@ runs:
10298
::error ::Failed to authenticate to Google Cloud.
10399
EOF
104100
105-
echo "Ensure that your team has write access to the Github-repository." >> $GITHUB_STEP_SUMMARY
101+
echo "Ensure that your team has write access to the GitHub repository." >> $GITHUB_STEP_SUMMARY
106102
echo "Ensure that you grant the following permissions in your workflow:" >> $GITHUB_STEP_SUMMARY
107103
echo '```yaml' >> $GITHUB_STEP_SUMMARY
108104
echo "permissions:" >> $GITHUB_STEP_SUMMARY
@@ -115,43 +111,28 @@ runs:
115111
uses: "google-github-actions/upload-cloud-storage@v2"
116112
with:
117113
path: "${{ inputs.source }}"
118-
parent: "true"
119-
destination: "${{ env.BUCKET_NAME }}/${{ inputs.team-name }}/${{ inputs.destination }}"
114+
parent: '${{ inputs.source_keep_parent_name }}'
115+
destination: "${{ env.BUCKET_NAME }}/${{ inputs.team }}/${{ inputs.destination }}"
120116

121-
# Invalidate cache if cache-invalidation is set to true
117+
# Invalidate cache if cache_invalidation is set to true
122118
- name: "Set up Cloud SDK"
123-
if: ${{ inputs.cache-invalidation == 'true' || inputs.no-cache-paths != '' }}
119+
if: ${{ inputs.cache_invalidation == 'true' || inputs.no_cache_paths != '' }}
124120
uses: "google-github-actions/setup-gcloud@v1"
125121
- name: "Invalidating cache"
126-
if: ${{ inputs.cache-invalidation == 'true' }}
127-
shell: bash
128-
run: |
129-
path="/${{ inputs.team-name }}/${{ inputs.destination }}"
130-
path="${path%/}/*"
131-
132-
gcloud compute url-maps invalidate-cdn-cache ${{ env.LOGICAL_NAME }} --global --async --path $path
133-
- if: ${{ inputs.cache-invalidation == 'true' }}
122+
if: ${{ inputs.cache_invalidation == 'true' }}
134123
shell: bash
135124
run: |
136-
path="/${{ inputs.team-name }}/${{ inputs.destination }}"
125+
path="/${{ inputs.team }}/${{ inputs.destination }}"
137126
path="${path%/}/*"
138127
139-
base_url="https://console.cloud.google.com/net-services/loadbalancing/details/httpAdvanced"
140-
console_url="$base_url/${{ env.LOGICAL_NAME }}?project=${{ inputs.nais_management_project_id }}"
141-
142-
echo "### CDN Cache Invalidation" >> $GITHUB_STEP_SUMMARY
143-
echo "Path: \`$path\`" >> $GITHUB_STEP_SUMMARY
144-
echo "Cache invalidation is running in the background. It may take up to `15 minutes` \
145-
before the cache is invalidated. You can check the status of the invalidation in \
146-
[Google Cloud Console]($console_url) → Caching." >> $GITHUB_STEP_SUMMARY
147-
128+
gcloud compute url-maps invalidate-cdn-cache nais-cdn --global --async --path $path
148129
- name: Set no-cache metadata
149-
if: ${{ inputs.no-cache-paths != '' }}
130+
if: ${{ inputs.no_cache_paths != '' }}
150131
shell: bash
151132
run: |
152-
paths=(${{ inputs.no-cache-paths }})
133+
paths=(${{ inputs.no_cache_paths }})
153134
IFS=','
154135
155136
for path in $paths; do
156-
gsutil setmeta -h "Cache-Control:no-store" "gs://${BUCKET_NAME}/${{ inputs.team-name }}/$path"
137+
gsutil setmeta -h "Cache-Control:no-store" "gs://${BUCKET_NAME}/${{ inputs.team }}/$path"
157138
done

0 commit comments

Comments
 (0)