Working with variables - AWS CodePipeline

Working with variables

Some actions in CodePipeline generate variables. To use variables:

  • You assign a namespace to an action to make the variables it produces available to a downstream action configuration.

  • You configure the downstream action to consume the variables generated by the action.

    You can view the details for each action execution to see the values for each output variable that was generated by the action in execution-time.

For reference information about variables, an example workflow, and a list of actions that generate output variables, see Variables.

Configure actions for variables

When you add an action to your pipeline, you can assign it a namespace and configure it to consume variables from previous actions.

Configure actions with variables (console)

This example creates a pipeline with a CodeCommit source action and a CodeBuild build action. The CodeBuild action is configured to consume the variables produced by the CodeCommit action.

If the namespace isn’t specified, the variables are not available for reference in the action configuration. When you use the console to create a pipeline, the namespace for each action is generated automatically.

To create a pipeline with variables

  1. Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.

  2. Choose Create pipeline. Enter a name for your pipeline, and then choose Next.

  3. In Source, in Provider, choose CodeCommit. Choose the CodeCommit repository and branch for the source action, and then choose Next.

  4. In Build, in Provider, choose CodeBuild. Choose an existing CodeBuild build project name or choose Create project. On Create build project, create a build project, and then choose Return to CodePipeline.

    Under Environment variables, choose Add environment variables. In this example, enter the execution ID with the variable syntax #{codepipeline.PipelineExecutionId} and commit ID with the variable syntax #{SourceVariables.CommitId}.

    
                            Example: Variables for multiple actions
    Note

    You can enter variable syntax in any action configuration field in the wizard.

  5. Choose Create.

  6. After the pipeline is created, you can view the namespace that was created by the wizard. On the pipeline, choose the help pane icon for the stage you want to view the namespace for. In this example, the source action's auto-generated namespace, SourceVariables, is displayed.

    
                            Example: Action Info Screen

To edit the namespace for an existing action

  1. Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.

  2. Choose the pipeline you want to edit, and then choose Edit. For the source stage, choose Edit stage. Add the CodeCommit action.

  3. On Edit action, view the Variable namespace field. If the existing action was created previously or without using the wizard, you must add a namespace. In Variable namespace, enter a namespace name, and then choose Save.

    
                            Example: Namespace in the Edit action screen

To view output variables

  1. Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.

  2. After the pipeline is created and runs successfully, you can view the variables on the Action execution details page. For information, see View variables (console).

Configure actions for variables (CLI)

When you use the create-pipeline command to create a pipeline or the update-pipeline command to edit a pipeline, you can reference/use variables in the configuration of an action.

If the namespace isn't specified, the variables produced by the action are not available to be referenced in any downstream action configuration.

