Setting up a CI/CD with Jenkins
Tags: devops, jenkins, docker, cdci
Published: 2021-08-16 22:41:41

### ** What you need ? **
* Dockerhub and Github Account
* Server with linux setup. For this case ubuntu
* Your application is already configured on your test instances
* This tutorial assumes you have set up a Jenkins file in your project. View [reference](https://github.com/kennethatria/my_guy/blob/master/Jenkinsfile) and make necessary changes

#### 1. Installing Jenkins on your Server
`sudo apt update`

search of all available packages & pick one from the options
`sudo apt search openjdk`

For this case I will be using version 11, so 
`sudo apt install openjdk-11-jdk`

Confirm installation with running version check
`java -version`

Now we are ready to install Jenkins. We start off by appending the Debian package repository address to the server's sources.list

`wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
    /etc/apt/sources.list.d/jenkins.list'`

We'll run update so that apt will use the new repo
`sudo apt-get update`

Installing Jenkins
`sudo apt-get install jenkins=2.289.3`

That's all 

#### 2. Setting up your Github and retrieving an API key
Login into your Github, and head over to your developer settings : `Profile -> Settings -> Developer settings -> Personal access tokens`

Note: I wouldn't consider this optional but it's good practice to set an expiration duration for your token.

Select admin:repo_hook

![Screenshot 2021-08-06 at 07.38.06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r17jzgg36s0cl240q74.png)
 
Lastly set `repo: *repo_status*`. Jenkins likes to access the repo commit status

![Screenshot 2021-08-06 at 07.33.21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmc62i2c6nuq0mnzmb04.png) 

Click "Generate token" and save your token somewhere.

#### 3. Setting up Jenkins

#####3.1 Setting up an Administrator 

1.SSH in your Jenkins instance
2.To retrieve your temporary password
`sudo "cat /var/lib/jenkins/secrets/initialAdminPassword"`
3.Visit `<your-instance-ip:8080>`
4.Paste retrieved password into Jenkins start up page.
5.Go ahead and set up a permanent admin user on Jenkins

#####3.2 Setting up your Jenkin Projects

1.Log into Jenkins

2.Set up credentials in Jenkins will use to login into servers. 
Click *Credentials* -> click *global* -> click *add credentials* and input your instance username: deploy, password :+++++, id: webserver_login

3.Set up docker hub credentials too: 
username: username, password :access-token, id: docker_hub_login

4.Set up global properties in Jenkins for staging server
Click "Manage Jenkins" - "Configure System" - "Global properties and select Environment Variables" -> Add
input "name: staging_ip" and "value:staging-ip-address"

5.Set up Jenkins project
Give your project a name -> Select Multibranch pipeline for project type

Under branch sources: click add source -> `GitHub`
Enter the details and use your GitHub access token as password and Github username as the username, set id e.g "github_key" and description

Select `github_key` for credentials
Input your GitHub name into owner field 
Set repository as intended repo
Go a head and Save

Jenkins will scan your repo and run an initial build

Click Jenkins, In the project - click `Master` -> `Build now`

##### 

## Next up
Powering up and down developer AWS instances using Slack and Jenkins