Setup CodeBuild with Jenkins
Prerequisites
Spin up a Jenkins Server in that account
- Navigate to IAM Console
- Click Role
- Click Create Role
- Choose EC2 as the service that will use your role
- Click Next:Permissions
- Give the role AWSCodeBuildAdminAccess, AmazonS3FullAccess, and CloudWatchLogsFullAccess
- Click Next:Tags
- Click Next:Review
- Name role CodeBuildDemo
- Click Create Role
Create a KeyPair
- Navigate to EC2 console
- Click Key Pair on the left
- Click Create Key Pair
- Save Keypair on desktop
Create Jenkins Server
- Navigate to CloudFormation Console
- Click Create Stack
- Click Specify an Amazon S3 template URL and use
https://s3.amazonaws.com/proberts-public/jenkins_build.yaml
- Set Stack Name
- Set SSHKey to keypair from above
- Set Subnet to a public subnet
- Set VPC to a vpc that is part of that subnet
- Navigate to EC2 and find the instance name Jenkins public DNS
- Connect to the dns at port 8080
- SSH onto jenkins box to get the password
Setup Github repo
Create a new repository in your github account
git clone https://github.com/johnhanks1/jekyll_example
cd jekyll_example
git checkout -b master
git remote set-url origin https://github.com/#{github-repo-name}
or if using ssh for auth
git remote set-url origin [email protected]:#{github-repo-name}
git push --set-upstream origin master
Now we will add a couple of files that will be used with CodeBuild and Jenkins
buildspec.yml
version: 0.2
phases:
install:
commands:
- gem install jekyll jekyll-paginate jekyll-sitemap jekyll-gist
- bundle install
build:
commands:
- echo "******** Building Jekyll site ********"
- jekyll build
artifacts:
files: "**/*"
base-directory: _site
Setup CodeBuild Resources
Create a bucket for our CodeBuild Artifacts to be publish to:
- Navigate to https://s3.console.aws.amazon.com/s3/home?region=#{region}
- Click Create Bucket
- Enter Bucket name
- Click Next
- Select Versioning
- Click Next
- uncheck the checkbox next to Block new public bucket policies (Recommended)
- uncheck the checkbox next to Block public and cross-account access if bucket has public policies (Recommended)
- Click Next
- Click Create Bucket
- Select Bucket From list
- Select Properties Tab
- Select Static website Hosting
- Select Use this Bucket to host a website
- Add index.html to Index Document
- Add 404.html to Error Document
- Note the endpoint
- Click Save
Add Public Read to bucket
- Click Permissions
- Click Bucket Policy
- Paste policy below make sure to replace bucket name
{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::#{bucket-name}/*"
}
]
}
Login to AWSConsole and navigate to:
https://#{region}.console.aws.amazon.com/codesuite/codebuild/project/new?region=#{region}
- Set Project Name to #{project-name}
- Navigate to Source section
- Select GitHub
- Follow OAuth flow to connect your GitHub account to CodeBuild
- Select the repository created above.
- Navigate to environment select Ubuntu -> Ruby -> aws/codebuild/ruby:2.5.1
- Navigate to Artifacts
- Select Amazon S3 as an artifact type
- Choose the bucket we created above: jekyll-example-artifacts-#{account-id}-#{region}
- In the Name text box enter a .
- Click Checkbox Remove Artifact Encryption
- Click Create build Project
Setup Jenkins pipeline resources
Create an IAM user that can call CodeBuild
- Navigate to https://console.aws.amazon.com/iam/home?region=#{region}#/home
- Select Users
- Click Add user
- Add name and select Programmatic access check box
- Click Next: Permissions
- Click Attach Existing Policies directly
- Search for AWSCodeBuildAdminAccess
- Click check box
- Click Next: Review
- Click Create User
- Note Access Key Id and Secret Access Key
Add user to Jenkins
- Navigate To Jenkins main menu
- Click Credentials
- Click System
- Click Global credentials
- Click Add Credentials
- Click Kind and select CodeBuild Credentials
- Enter ID and note the id
- Add AccessKey and Secret Access Key from above leaving all other areas blank.
- Click OK
We now need to add a Jenkinsfile to our repository. This will have the information that
jenkins will need to call CodeBuild.
Jenkinsfile
Make sure to replace values with noted values from above
pipeline {
agent any
stages {
stage('Build') {
steps {
awsCodeBuild projectName: '#{project-name}',
credentialsId: '#{credential-id}',
credentialsType: 'jenkins',
region: '#{region}',
sourceControlType: 'project'
}
}
}
}
Push Changes into repo
git add buildspec.yml
git add Jenkinsfile
git commit -am "Adding Jenkinsfile and buildspec
git push
Create Jenkins Pipeline
- Click New Item
- Set Name
- Select Pipeline
- Click OK
- Navigate to Build Triggers
- Click Poll SCM
- Enter "* * * * *" this will pull github every minute for changes
- Navigate to Pipeline
- Click Definition and select Pipeline Script from SCM
- Click SCM select Git
- Enter Repository URL
- Click Save
- Now wait up to 1 minute and a build should be kicked off.
- Make sure build succeeded
- Check S3 Bucket website and see the newly created blog
Update Blog
We will now create a new post that will get automatically built.
- Open _posts/2018-11-14-welcome-to-jekyll.markdown
- Edit the posts Title and contents
- Commit and push changes to repository
- Watch for build to automatically be triggered by Jenkins
- Once build is complete look at the S3 website to see the update
Clean Up
We want to make sure to clear up these resources so that our aws account doesn't rack up charges.
- Terminate Ec2 Host
- Delete S3 Bucket
- Delete User and Role
- Delete CodeBuild Project