Skip to content

Commit b9833dd

Browse files
authored
Merge pull request #21353 from dvdksn/fix-releaser-etoomanyredirects-take2
releaser: fix error with too many (redirect) args to jq
2 parents 1c78c4e + db6966c commit b9833dd

File tree

5 files changed

+87
-34
lines changed

5 files changed

+87
-34
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,30 @@ permissions:
1515
contents: read # to fetch code (actions/checkout)
1616

1717
jobs:
18+
releaser:
19+
runs-on: ubuntu-24.04
20+
steps:
21+
-
22+
name: Checkout
23+
uses: actions/checkout@v4
24+
-
25+
name: Set up Docker Buildx
26+
uses: docker/setup-buildx-action@v3
27+
-
28+
name: Build
29+
uses: docker/bake-action@v5
30+
with:
31+
files: |
32+
docker-bake.hcl
33+
targets: releaser-build
34+
set: |
35+
*.cache-from=type=gha,scope=releaser
36+
*.cache-to=type=gha,scope=releaser,mode=max
37+
1838
build:
1939
runs-on: ubuntu-24.04
40+
needs:
41+
- releaser
2042
steps:
2143
-
2244
name: Checkout
@@ -34,6 +56,16 @@ jobs:
3456
set: |
3557
*.cache-from=type=gha,scope=build
3658
*.cache-to=type=gha,scope=build,mode=max
59+
-
60+
name: Check Cloudfront config
61+
uses: docker/bake-action@v5
62+
with:
63+
targets: aws-cloudfront-update
64+
env:
65+
DRY_RUN: true
66+
AWS_REGION: us-east-1
67+
AWS_CLOUDFRONT_ID: 0123456789ABCD
68+
AWS_LAMBDA_FUNCTION: DockerDocsRedirectFunction-dummy
3769

3870
vale:
3971
if: ${{ github.event_name == 'pull_request' }}
@@ -76,25 +108,3 @@ jobs:
76108
*.cache-to=type=gha,scope=validate-${{ matrix.target }},mode=max
77109
*.cache-from=type=gha,scope=validate-${{ matrix.target }}
78110
*.cache-from=type=gha,scope=build
79-
80-
# build-releaser builds the _releaser used for AWS deployment in publish workflow.
81-
# It's just to be sure it builds correctly.
82-
build-releaser:
83-
runs-on: ubuntu-24.04
84-
steps:
85-
-
86-
name: Checkout
87-
uses: actions/checkout@v4
88-
-
89-
name: Set up Docker Buildx
90-
uses: docker/setup-buildx-action@v3
91-
-
92-
name: Build
93-
uses: docker/bake-action@v5
94-
with:
95-
files: |
96-
docker-bake.hcl
97-
targets: releaser-build
98-
set: |
99-
*.cache-from=type=gha,scope=releaser
100-
*.cache-to=type=gha,scope=releaser,mode=max

_releaser/Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ARG GO_VERSION=1.19
55
FROM scratch AS sitedir
66

77
FROM golang:${GO_VERSION}-alpine AS base
8-
RUN apk add --no-cache jq openssl
8+
RUN apk add --no-cache openssl
99
ENV CGO_ENABLED=0
1010
WORKDIR /src
1111
COPY go.mod go.sum ./
@@ -19,6 +19,7 @@ RUN --mount=type=bind,target=. \
1919
go build -o /out/releaser .
2020

2121
FROM base AS aws-s3-update-config
22+
ARG DRY_RUN=false
2223
ARG AWS_REGION
2324
ARG AWS_S3_BUCKET
2425
ARG AWS_S3_CONFIG
@@ -30,6 +31,7 @@ RUN --mount=type=bind,target=. \
3031
releaser aws s3-update-config
3132

3233
FROM base AS aws-lambda-invoke
34+
ARG DRY_RUN=false
3335
ARG AWS_REGION
3436
ARG AWS_LAMBDA_FUNCTION
3537
RUN --mount=type=bind,from=releaser,source=/out/releaser,target=/usr/bin/releaser \
@@ -39,16 +41,17 @@ RUN --mount=type=bind,from=releaser,source=/out/releaser,target=/usr/bin/release
3941
releaser aws lambda-invoke
4042

