1
1
name : " Upload to CDN"
2
2
description : " Upload static assets to CDN"
3
3
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"
6
10
required : true
7
11
source :
8
12
description : " Source directory"
9
13
required : true
10
14
destination :
11
15
description : " Destination directory"
12
16
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 :
14
22
description : " Cache invalidation"
15
23
required : false
16
24
default : " false"
17
- no-cache-paths :
25
+ no_cache_paths :
18
26
description : " Comma separated list of paths that should not be cached"
19
27
required : false
20
28
default : " "
21
- nais_management_project_id :
29
+ project_id :
30
+ description : " Google Cloud project ID where buckets are hosted"
22
31
required : true
23
- nais_workload_identity_provider :
32
+ identity_provider :
33
+ description : " Google Workload Identity Provider"
24
34
required : true
25
35
26
36
outputs :
27
37
uploaded :
28
38
description : " Uploaded files"
29
39
value : ${{ steps.upload-file.outputs.uploaded }}
40
+
30
41
runs :
31
42
using : " composite"
32
43
steps :
33
44
- id : " cdn"
34
45
shell : bash
35
46
run : |
36
- if [ -z "${{ inputs.team-name }}" ]; then
47
+ if [ -z "${{ inputs.team }}" ]; then
37
48
echo "::error ::team not set. Please provide as input."
38
49
exit 1
39
50
fi
40
51
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.
48
71
prefixLength=${#prefix}
49
- hashLength=4
50
- slugLength =$((maxLength - prefixLength - hashLength - 2) )
72
+ maxLength=$((maxLength - prefixLength - hashLength - 2))
73
+ truncatedTenantTeam =$(echo -n "${tenantTeam:0:$maxLength}" )
51
74
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"
59
76
}
60
77
78
+ principal=$(slug_hash_prefix_truncate ${{ inputs.team }} "cdn" 30)
79
+ bucket_name=$(slug_hash_prefix_truncate "${{ inputs.tenant }}-${{ inputs.team }}" "nais-cdn" 63)
61
80
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
71
83
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
88
84
# Authenticate with Google Cloud using Workload Identity Federation
89
85
- id : " auth"
90
86
name : " Authenticate to Google Cloud"
91
87
uses :
" google-github-actions/[email protected] "
92
88
with :
93
- workload_identity_provider : ${{ inputs.nais_workload_identity_provider }}
89
+ workload_identity_provider : ${{ inputs.identity_provider }}
94
90
service_account : ${{ env.SA_EMAIL }}
95
91
token_format : " access_token"
96
92
102
98
::error ::Failed to authenticate to Google Cloud.
103
99
EOF
104
100
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
106
102
echo "Ensure that you grant the following permissions in your workflow:" >> $GITHUB_STEP_SUMMARY
107
103
echo '```yaml' >> $GITHUB_STEP_SUMMARY
108
104
echo "permissions:" >> $GITHUB_STEP_SUMMARY
@@ -115,43 +111,28 @@ runs:
115
111
uses : " google-github-actions/upload-cloud-storage@v2"
116
112
with :
117
113
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 }}"
120
116
121
- # Invalidate cache if cache-invalidation is set to true
117
+ # Invalidate cache if cache_invalidation is set to true
122
118
- 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 != '' }}
124
120
uses : " google-github-actions/setup-gcloud@v1"
125
121
- 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' }}
134
123
shell : bash
135
124
run : |
136
- path="/${{ inputs.team-name }}/${{ inputs.destination }}"
125
+ path="/${{ inputs.team }}/${{ inputs.destination }}"
137
126
path="${path%/}/*"
138
127
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
148
129
- name : Set no-cache metadata
149
- if : ${{ inputs.no-cache-paths != '' }}
130
+ if : ${{ inputs.no_cache_paths != '' }}
150
131
shell : bash
151
132
run : |
152
- paths=(${{ inputs.no-cache-paths }})
133
+ paths=(${{ inputs.no_cache_paths }})
153
134
IFS=','
154
135
155
136
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"
157
138
done
0 commit comments