Christopher
Stoll

Docker Build in Jenkins from AWS CodePipeline

I know the title has a high buzzword quotient, but I assure you it is a real thing. The use case is this: there is code stored on GitHub which, after it passes all tests, will be deployed to Elastic Beanstalk inside of Docker container. For this project CodePipeline will get the source from GitHub and send it to Jenkins for testing. If the tests pass then CodePipeline will publish the app to Elastic Beanstalk. Below are the steps required to get this up and running.

Before proceeding you must have an Amazon Web Services account and be somewhat familiar with AWS.

Create the CodePipeline

  1. Log in to the AWS console and got to EC2 (Services > Developer Tools > CodePipeline)
  2. Create the pipeline
    • Name
      • I’ll call it DEMO-CPL, replace that with a name of your choosing
    • Source
      • Select GitHub for source provider
      • Click “Connect to GitHub”
      • Choose the correct repository
      • Choose the appropriate branch
    • Build
      • Choose “Add Jenkins” for the build provider
      • I’ll set the provider name to JENKINS-DEMO-CPL-BUILD, replace that with a name of your choosing
      • If you are coming from the Jenkins install tutorial, then the Server URL should be PUBLIC_DNS_NAME:8080
      • I’ll set the project name to DEMO-CPL-BUILD, replace that with a name of your choosing
    • Beta
      • Choose AWS Elastic Beanstalk for deployment provider
      • I’ll set the application name to DEMO-CPL, replace that with a name of your choosing
      • I’ll set the environment name to DEMO-CPL-ENV-DEFAULT, replace that with a name of your choosing
    • Role
      • Create a role named AWS-CodePipeline-Service so that the pipeline can access Elastic Beanstalk (and S3, if you use that instead of GitHub)
    • Review and save
  3. Edit the newly created pipeline
    • Change the stage named “Build” to “Test”
    • Edit the stage action named “Build”
      • Change the action category to “Test”
      • Set the action name
      • Choose “Add Jenkins” for “Test Provider”
      • I’ll set the provider name to JENKINS-DEMO-CPL-TEST, replace that with a name of your choosing
      • If you are coming from the Jenkins install tutorial, then the Server URL should be PUBLIC_DNS_NAME:8080
      • I’ll set the project name to DEMO-CPL-TEST, replace that with a name of your choosing
      • Save the action
    • Edit the stage action named “Build” (again)
      • Set the input artifact
      • Leave the output artifact blank (if you want the Beta stage to use the source as it was checked out from GitHub, which you probably do if you are just testing and not modifying or building the code)
      • Update the action
    • Edit the action in the stage named “Beta”
      • Set the input artifact the same as the input artifact from the test stage (the output artifact from the source stage)

Configure Jenkins

  1. Log in to Jenkins and click “New Item”
  2. Name the item DEMO-CPL-TEST, select “Freestyle project”, and click OK
  3. Configure the new item
    • Check “Discard Old Builds” and select a rotation strategy (i.e set “Max # of builds to keep” to 5)
    • Set Source Code Management to AWS CodePipeline
      • Change the region if you are not using US_EAST_1
      • Set the category to Test
      • Set the provider to JENKINS-DEMO-CPL-TEST
    • Select Poll SCM under Build Triggers
      • Set the schedule to H/5 * * * * to have Jenkins check CodePipeline every 5 minutes
    • Add an execute shell build step
      • Add the commands listed below
    • Add post-build actions
      • Add “Publish JUnit test result report”
        • Set “Test report XMLs” to test-results.xml (or whatever you want to call it)
      • Add “AWS CodePipeline Publisher”
        • Add a “Build Output Location”, but leave it blank
      • Add “E-Mail Notification”
        • Set the recipients
        • And check send email for every unstable build
    • Save

Build Step Commands

docker build -t DEMO-CPL .
docker run --rm -v \
	$WORKSPACE:/DEMO-CPL \
	--net=host -e \
	DOTEST=1 \
	DEMO-CPL

The process

Once everything is set up, CodePipeline will monitor GitHub for changes and automatically initiate the process. It is also possible to manually release the change from within the CodePipeline interface.

Published: 2016-01-09
DevOpsDockerJenkinsAWSCodePipelineElastic Beanstalk