Add Application to Pipeline

Create Stage #

At this point, you have a fully operating CodePipeline that will automatically update itself on every commit, BUT at the moment, that is all it does. We need to add a stage to the pipeline that will deploy our application.

Create a new file in CdkWorkshop called WorkshopPipelineStage.java with the code below:

package com.myorg;

import software.amazon.awscdk.Stage;
import software.constructs.Construct;
import software.amazon.awscdk.StageProps;

public class WorkshopPipelineStage extends Stage {
    public WorkshopPipelineStage(final Construct scope, final String id) {
        this(scope, id, null);
    }

    public WorkshopPipelineStage(final Construct scope, final String id, final StageProps props) {
        super(scope, id, props);

        new CdkWorkshopStack(this, "WebService");
    }
}

All this does is declare a new Stage (component of a pipeline), and in that stage instantiate our application stack.

Add stage to pipeline #

Now we must add the stage to the pipeline by adding the following code to WorkshopPipelineStack.java:

package com.myorg;

import java.util.List;
import java.util.Map;

import software.constructs.Construct;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.pipelines.CodeBuildStep;
import software.amazon.awscdk.pipelines.CodePipeline;
import software.amazon.awscdk.pipelines.CodePipelineSource;

import software.amazon.awscdk.services.codecommit.Repository;

public class WorkshopPipelineStack extends Stack {
    public WorkshopPipelineStack(final Construct parent, final String id) {
        this(parent, id, null);
    }

    public WorkshopPipelineStack(final Construct parent, final String id, final StackProps props) {
        super(parent, id, props);

        // This creates a new CodeCommit repository called 'WorkshopRepo'
        final Repository repo = Repository.Builder.create(this, "WorkshopRepo")
            .repositoryName("WorkshopRepo")
            .build();

        // The basic pipeline declaration. This sets the initial structure
        // of our pipeline
        final CodePipeline pipeline = CodePipeline.Builder.create(this, "Pipeline")
                .pipelineName("WorkshopPipeline")
                .synth(CodeBuildStep.Builder.create("SynthStep")
                        .input(CodePipelineSource.codeCommit(repo, "main"))
                        .installCommands(List.of(
                                "npm install -g aws-cdk"   // Commands to run before build
                        ))
                        .commands(List.of(
                                "mvn package",            // Language-specific build commands
                                "npx cdk synth"           // Synth command (always same)
                        )).build())
                .build();

        final WorkshopPipelineStage deploy = new WorkshopPipelineStage(this, "Deploy");
        pipeline.addStage(deploy);
    }
}

This imports and creates an instance of the WorkshopPipelineStage. Later, you might instantiate this stage multiple times (e.g. you want a Production deployment and a separate development/test deployment).

Then we add that stage to our pipeline (pipeline.addStage(deploy);). An ApplicationStage in a CodePipeline represents any CDK deployment action.

Commit/Deploy #

Now that we have added the code to deploy our application, all that’s left is to commit and push those changes to the repo.

git commit -am "Add deploy stage to pipeline" && git push

Once that is done, we can go back to the CodePipeline console and take a look as the pipeline runs (this may take a while).

Success!




We use analytics to make this content better, but only with your permission.

More information