4143
FROM base AS aws-cloudfront-update
44+
ARG DRY_RUN=false
4245
ARG AWS_REGION
4346
ARG AWS_LAMBDA_FUNCTION
4447
ARG AWS_CLOUDFRONT_ID
48+
ARG AWS_LAMBDA_FUNCTION_FILE="cloudfront-lambda-redirects.js"
49+
ARG REDIRECTS_FILE="/site/redirects.json"
50+
ARG REDIRECTS_PREFIXES_FILE="redirects-prefixes.json"
4551
RUN --mount=type=bind,target=. \
4652
--mount=type=bind,from=sitedir,target=/site \
4753
--mount=type=bind,from=releaser,source=/out/releaser,target=/usr/bin/releaser \
4854
--mount=type=secret,id=AWS_ACCESS_KEY_ID \
4955
--mount=type=secret,id=AWS_SECRET_ACCESS_KEY \
5056
--mount=type=secret,id=AWS_SESSION_TOKEN \
51-
AWS_LAMBDA_FUNCTION_FILE=cloudfront-lambda-redirects.js \
52-
REDIRECTS_JSON=$(jq -c '.' /site/redirects.json) \
53-
REDIRECTS_PREFIXES_JSON=$(jq -c '.' redirects-prefixes.json) \
5457
releaser aws cloudfront-update

_releaser/aws.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ type AwsS3UpdateConfigCmd struct {
3030
Region string `kong:"name='region',env='AWS_REGION'"`
3131
S3Bucket string `kong:"name='s3-bucket',env='AWS_S3_BUCKET'"`
3232
S3Config string `kong:"name='s3-website-config',env='AWS_S3_CONFIG'"`
33+
DryRun bool `kong:"name='dry-run',env='DRY_RUN'"`
3334
}
3435

