I built a Code Pipeline with AWS CDK.

Good work. @naokiur. We look forward to working with you in 2020 and this year as well.

After knowing AWS CDK "How do you create this with an AWS CDK ...?" I often think like this.

This time, I will introduce a simple CI / CD environment. Built with AWS CDK (Java).

environment

Building Code Pipeline

Diagram

スクリーンショット 2020-01-05 21.01.32.png

Get the source, ZIP, just store in S3, Because I use Github for business, Not CodeCommit I made Github the source.

Code Pipeline construction

To build CodePipeline Broadly speaking, I was able to build it by creating the following.

Pipeline class

Literally, it is a class that represents CodePipeline. build () this class By cdk deploy I was able to build a Code Pipeline on AWS.

final Pipeline saveToS3Pipeline = Pipeline.Builder
        .create(this, "saveSourceToS3")
        .pipelineName("saveSourceToS3")
        .stages(new ArrayList<>(Arrays.asList(source, build, deploy)))
        .build();

Not limited to this To generate AWS resources hoge.Builder.create (Stack class, id). ~ Omitted ~ .build () It seems that it can be built with I feel that it is easy to understand.

CodePipeline requires at least two Stages, so You must specify a List with at least two elements in stages (). If not specified, an error will occur.

Stage class

This is the Stage class to be set in CodePipeline. This time (although there is not much content) You have created all three stages: Source, Build, and Deploy.

final StageProps source = StageProps.builder()
        .stageName("DownloadSourceFromGithub")
        .actions(new ArrayList<>(Arrays.asList(github)))
        .build();

final StageProps build = StageProps.builder()
        .stageName("BuildSource")
        .actions(new ArrayList<>(Arrays.asList(codeBuild)))
        .build();

final StageProps deploy = StageProps.builder()
        .stageName("SaveSourceToS3")
        .actions(new ArrayList<>(Arrays.asList(s3)))
        .build();

It's not written as hoge.Builder.create (Stack class, id). ~ Omitted ~ .build (). Stage is not an AWS service, but an element of CodePipeline, This is because it does not appear as a resource in the CloudFormation stack. (* This is an individual opinion)

It seems that Stage can have multiple Actions.

Action class

A class of Action to be executed in Stage. Create Actions for each Stage. This time (because there is not much content) One for each stage.

final Action github = GitHubSourceAction.Builder
        .create()
        .actionName("DownloadFromGithub")
        .oauthToken(githubToken)
        .branch(branchName)
        .repo(repoName)
        .owner(ownerName)
        .output(sourceArtifact)
        .build();

final Action codeBuild = CodeBuildAction.Builder
        .create()
        .actionName("BuildSource")
        .project(codeBuildProject)
        .input(sourceArtifact)
        .outputs(new ArrayList<>(Arrays.asList(buildArtifact)))
        .build();

final Action s3 = S3DeployAction.Builder
        .create()
        .bucket(deployBucket)
        .actionName("DeploySourceToS3")
        .input(buildArtifact)
        .build();

In the codepipeline.actions package, Since there is a class according to the Action to be executed, Generate the required class. (Currently, it doesn't seem to be all that CodePipeline can do ...)

GitHubSourceAction class

Action to use Github as Source. It's easy to understand. Set up repositories and branches.

You can also specify an OAuth Token to connect, Use the SecretValue class.

SecretValue class

This class is for getting secret information. Get information from Secret in the System Manager parameter store It seems that you can get information from Secret Manager.

This time, set the Token of Github in Secret Manager in advance, I tried to get it.

final SecretsManagerSecretOptions secretOptions = SecretsManagerSecretOptions.builder()
        .jsonField("github-token")
        .build();
final SecretValue githubToken = SecretValue.secretsManager(
        "naokiur-secret",
        secretOptions
);

CodeBuildAction class

A class that builds CodeBuild for CodePipeline. This time I created buildspec.yml in the repository.

S3DeployAction class

A class for deploying to S3.

Artifact class

I came out in the Action class Also a CodePipeline, This class is specified for Input / Output of each Action. Now you have the image of handing over.

Create the following two Specified for Input / Output of each Action.

final Artifact sourceArtifact = Artifact.artifact("Source");
final Artifact buildArtifact = Artifact.artifact("Build");

Now you have a complete build! !! スクリーンショット 2020-01-08 13.30.35.png

Where I was addicted

I used it as a reference

Recommended Posts

I built a Code Pipeline with AWS CDK.
I built Step Functions with AWS CDK.
I made a GUI with Swing
Run (provisionally) a Docker image with ShellCommandActivity on AWS Data Pipeline
I built a rails environment with docker and mysql, but I got stuck
I tried playing with BottomNavigationView a little ①
I made a risky die with Ruby
I made a rock-paper-scissors app with kotlin
I made a rock-paper-scissors app with android
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I built a Java EE environment on AWS and tried running a web application
I wrote a Jenkins file with Declarative Pipeline (Checkstyle, Findbugs, PMD, CPD, etc.)
04. I made a front end with SpringBoot + Thymeleaf
I made a gender selection column with enum
I made a LINE bot with Rails + heroku
Try debugging a Java program with VS Code
I tried to break a block with java (1)
I made a portfolio with Ruby On Rails
Build a Java development environment with VS Code
I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)
Submit a job to AWS Batch with Java (Eclipse)
I read the readable code, so make a note
[Ruby] Generate a concatenated QR code with rqrcode (Knowledge)
How to delete a new_record object built with Rails
[Ruby] I made a crawler with anemone and nokogiri.
I want to monitor a specific file with WatchService
Why can I develop Java with Visual Studio Code?
[Ruby] Generate a concatenated QR code with rqrcode (Practice)
I tried OCR processing a PDF file with Java
[Beginner] I stumbled upon launching a project with Rails6
[Environment construction] Build a Java development environment with VS Code!
I wrote a test with Spring Boot + JUnit 5 now