Introduction
This article provides instructions to setup a basic deployment using Jenkins to Amazon S3 by pulling the source code from GitLab and pushing it to a S3 bucket. Deployment automation using Jenkins saves us from manually uploading files to the S3 bucket using AWS online console. In addition to saving time, this method pushes the exact code that was committed in the latest Git push, therefore avoiding upload of wrong files. This is useful for static web site stored on S3 having a large number of files.
Pre-requisite
OS: Mac OS but the main ideas are OS agnostic.
It is assumed that you already have a static webpage on AWS S3. If not, you can check out instructions from AWS documentation.
You should also have your project added in GitLab. If not, you can have a look at their documentation.
Install Jenkins
- Download Jenkins and install (Current LTS version as of this writing is 2.89.2)
- After the installation, change the Update Site url from https to http:
Jenkins->Plugin Manager->Advanced (tab)->Update Site: http://updates.jenkins.io/update-center.json
- Install Git Plugin (3.7.0)
Jenkins->Plugin Manager->Available (tab)
Optional. Install Green Balls plugin to make the blue success ball green.
Setup to Pull Project from GitLab
Establish SSH authentication between your local machine and GitLab.
- Get the Private Key inside the file /.ssh/id_rsa and create a Git Credential.
This site shows you how to create a Pair of Ssh Key if you do not have one: https://docs.gitlab.com/ce/ssh/README.html
- Create a Git credential in Jenkins.
Jenkins->Credentials->System->Global credentials (unrestricted) -> Add Credentials
- Kind: SSH Username with private key
- Select Private Key and paste the private key
- Press OK
![Image 1](/KB/Articles/1222632/Add_Git_credential-r-700.png)
- Add Git username and email to Jenkins Git properties.
- Manage Jenkins->Configure System
- Add your Git username and email under Git plugin section
![Image 2](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Create a project/job in Jenkins.
Jenkins->New Item
- Enter any name for this Job
- Select Freestyle Project
- Press OK
![Image 3](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Configure Source Code Management to pull the project from Git.
- Fill in Git project URL and select credential to use (created in step 2) under Source Code Management tab.
- Press Save.
![Image 4](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- At this point, you can test to see if the Jenkins job can retrieve your project from Git by clicking Build Now.
![Image 5](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
If successful, you should see a blue or green ball. You can check the logs by clicking on the blue/green ball.
![Image 6](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
To check if the project is correctly pulled from git to Jenkins workspace, click on Workspace. You should see the same files that are in your Git repository.
![Image 7](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Pushing Project to Amazon S3
- Create a user to allow access to your S3 bucket.
- In the online AWS console->IAM (service)->Users
- Click Add user
- Enter any username you want
- Check Programmatic access
- Click Next Permission
- Select tab Attach Policy Directly
- Search for 's3'
- Select AmazonS3FullAccess
- Click Next Review
- Click Create user
- Write down the user Access Key ID and Secret Access Key as they will not be available anymore after this point.
- Click Close
![Image 8](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 9](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 10](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 11](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Install and configure S3cmd (command line client to access S3).
In a terminal:
- Install PIP if not already installed, to use it to install S3CMD: sudo easy_install pip
- Install S3cmd: sudo pip install s3cmd
- Configure S3cmd: s3cmd —configure
- Enter the Access Key ID and Secret Access Key and accept default for other questions. (Except if your region is other than US, enter a different region.)
- Type which s3cmd to find the location of S3cmd (mine is "/usr/local/bin/s3cmd")
- Update the Jenkins job to use S3cmd to push code to S3:
- Return to the job created in Jenkins and click Configure
- Go to the Build section
- Enter:
PATH="/usr/local/bin:$PATH"
s3cmd sync . s3://optimus.com --delete-removed --exclude '.git/'
Explanation:
The first line tells Jenkins where to find s3cmd command by adding its location to PATH.
The second line uses s3cmd to copy all (.) to a S3 bucket name optimus.com
--delete-removed will remove deleted files from S3
--exclude '.git/' excludes the git directory to sync to S3.
- Click Save
- Click Build Now
![Image 12](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
By checking the job’s log, you should see that Jenkins pulled the code from Git and pushed it to AWS S3.
That’s it! I hope this helped you.
History
- 1st January, 2018: Initial version
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.