From 0a3f9780ce487e9ab750b4f2e6943c35847557d1 Mon Sep 17 00:00:00 2001 From: David Karlsson <35727626+dvdksn@users.noreply.github.com> Date: Tue, 5 Nov 2024 11:53:19 +0100 Subject: [PATCH] releaser: get redirects and lambda fn from files instead of env vars Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com> --- _releaser/Dockerfile | 6 +++--- _releaser/aws.go | 23 +++++++++-------------- layouts/index.redirects.json | 3 ++- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/_releaser/Dockerfile b/_releaser/Dockerfile index 5b945dd596..9d15b86c4f 100644 --- a/_releaser/Dockerfile +++ b/_releaser/Dockerfile @@ -5,7 +5,7 @@ ARG GO_VERSION=1.19 FROM scratch AS sitedir FROM golang:${GO_VERSION}-alpine AS base -RUN apk add --no-cache jq openssl +RUN apk add --no-cache openssl ENV CGO_ENABLED=0 WORKDIR /src COPY go.mod go.sum ./ @@ -49,6 +49,6 @@ RUN --mount=type=bind,target=. \ --mount=type=secret,id=AWS_SECRET_ACCESS_KEY \ --mount=type=secret,id=AWS_SESSION_TOKEN \ AWS_LAMBDA_FUNCTION_FILE=cloudfront-lambda-redirects.js \ - REDIRECTS_JSON=$(jq -c '.' /site/redirects.json) \ - REDIRECTS_PREFIXES_JSON=$(jq -c '.' redirects-prefixes.json) \ + REDIRECTS_FILE="/site/redirects.json" \ + REDIRECTS_PREFIXES_FILE="redirects-prefixes.json" \ releaser aws cloudfront-update diff --git a/_releaser/aws.go b/_releaser/aws.go index 3f79c2bd2e..5e1714310e 100644 --- a/_releaser/aws.go +++ b/_releaser/aws.go @@ -98,17 +98,17 @@ func (s *AwsLambdaInvokeCmd) Run() error { type AwsCloudfrontUpdateCmd struct { Region string `kong:"name='region',env='AWS_REGION'"` Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"` - FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"` + FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE',type=filecontent"` CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"` - RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"` - RedirectsPrefixesJSON string `kong:"name='redirects-prefixes-json',env='REDIRECTS_PREFIXES_JSON'"` + RedirectsFile string `kong:"name='redirects-file',env='REDIRECTS_FILE',type=filecontent"` + RedirectsPrefixesFile string `kong:"name='redirects-prefixes-file',env='REDIRECTS_PREFIXES_FILE',type=filecontent"` } func (s *AwsCloudfrontUpdateCmd) Run() error { var err error ver := time.Now().UTC().Format(time.RFC3339) - zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON, s.RedirectsPrefixesJSON) + zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsFile, s.RedirectsPrefixesFile) if err != nil { return fmt.Errorf("cannot create lambda function zip: %w", err) } @@ -228,20 +228,15 @@ func (s *AwsCloudfrontUpdateCmd) Run() error { return nil } -func getLambdaFunctionZip(funcFilename string, redirectsJSON string, redirectsPrefixesJSON string) ([]byte, error) { - funcdt, err := os.ReadFile(funcFilename) - if err != nil { - return nil, fmt.Errorf("failed to read lambda function file %q: %w", funcFilename, err) - } - +func getLambdaFunctionZip(funcdt, redirects, redirectsPrefixes string) ([]byte, error) { var funcbuf bytes.Buffer - functpl := template.Must(template.New("").Parse(string(funcdt))) - if err = functpl.Execute(&funcbuf, struct { + functpl := template.Must(template.New("").Parse(funcdt)) + if err := functpl.Execute(&funcbuf, struct { RedirectsJSON string RedirectsPrefixesJSON string }{ - redirectsJSON, - redirectsPrefixesJSON, + redirects, + redirectsPrefixes, }); err != nil { return nil, err } diff --git a/layouts/index.redirects.json b/layouts/index.redirects.json index 8136b17c13..abf7111aca 100644 --- a/layouts/index.redirects.json +++ b/layouts/index.redirects.json @@ -12,4 +12,5 @@ {{- $redirects.SetInMap "paths" . $target -}} {{- end -}} {{- end -}} -{{ $redirects.Get "paths" | jsonify }} +{{- $opts := dict "noHTMLEscape" true }} +{{- $redirects.Get "paths" | jsonify $opts }}