Home > Net >  Create a stage in cdk pipeline to run shell commands
Create a stage in cdk pipeline to run shell commands

Time:08-20

I am trying to create an AWS CodePipeline using AWS CDK in python

cdk verson = 2.29.0

import aws_cdk as cdk
from aws_cdk.pipelines import CodePipeline, CodePipelineSource, ShellStep
from aws_cdk import (
    aws_codecommit,
    pipelines,
    aws_codepipeline_actions,
    aws_codepipeline,
    aws_codebuild as codebuild,
    aws_iam as iam
    )
from my_pipeline.my_pipeline_app_stage import MyPipelineAppStage
from constructs import Construct



class MyStacksStage(cdk.Stage):

    def __init__(self, scope, id, *, env=None, outdir=None):
        super().__init__(scope, id, env=env, outdir=outdir)
        self.stack1 = cdk.Stack(self, "stack1")
        
        
class MyPipelineStack(cdk.Stack):

    def __init__(self, scope: Construct, construct_id: str, branch, **kwargs) -> None:
        super().__init__(scope, construct_id  ,**kwargs)
        
        
        repository = aws_codecommit.Repository.from_repository_name(self,"cdk_pipeline", repository_name="repository-name")
        
        pipeline_source = CodePipelineSource.code_commit(repository,"master")
        pipeline =  CodePipeline(self, "Pipeline",
                        self_mutation=False,
                        pipeline_name="cdk_pipeline",
                        synth=ShellStep("Synth", 
                            input=pipeline_source,
                            commands=["npm install -g aws-cdk", 
                                      "python -m pip install -r requirements.txt", 
                                    "cdk synth"]
                        ),
                        
                        )

        pipeline.add_stage(prod,
                           post=[pipelines.ShellStep("stack2_post",
                                                      commands=["ls"])]
        

I am creating the pipeline using aws_cdk.pipelines.CodePipeline.

What I want is just to create a step to run a script in CodeBuild, but to add a stage I need to create a class stage that contains at least a stack.

The way I am doing it right now is by creating the MyStacksStage class and adding a variable cdk.Stack.

When I add the stage then I add the ShellStep in the post parameter to be able to run a shell command.

It´s my first time working with AWS CodePipeline and I would want to know if there is another way to create a stage to run shell commands without creating a stack and run them in pre or post?

CodePudding user response:

CDK pipelines is for deploying CDK apps. If you just want to create a pipeline that doesn't deploy any CloudFormation stacks defined with CDK and instead runs arbitrary shell commands in Code Build, you don't need CDK pipelines at all - just create a plain codebuild.Pipeline and add a CodeBuildAction to it.

  • Related