3536
func (s *AwsS3UpdateConfigCmd) Run() error {
37+
if s.DryRun {
38+
log.Printf("INFO: Dry run mode enabled. Configuration:\nRegion: %s\nS3Bucket: %s\nS3Config: %s\n", s.Region, s.S3Bucket, s.S3Config)
39+
return nil
40+
}
41+
3642
file, err := os.ReadFile(s.S3Config)
3743
if err != nil {
3844
return fmt.Errorf("failed to read s3 config file %s: %w", s.S3Config, err)
@@ -74,9 +80,15 @@ func (s *AwsS3UpdateConfigCmd) Run() error {
7480
type AwsLambdaInvokeCmd struct {
7581
Region string `kong:"name='region',env='AWS_REGION'"`
7682
LambdaFunction string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"`
83+
DryRun bool `kong:"name='dry-run',env='DRY_RUN'"`
7784
}
7885

7986
func (s *AwsLambdaInvokeCmd) Run() error {
87+
if s.DryRun {
88+
log.Printf("INFO: Dry run mode enabled. Configuration:\nRegion: %s\nLambdaFunction: %s\n", s.Region, s.LambdaFunction)
89+
return nil
90+
}
91+
8092
svc := lambda.New(session.Must(session.NewSessionWithOptions(session.Options{
8193
SharedConfigState: session.SharedConfigEnable,
8294
})), &aws.Config{
@@ -100,19 +112,26 @@ type AwsCloudfrontUpdateCmd struct {
100112
Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"`
101113
FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"`
102114
CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"`
103-
RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"`
104-
RedirectsPrefixesJSON string `kong:"name='redirects-prefixes-json',env='REDIRECTS_PREFIXES_JSON'"`
115+
RedirectsFile string `kong:"name='redirects-file',env='REDIRECTS_FILE'"`
116+
RedirectsPrefixesFile string `kong:"name='redirects-prefixes-file',env='REDIRECTS_PREFIXES_FILE'"`
117+
DryRun bool `kong:"name='dry-run',env='DRY_RUN'"`
105118
}
106119

107120
func (s *AwsCloudfrontUpdateCmd) Run() error {
108121
var err error
109122
ver := time.Now().UTC().Format(time.RFC3339)
110123

111-
zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON, s.RedirectsPrefixesJSON)
124+
zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsFile, s.RedirectsPrefixesFile, s.DryRun)
112125
if err != nil {
113126
return fmt.Errorf("cannot create lambda function zip: %w", err)
114127
}
115128

129+
if s.DryRun {
130+
log.Printf("INFO: Dry run mode enabled. Configuration:\nRegion: %s\nFunction: %s\nFunctionFile: %s\nCloudfrontID: %s\nRedirectsFile: %s\nRedirectsPrefixesFile: %s\n",
131+
s.Region, s.Function, s.FunctionFile, s.CloudfrontID, s.RedirectsFile, s.RedirectsPrefixesFile)
132+
return nil
133+
}
134+
116135
svc := lambda.New(session.Must(session.NewSessionWithOptions(session.Options{
117136
SharedConfigState: session.SharedConfigEnable,
118137
})), &aws.Config{
@@ -228,24 +247,39 @@ func (s *AwsCloudfrontUpdateCmd) Run() error {
228247
return nil
229248
}
230249

231-
func getLambdaFunctionZip(funcFilename string, redirectsJSON string, redirectsPrefixesJSON string) ([]byte, error) {
250+
func getLambdaFunctionZip(funcFilename, redirectsFile, redirectsPrefixesFile string, dryrun bool) ([]byte, error) {
232251
funcdt, err := os.ReadFile(funcFilename)
233252
if err != nil {
234253
return nil, fmt.Errorf("failed to read lambda function file %q: %w", funcFilename, err)
235254
}
236255

256+
redirects, err := os.ReadFile(redirectsFile)
257+
if err != nil {
258+
return nil, fmt.Errorf("failed to read redirects file %q: %w", redirectsFile, err)
259+
}
260+
261+
redirectsPrefixes, err := os.ReadFile(redirectsPrefixesFile)
262+
if err != nil {
263+
return nil, fmt.Errorf("failed to read redirects prefixes file %q: %w", redirectsPrefixesFile, err)
264+
}
265+
237266
var funcbuf bytes.Buffer
238267
functpl := template.Must(template.New("").Parse(string(funcdt)))
239268
if err = functpl.Execute(&funcbuf, struct {
240269
RedirectsJSON string
241270
RedirectsPrefixesJSON string
242271
}{
243-
redirectsJSON,
244-
redirectsPrefixesJSON,
272+
string(redirects),
273+
string(redirectsPrefixes),
245274
}); err != nil {
246275
return nil, err
247276
}
248277

278+
if dryrun {
279+
log.Printf("INFO: Dry run mode enabled. Lambda Function Definition:\n\n%s\n", funcbuf.String())
280+
return nil, nil
281+
}
282+
249283
tmpdir, err := os.MkdirTemp("", "lambda-zip")
250284
if err != nil {
251285
return nil, err

docker-bake.hcl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ variable "DOCS_SITE_DIR" {
1010
default = "public"
1111
}
1212

13+
variable "DRY_RUN" {
14+
default = null
15+
}
16+
1317
group "default" {
1418
targets = ["release"]
1519
}
@@ -98,6 +102,7 @@ variable "AWS_LAMBDA_FUNCTION" {
98102

99103
target "_common-aws" {
100104
args = {
105+
DRY_RUN = DRY_RUN
101106
AWS_REGION = AWS_REGION
102107
AWS_S3_BUCKET = AWS_S3_BUCKET
103108
AWS_S3_CONFIG = AWS_S3_CONFIG

layouts/index.redirects.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ $redirects := newScratch }}
1+
{{- $redirects := newScratch }}
22
{{- range $i, $e := site.AllPages -}}
33
{{- if .Params.aliases -}}
44
{{- $target := .RelPermalink -}}
@@ -12,4 +12,5 @@
1212
{{- $redirects.SetInMap "paths" . $target -}}
1313
{{- end -}}
1414
{{- end -}}
15-
{{ $redirects.Get "paths" | jsonify }}
15+
{{- $opts := dict "noHTMLEscape" true }}
16+
{{- $redirects.Get "paths" | jsonify $opts }}

0 commit comments

Comments
 (0)