Skip to content

Commit

Permalink
Adding Codepipeline support
Browse files Browse the repository at this point in the history
Add config flag and new handler
  • Loading branch information
ChrisPates committed Nov 7, 2022
1 parent 2a343f3 commit 4a39b2b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 48 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ Complete documentation is available at https://github.com/awslabs/ssosync`,
// running inside of AWS Lambda, we use the Lambda
// execution path.
func Execute() {
if cfg.IsLambda {
if cfg.IsLambdaRunningInCodePipeline {
lambda.Start(Handler)
}
else if cfg.IsLambda {
lambda.Start(rootCmd.Execute)
}

Expand All @@ -74,6 +77,50 @@ func Execute() {
}
}

func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error) {
log.Debug(event)
err := rootCmd.Execute()
if err != nil {
// notify codepipeline and mark its job execution as Failure
s := session.Must(session.NewSession())
cpl := codepipeline.New(s)
log.Fatal("Notifying CodePipeline and mark its job execution as Failure")
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Failure.
cplFailure := &codepipeline.PutJobFailureResultInput{
JobId: aws.String(jobID),
FailureDetails: &codepipeline.FailureDetails{
Message: aws.String(err.Error()),
Type: aws.String("JobFailed"),
},
}
_, cplErr := cpl.PutJobFailureResult(cplFailure)
if cplErr != nil {
log.Fatal("Failed to update CodePipeline jobID %s status with: %s", jobID, cplErr.Error())
}
return "Failure", err
}
s := session.Must(session.NewSession())
cpl := codepipeline.New(s)
log.Info("Notifying CodePipeline and mark its job execution as Success")
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Success.
cplSuccess := &codepipeline.PutJobSuccessResultInput{
JobId: aws.String(jobID),
}
_, cplErr := cpl.PutJobSuccessResult(cplSuccess)
if cplErr != nil {
log.Fatal("Failed to update CodePipeline jobID %s status with: %s", jobID, cplErr.Error())
}
return "Success", nil
}

func init() {
// init config
cfg = config.New()
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Config struct {
IsLambda bool
// IsLambdaRunningInCodePipeline ...
IsLambdaRunningInCodePipeline bool
// IsLambdaRunningInCodePipeline ...
IsLambdaRunningInCodePipeline bool
// Ignore users ...
IgnoreUsers []string `mapstructure:"ignore_users"`
// Ignore groups ...
Expand Down

0 comments on commit 4a39b2b

Please sign in to comment.