Skip to content

feat(codepipeline): support environment variables for actions #34667

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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Jun 10, 2025

Issue # (if applicable)

N/A

Reason for this change

CodePipeline supports the Environment Variables in actions, but L2 Pipeline with actions doesn't support it.

Description of changes

Added EnvironmentVariable with PlaintextEnvironmentVariable and SecretsManagerEnvironmentVariable.

Describe any new or updated permissions being added

None.

Description of how you validated changes

Both unit tests and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added the p2 label Jun 10, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team June 10, 2025 10:12
@github-actions github-actions bot added the distinguished-contributor [Pilot] contributed 50+ PRs to the CDK label Jun 10, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@go-to-k go-to-k changed the title feat(codepipeline-actions): support environment variables feat(codepipeline): support environment variables Jun 10, 2025
@go-to-k go-to-k marked this pull request as ready for review June 10, 2025 12:00
@aws-cdk-automation aws-cdk-automation dismissed their stale review June 10, 2025 12:01

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@go-to-k go-to-k marked this pull request as draft June 10, 2025 12:08
@go-to-k go-to-k marked this pull request as ready for review June 10, 2025 13:08
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jun 10, 2025
@go-to-k go-to-k changed the title feat(codepipeline): support environment variables feat(codepipeline): support environment variables for actions Jun 10, 2025
change integ
*
* @default - no environment variables
*/
readonly actionEnvironmentVariables?: EnvironmentVariable[];
Copy link
Contributor Author

@go-to-k go-to-k Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoided the name environmentVariables because CodeBuildActionProps that extends codepipeline.CommonAwsActionProps and this CommonActionProps has already used a property with the same name and an error occurs if the name is used in this props.

https://github.com/aws/aws-cdk/blob/v2.200.1/packages/aws-cdk-lib/aws-codepipeline-actions/lib/codebuild/build-action.ts#L81

@go-to-k go-to-k marked this pull request as draft June 10, 2025 17:43
@go-to-k go-to-k marked this pull request as ready for review June 11, 2025 11:12
@go-to-k go-to-k marked this pull request as draft June 11, 2025 11:13
unused grantRead for secret

unit tests
@go-to-k go-to-k force-pushed the cp-env-vars branch 3 times, most recently from 7eb8299 to 2a82dcd Compare June 11, 2025 12:09
doc

refactor tests

rm unused improts
@go-to-k go-to-k marked this pull request as ready for review June 11, 2025 12:10
Comment on lines +148 to +153
// Don't use `grantRead` method for the secret because `secretsmanager:DescribeSecret` is not required.
// See https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-Commands.html#action-reference-Commands-envvars
options.role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['secretsmanager:GetSecretValue'],
resources: [secretArn],
}));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-Commands.html#action-reference-Commands-envvars

To use the SecretsManager, you must add the following permissions to your pipeline service role:

{
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetSecretValue"
            ],
            "Resource": [
                "SECRET_ARN"
            ]
        }

Comment on lines +85 to +89
/**
* Bind the environment variable to the action.
* @internal
*/
public abstract _bind(scope: Construct, actionProperties: ActionProperties, options: ActionBindOptions): void;
Copy link
Contributor Author

@go-to-k go-to-k Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, this abstract method was not made, but the concrete bind method was only created for SecretsManagerEnvironmentVariable. However, considering that support for PARAMETER_STORE may be added in the future, such as the one for CodeBuild, it is better to create the abstract method, so I did it this way. Otherwise, it would be necessary to implement the bind calling as follows. If more types are added in the future, the code will become even more complicated.

    envVars?.forEach(envVar => {
      if (envVar instanceof SecretsManagerEnvironmentVariable || envVar instanceof ParameterStoreEnvironmentVariable) {
        envVar._bind(scope, this.actionProperties, options);
      }
    });

Now we can write like:

    envVars?.forEach(envVar => {
      envVar._bind(scope, this.actionProperties, options);
    });

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 9d906f7
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants