Home > front end >  (@aws-cdk/aws-codepipeline) - Using "ShellScriptAction" equivalent in a Pipeline made with
(@aws-cdk/aws-codepipeline) - Using "ShellScriptAction" equivalent in a Pipeline made with

Time:02-18

Here's my problem. I'm currently struggling to run a basic shell script execution action in my pipeline. The pipeline was created thru the Pipeline construct in @aws-cdk/aws-codepipeline

import { Artifact, IAction, Pipeline } from "@aws-cdk/aws-codepipeline"
const pipeline = new Pipeline(this, "backend-pipeline",{
...
});

Now, I'm running a cross deployment pipeline and would like to invoke a lambda right after it's been created. Before, a simple ShellScriptAction would've sufficed in the older (@aws-cdk/pipelines) package, but for some reason, both packages pipelines and aws-codepipeline are both maintained at the same time.

What I would like to know is how to run a simple basic command in the new (aws-codepipeline) package, ideally as an Action in a Stage.

Thanks in advance!

CodePudding user response:

@aws-cdk/aws-codepipeline is for AWS Codepipeline. @aws-cdk/pipelines is for utilizing AWS Codepipeline to deploy CDK apps. Read more about the package and its justification here.

Regarding your question, you have some options here.

First of all, if you're looking for a simple CodeBuild action to run arbitrary commands, you can use CodeBuildAction.

There's a separate action specifically for invoking a lambda, too, it's LambdaInvokeAction.

Both are part of @aws-cdk/aws-codepipeline-actions module.

CodePudding user response:

You would use a codebuild.PipelineProject in a codepipeline_actions.CodeBuildAction to run arbitrary shell commands in your pipeline. The CDK has several build tool constructs*, used in different places. pipelines.CodePipeline specializes in deploying CDK apps, while the lower-level codepipeline.Pipeline has a broader build capabilities:

Build tool construct CloudFormation Resource Can use where?
pipelines.ShellStep AWS::CodeBuild::Project pipelines.CodePipeline
codebuild.PipelineProject AWS::CodeBuild::Project codepipeline.Pipeline
codebuild.Project AWS::CodeBuild::Project codepipeline.Pipeline or standalone

In your case, the setup goes Pipeline > Stage > CodeBuildAction > PipelineProject.

// add to stage actions
new codepipeline_actions.CodeBuildAction({
  actionName: 'CodeBuild',
  project: new codebuild.PipelineProject(this, 'PipelineProject', {
    buildSpec: codebuild.BuildSpec.fromObject({
      version: '0.2',
      phases: {
        build: { commands: ['echo "[project foo] $PROJECT_FOO"'] },
      },
    }),
    environmentVariables: {
      PROJECT_FOO: {
        value: 'Foo',
        type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
      },
    },
  }),
  input: sourceOutput,
});

* The ShellScriptAction you mentioned is another one, now deprecated in v1 and removed from v2.

  • Related