I finally managed to use Jenkins on my CodeCommit repository. Here are the steps I followed in order to do that.
I assume you have a Jenkins Server up and running. Basically we will need to make configuration in both AWS and Jenkins.
AWS IAM Settings
First we need to have an AWS user that has access to our repository. Go to IAM Manager and either create a new user or give the right permissions to an existing user. I am not too paranoid so I attached "AWSCodeCommitFullAccess" policy to my existing user. You can narrow down the credential that only has the access to the specific repository but I did not bother.
Then select this user and in the summary navigate to "Security Credentials" tab. The easiest is to use "HTTPS Git credentials for AWS CodeCommit". So I generated a username and password to give to Jenkins to access my repo. Generate and save it to somewhere safe we will use this later.
Also if you don't have the AWS Access Keys for this IAM User you will need to create it too. We will need both user/pass and AWS Access Keys.
Jenkins Settings
If you've installed Jenkins with most common settings you will see that it will not have CodeCommit plugin. So head over to: Jenkins > Manage Jenkins > Manage Plugins
and install "AWS Code Commit Jobs Plugin".
Now go to Credentials and create 2 global credentials:
- Username with Password
- AWS Credentials
Enter your generated username/password
Then enter your AWS credentials
So my credentials list looks like below:
Now create new item in Jenkins and select "AWS Code Commit"
Now attach your "AWS Credentials" and "Code Commit Credentials" and make sure that your zone is correct in the URL. https://codecommit.us-east-1.amazonaws.com If you don't provide a regex Jenkins will scan and discover all your repositories and create a job for each branch.
Now in your root folder create a jenkinsfile and jenkins will automatically detect it when you push to repository.
My first basic jenkinsfile is as below and what it does is to clone the repository (first delete if it already exists) then run npm install
and npm test
commands on it.
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
checkout scm
}
stage ('Build') {
sh "npm install"
sh "npm test"
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
Now all I need to do is to drill into the branch I want to build and click 'Build' button on the left.
Well this is a manual build. You will want to automate this upon creating a PR or pushing a commit into the repo. Jenkins does not provide a good integration into CodeCommit as it does for GitHub. But, you can create an AWS Lambda trigger which executes an HTTP POST request. This request will trigger a build.
curl --request POST --url 'http://35.xx.xx.xx/jenkins/job/MYJOBNAME/job/MYREPONAME/job/master/build?delay=0sec' --user MYUSERNAME:MYAPITOKEN
to get the API token, click your username on the top right menu (next to logout button), click 'Configure' and then 'Show API Token...'