Merge pull request #21338 from dvdksn/fix-releaser-etoomanyredirects

releaser: get redirects from files instead of env vars
This commit is contained in:
David Karlsson 2024-11-06 09:11:26 +01:00 committed by GitHub
commit b6abee8015
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 18 deletions

View File

@ -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

View File

@ -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
}

View File

@ -12,4 +12,5 @@
{{- $redirects.SetInMap "paths" . $target -}}
{{- end -}}
{{- end -}}
{{ $redirects.Get "paths" | jsonify }}
{{- $opts := dict "noHTMLEscape" true }}
{{- $redirects.Get "paths" | jsonify $opts }}