@@ -36,17 +36,103 @@ inputs:
36
36
outputs :
37
37
uploaded :
38
38
description : " Uploaded files"
39
- value : ${{ steps.legacy .outputs.uploaded }}
39
+ value : ${{ steps.upload-file .outputs.uploaded }}
40
40
41
41
runs :
42
42
using : " composite"
43
43
steps :
44
- - id : legacy
45
- uses : " navikt/frontend/actions/cdn-upload/v1@main"
44
+ - id : " cdn"
45
+ shell : bash
46
+ run : |
47
+ if [ -z "${{ inputs.team }}" ]; then
48
+ echo "::error ::team not set. Please provide as input."
49
+ exit 1
50
+ fi
51
+
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.
71
+ prefixLength=${#prefix}
72
+ maxLength=$((maxLength - prefixLength - hashLength - 2))
73
+ truncatedTenantTeam=$(echo -n "${tenantTeam:0:$maxLength}")
74
+
75
+ echo "$prefix-$truncatedTenantTeam-$hash"
76
+ }
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)
80
+
81
+ echo "SA_EMAIL=${principal}@${{ inputs.project_id }}.iam.gserviceaccount.com" >> $GITHUB_ENV
82
+ echo "BUCKET_NAME=${bucket_name}" >> $GITHUB_ENV
83
+
84
+ # Authenticate with Google Cloud using Workload Identity Federation
85
+ - id : " auth"
86
+ name : " Authenticate to Google Cloud"
87
+ uses :
" google-github-actions/[email protected] "
46
88
with :
47
- cdn-team-name : ${{ inputs.team }}
48
- source : ${{ inputs.source }}
49
- destination : ${{ inputs.destination }}
50
- source-keep-parent-name : ${{ inputs.source_keep_parent_name }}
51
- cache-invalidation : ${{ inputs.cache_invalidation }}
52
- no-cache-paths : ${{ inputs.no_cache_paths }}
89
+ workload_identity_provider : ${{ inputs.identity_provider }}
90
+ service_account : ${{ env.SA_EMAIL }}
91
+ token_format : " access_token"
92
+
93
+ - name : " Handle authentication failure"
94
+ if : ${{ failure() && steps.auth.outcome == 'failure' }}
95
+ shell : bash
96
+ run : |
97
+ cat <<EOF
98
+ ::error ::Failed to authenticate to Google Cloud.
99
+ EOF
100
+
101
+ echo "Ensure that your team has write access to the GitHub repository." >> $GITHUB_STEP_SUMMARY
102
+ echo "Ensure that you grant the following permissions in your workflow:" >> $GITHUB_STEP_SUMMARY
103
+ echo '```yaml' >> $GITHUB_STEP_SUMMARY
104
+ echo "permissions:" >> $GITHUB_STEP_SUMMARY
105
+ echo " contents: read" >> $GITHUB_STEP_SUMMARY
106
+ echo " id-token: write" >> $GITHUB_STEP_SUMMARY
107
+ echo '```' >> $GITHUB_STEP_SUMMARY
108
+
109
+ # Upload files to Google Cloud Storage Bucket connected to CDN
110
+ - id : " upload-file"
111
+ uses : " google-github-actions/upload-cloud-storage@v2"
112
+ with :
113
+ path : " ${{ inputs.source }}"
114
+ parent : ' ${{ inputs.source_keep_parent_name }}'
115
+ destination : " ${{ env.BUCKET_NAME }}/${{ inputs.team }}/${{ inputs.destination }}"
116
+
117
+ # Invalidate cache if cache_invalidation is set to true
118
+ - name : " Set up Cloud SDK"
119
+ if : ${{ inputs.cache_invalidation == 'true' || inputs.no_cache_paths != '' }}
120
+ uses : " google-github-actions/setup-gcloud@v1"
121
+ - name : " Invalidating cache"
122
+ if : ${{ inputs.cache_invalidation == 'true' }}
123
+ shell : bash
124
+ run : |
125
+ path="/${{ inputs.team }}/${{ inputs.destination }}"
126
+ path="${path%/}/*"
127
+
128
+ gcloud compute url-maps invalidate-cdn-cache nais-cdn --global --async --path $path
129
+ - name : Set no-cache metadata
130
+ if : ${{ inputs.no_cache_paths != '' }}
131
+ shell : bash
132
+ run : |
133
+ paths=(${{ inputs.no_cache_paths }})
134
+ IFS=','
135
+
136
+ for path in $paths; do
137
+ gsutil setmeta -h "Cache-Control:no-store" "gs://${BUCKET_NAME}/${{ inputs.team }}/$path"
138
+ done
0 commit comments