- You have an AWSAccount
- 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 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
- Fill in each value for the template and create the stack.
- You will receive the password to login to your jenkins host on your phone.
- Navigate to EC2 and find the instance name Jenkins public DNS
- Connect to the dns at port 8080
Replace values in walk through with customer values
- #{region} -> region you would like to create resources in
- #{account-id} -> your account id
- #{project-name} -> AWSCodeBuildProject name such as CodeBuildJekyllExample
Assume customer has a working Jenkins server.
Create a new repository
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 git@github.com:#{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
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 Path text box enter a .
- Click Checkbox Remove Artifact Encryption
- Click Create build Project
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
- 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'
}
}
}
}
git add buildspec.yml
git add Jenkinsfile
git push
- 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
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