To configure an action with a namespace

  1. Follow the steps in Create a pipeline in CodePipeline to create a pipeline using the CLI. Start an input file to provide the create-pipeline command with the --cli-input-json parameter. In the pipeline structure, add the namespace parameter and specify a name, such as SourceVariables.

    . . . { "inputArtifacts": [], "name": "Source", "region": "us-west-2", "namespace": "SourceVariables", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "CodeCommit" }, "outputArtifacts": [ . . .
  2. Save the file with a name like MyPipeline.json.

  3. At a terminal (Linux, macOS, or Unix) or command prompt (Windows), run the create-pipeline command and create the pipeline.

    Call the file you created when you run the create-pipeline command. For example:

    aws codepipeline create-pipeline --cli-input-json file://MyPipeline.json

To configure downstream actions to consume variables

  1. Edit an input file to provide the update-pipeline command with the --cli-input-json parameter. In the downstream action, add the variable to the configuration for that action. A variable is made up of a namespace and key, separated by a period. For example, to add variables for the pipeline execution ID and the source commit ID, specify the namespace codepipeline for the variable #{codepipeline.PipelineExecutionId}. Specify the namespace SourceVariables for the variable #{SourceVariables.CommitId}.

    { "name": "Build", "actions": [ { "outputArtifacts": [ { "name": "BuildArtifacts" } ], "name": "Build", "configuration": { "EnvironmentVariables": "[{\"name\":\"Execution_ID\",\"value\":\"#{codepipeline.PipelineExecutionId}\",\"type\":\"PLAINTEXT\"},{\"name\":\"Commit_ID\",\"value\":\"#{SourceVariables.CommitId}\",\"type\":\"PLAINTEXT\"}]", "ProjectName": "env-var-test" }, "inputArtifacts": [ { "name": "SourceArtifact" } ], "region": "us-west-2", "actionTypeId": { "provider": "CodeBuild", "category": "Build", "version": "1", "owner": "AWS" }, "runOrder": 1 } ] },
  2. Save the file with a name like MyPipeline.json.

  3. At a terminal (Linux, macOS, or Unix) or command prompt (Windows), run the create-pipeline command and create the pipeline.

    Call the file you created when you run the create-pipeline command. For example:

    aws codepipeline create-pipeline --cli-input-json file://MyPipeline.json

View output variables

You can view the action execution details to view the variables for that action, specific to each execution.

View variables (console)

You can use the console to view variables for an action.

  1. Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.

    The names of all pipelines associated with your AWS account are displayed.

  2. In Name, choose the name of the pipeline.

  3. Choose View history.

  4. After the pipeline runs successfully, you can view the variables produced by the source action. Choose View history. Choose Source in the action list for the pipeline execution to view the action execution details for the CodeCommit action. On the action detail screen, view the variables under Output variables.

    
                            Example: Source output variables
  5. After the pipeline runs successfully, you can view the variables consumed by the build action. Choose View history. In the action list for the pipeline execution, choose Build to view the action execution details for the CodeBuild action. On the action detail page, view the variables under Action configuration. The auto-generated namespace is displayed.

    
                            Example: Action Configuration Variables

    By default, Action configuration displays the variable syntax. You can choose Show resolved configuration to toggle the list to display the values that were produced during the action execution.

    
                            Example: Resolved Action Configuration Variables

View variables (CLI)

You can use the list-action-executions command to view variables for an action.

  1. Use the following command:

    aws codepipeline list-action-executions

    The output shows the outputVariables parameter as shown here.

    "outputVariables": { "BranchName": "master", "CommitMessage": "Updated files for test", "AuthorDate": "2019-11-08T22:24:34Z", "CommitId": "d99b0083cc10EXAMPLE", "CommitterDate": "2019-11-08T22:24:34Z", "RepositoryName": "variables-repo" },
  2. Use the following command:

    aws codepipeline get-pipeline --name <pipeline-name>

    In the action configuration for the CodeBuild action, you can view the variables:

    { "name": "Build", "actions": [ { "outputArtifacts": [ { "name": "BuildArtifact" } ], "name": "Build", "configuration": { "EnvironmentVariables": "[{\"name\":\"Execution_ID\",\"value\":\"#{codepipeline.PipelineExecutionId}\",\"type\":\"PLAINTEXT\"},{\"name\":\"Commit_ID\",\"value\":\"#{SourceVariables.CommitId}\",\"type\":\"PLAINTEXT\"}]", "ProjectName": "env-var-test" }, "inputArtifacts": [ { "name": "SourceArtifact" } ], "region": "us-west-2", "actionTypeId": { "provider": "CodeBuild", "category": "Build", "version": "1", "owner": "AWS" }, "runOrder": 1 } ] },

Example: Use variables in manual approvals

When you specify a namespace for an action, and that action produces output variables, you can add a manual approval that displays variables in the approval message. This example shows you how to add variable syntax to a manual approval message.

  1. Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home.

    The names of all pipelines associated with your AWS account are displayed. Choose the pipeline you want to add the approval to.

  2. To edit your pipeline, choose Edit. Add a manual approval after the source action. In Action name, enter the name of the approval action.

  3. In Action provider, choose Manual approval.

  4. In URL for review and Comments, add the variable syntax for CommitId and CommitMessage as shown. Make sure you use the namespace assigned to your source action. For example, the variable syntax for a CodeCommit action with the default namespace SourceVariables is #{SourceVariables.CommitId}.

  5. After the pipeline runs successfully, you can view the variable values in the approval message.