-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New action: aws_codebuild_start_build
#44444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
90106c8
New action: aws_codebuild_start_build
YakDriver c3be0af
Add changelog
YakDriver 0868a3e
Add docs
YakDriver 2ca9c99
Add new action: aws_codebuild_start_build
YakDriver 44e4fa2
Add tests for start_build
YakDriver 452bd4a
Lintering
YakDriver 4f69357
Lint fix
YakDriver 2bc5c2b
Fix semgrep
YakDriver b3111af
Update make note
YakDriver 2cd4c0f
Regional action
YakDriver 8964cb0
Add region
YakDriver dfb01c1
Fix hardcoded partitions
YakDriver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ```release-note:new-action | ||
| aws_codebuild_start_build | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package codebuild | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/service/codebuild" | ||
| awstypes "github.com/aws/aws-sdk-go-v2/service/codebuild/types" | ||
| "github.com/hashicorp/terraform-plugin-framework/action" | ||
| "github.com/hashicorp/terraform-plugin-framework/action/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
| fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" | ||
| fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" | ||
| "github.com/hashicorp/terraform-provider-aws/names" | ||
| ) | ||
|
|
||
| // @Action(aws_codebuild_start_build, name="CodeBuild Start Build") | ||
| func newStartBuildAction(context.Context) (action.ActionWithConfigure, error) { | ||
| return &startBuildAction{}, nil | ||
| } | ||
|
|
||
| type startBuildAction struct { | ||
| framework.ActionWithModel[startBuildActionModel] | ||
| } | ||
|
|
||
| type startBuildActionModel struct { | ||
| framework.WithRegionModel | ||
| ProjectName types.String `tfsdk:"project_name"` | ||
| SourceVersion types.String `tfsdk:"source_version"` | ||
| Timeout types.Int64 `tfsdk:"timeout"` | ||
| EnvironmentVariablesOverride fwtypes.ListNestedObjectValueOf[environmentVariableModel] `tfsdk:"environment_variables_override"` | ||
| BuildID types.String `tfsdk:"build_id"` | ||
| } | ||
|
|
||
| type environmentVariableModel struct { | ||
| Name types.String `tfsdk:"name"` | ||
| Value types.String `tfsdk:"value"` | ||
| Type types.String `tfsdk:"type"` | ||
| } | ||
|
|
||
| func (a *startBuildAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: "Starts a CodeBuild project build", | ||
| Attributes: map[string]schema.Attribute{ | ||
| "project_name": schema.StringAttribute{ | ||
| Description: "Name of the CodeBuild project", | ||
| Required: true, | ||
| }, | ||
| "source_version": schema.StringAttribute{ | ||
| Description: "Version of the build input to be built", | ||
| Optional: true, | ||
| }, | ||
| names.AttrTimeout: schema.Int64Attribute{ | ||
| Description: "Timeout in seconds for the build operation", | ||
| Optional: true, | ||
| }, | ||
| "build_id": schema.StringAttribute{ | ||
| Description: "ID of the started build", | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| Blocks: map[string]schema.Block{ | ||
| "environment_variables_override": schema.ListNestedBlock{ | ||
| CustomType: fwtypes.NewListNestedObjectTypeOf[environmentVariableModel](ctx), | ||
| Description: "Environment variables to override for this build", | ||
| NestedObject: schema.NestedBlockObject{ | ||
| Attributes: map[string]schema.Attribute{ | ||
| names.AttrName: schema.StringAttribute{ | ||
| Description: "Environment variable name", | ||
| Required: true, | ||
| }, | ||
| names.AttrValue: schema.StringAttribute{ | ||
| Description: "Environment variable value", | ||
| Required: true, | ||
| }, | ||
| names.AttrType: schema.StringAttribute{ | ||
| Description: "Environment variable type", | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (a *startBuildAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { | ||
| var model startBuildActionModel | ||
| resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| conn := a.Meta().CodeBuildClient(ctx) | ||
|
|
||
| timeout := 30 * time.Minute | ||
| if !model.Timeout.IsNull() { | ||
| timeout = time.Duration(model.Timeout.ValueInt64()) * time.Second | ||
| } | ||
|
|
||
| tflog.Info(ctx, "Starting CodeBuild project build", map[string]any{ | ||
| "project_name": model.ProjectName.ValueString(), | ||
| }) | ||
|
|
||
| resp.SendProgress(action.InvokeProgressEvent{ | ||
| Message: "Starting CodeBuild project build...", | ||
| }) | ||
|
|
||
| var input codebuild.StartBuildInput | ||
| resp.Diagnostics.Append(fwflex.Expand(ctx, model, &input)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| output, err := conn.StartBuild(ctx, &input) | ||
| if err != nil { | ||
| resp.Diagnostics.AddError("Starting CodeBuild project build", err.Error()) | ||
| return | ||
| } | ||
|
|
||
| buildID := aws.ToString(output.Build.Id) | ||
| model.BuildID = types.StringValue(buildID) | ||
|
|
||
| resp.SendProgress(action.InvokeProgressEvent{ | ||
| Message: "Build started, waiting for completion...", | ||
| }) | ||
|
|
||
| // Poll for build completion | ||
| deadline := time.Now().Add(timeout) | ||
| pollInterval := 30 * time.Second | ||
| progressInterval := 2 * time.Minute | ||
| lastProgressUpdate := time.Now() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| resp.Diagnostics.AddError("Build monitoring cancelled", "Context was cancelled") | ||
| return | ||
| default: | ||
| } | ||
|
|
||
| if time.Now().After(deadline) { | ||
| resp.Diagnostics.AddError("Build timeout", "Build did not complete within the specified timeout") | ||
| return | ||
| } | ||
|
|
||
| input := codebuild.BatchGetBuildsInput{ | ||
| Ids: []string{buildID}, | ||
| } | ||
| batchGetBuildsOutput, err := conn.BatchGetBuilds(ctx, &input) | ||
| if err != nil { | ||
| resp.Diagnostics.AddError("Getting build status", err.Error()) | ||
| return | ||
| } | ||
|
|
||
| if len(batchGetBuildsOutput.Builds) == 0 { | ||
| resp.Diagnostics.AddError("Build not found", "Build was not found in BatchGetBuilds response") | ||
| return | ||
| } | ||
|
|
||
| build := batchGetBuildsOutput.Builds[0] | ||
| status := build.BuildStatus | ||
|
|
||
| if time.Since(lastProgressUpdate) >= progressInterval { | ||
| resp.SendProgress(action.InvokeProgressEvent{ | ||
| Message: "Build currently in state: " + string(status), | ||
| }) | ||
| lastProgressUpdate = time.Now() | ||
| } | ||
|
|
||
| switch status { | ||
| case awstypes.StatusTypeSucceeded: | ||
| resp.SendProgress(action.InvokeProgressEvent{ | ||
| Message: "Build completed successfully", | ||
| }) | ||
| return | ||
| case awstypes.StatusTypeFailed, awstypes.StatusTypeFault, awstypes.StatusTypeStopped, awstypes.StatusTypeTimedOut: | ||
| resp.Diagnostics.AddError("Build failed", "Build completed with status: "+string(status)) | ||
| return | ||
| case awstypes.StatusTypeInProgress: | ||
| // Continue polling | ||
| default: | ||
| resp.Diagnostics.AddError("Unexpected build status", "Received unexpected build status: "+string(status)) | ||
| return | ||
| } | ||
|
|
||
| time.Sleep(pollInterval) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for our future selves -- we should pull this poll/send progress logic into something generic like we have with retries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed!