Skip to content

Commit 9ee4e85

Browse files
committed
releaser: add dry run mode
Signed-off-by: David Karlsson <[email protected]>
1 parent d7e80d5 commit 9ee4e85

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

_releaser/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,6 +41,7 @@ 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

_releaser/aws.go

Lines changed: 26 additions & 2 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{
@@ -102,17 +114,24 @@ type AwsCloudfrontUpdateCmd struct {
102114
CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"`
103115
RedirectsFile string `kong:"name='redirects-file',env='REDIRECTS_FILE'"`
104116
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.RedirectsFile, s.RedirectsPrefixesFile)
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,7 +247,7 @@ func (s *AwsCloudfrontUpdateCmd) Run() error {
228247
return nil
229248
}
230249

231-
func getLambdaFunctionZip(funcFilename, redirectsFile, redirectsPrefixesFile 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)
@@ -256,6 +275,11 @@ func getLambdaFunctionZip(funcFilename, redirectsFile, redirectsPrefixesFile str
256275
return nil, err
257276
}
258277

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+
259283
tmpdir, err := os.MkdirTemp("", "lambda-zip")
260284
if err != nil {
261285
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

0 commit comments

Comments
 (0)