Jenkins Essentials
7 minutes read · Dec 22, 2024
Tech: Docker, Linux, Jenkins, Makefile, Git, GitHub
This document outlines the Jenkins CI/CD pipeline essentials for automating the deployment process. Jenkins is an open-source automation server that helps automate the building, testing, and deployment of software projects.
Jenkins Guest Account
For the demo, You can use the guest account to access the Jenkins server to see the pipeline in action!
Server URL: Jenkins Server
- Username:
guest - Password:
guest
Pre-requisites
Before setting up the Jenkins pipeline, ensure the following pre-requisites are met:
- VPS: Virtual Private Server (VPS) is available for deployment.
- Docker: Docker is installed on the VPS.
- Docker Compose: Docker Compose is installed on the VPS.
- SSH Key: SSH key is generated and added to the VPS for secure access.
- Running Production Containers: Ensure that the production containers are running on the VPS (if its initial deployment).
How to install Jenkins
- Create Dockerfile
Create a file named Dockerfile and paste the following content into it:
Dockerfile
FROM jenkins/jenkins:lts
# Switch to root user to install dependencies
USER root
# Set the timezone using the TZ environment variable
ENV TZ=Asia/Bangkok
# Install curl and nano
RUN apt-get update && apt-get install -y curl nano rsync
# Add GitHub's SSH host key to known_hosts
RUN mkdir -p /var/jenkins_home/.ssh && \
ssh-keyscan github.com >> /var/jenkins_home/.ssh/known_hosts
# Set the timezone to Asia/Bangkok or Asia/Jakarta
RUN ln -fs /usr/share/zoneinfo/Asia/Bangkok /etc/localtime && \
dpkg-reconfigure -f noninteractive tzdata
# Switch to the Jenkins user
USER jenkins
# Install bun
RUN curl -fsSL https://bun.sh/install | bash
# Add bun to PATH
ENV BUN_INSTALL="/var/jenkins_home/.bun"
ENV PATH="${BUN_INSTALL}/bin:${PATH}"FROM jenkins/jenkins:lts
# Switch to root user to install dependencies
USER root
# Set the timezone using the TZ environment variable
ENV TZ=Asia/Bangkok
# Install curl and nano
RUN apt-get update && apt-get install -y curl nano rsync
# Add GitHub's SSH host key to known_hosts
RUN mkdir -p /var/jenkins_home/.ssh && \
ssh-keyscan github.com >> /var/jenkins_home/.ssh/known_hosts
# Set the timezone to Asia/Bangkok or Asia/Jakarta
RUN ln -fs /usr/share/zoneinfo/Asia/Bangkok /etc/localtime && \
dpkg-reconfigure -f noninteractive tzdata
# Switch to the Jenkins user
USER jenkins
# Install bun
RUN curl -fsSL https://bun.sh/install | bash
# Add bun to PATH
ENV BUN_INSTALL="/var/jenkins_home/.bun"
ENV PATH="${BUN_INSTALL}/bin:${PATH}"- Build Docker Image Navigate to the directory where your Dockerfile is located and run the following command to build the Docker image:
docker build -t jenkins-custom:lts .docker build -t jenkins-custom:lts .- Run Docker Container Once the image is built, you can run a container using the following command:
docker run -d -p 8060:8080 -p 50000:50000 --name jenkins custom-jenkins:ltsdocker run -d -p 8060:8080 -p 50000:50000 --name jenkins custom-jenkins:lts-d: Run the container in detached mode.-p 8060:8080: Map port 8060 on the host to port 8080 in the container.-p 50000:50000: Map port 50000 on the host to port 50000 in the container.--name jenkins: Assign the namejenkinsto the container.custom-jenkins:lts: The name of the Docker image.
- Access Jenkins
Once the container is running, you can access the Jenkins web UI by navigating to http://<your-server-ip>:8080 in your web browser.
- Unlock Jenkins
To unlock Jenkins, you will need to retrieve the initial admin password from the container. Run the following command to get the password:
docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassworddocker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPasswordoptionally, you can use the following command to copy the password to your clipboard:
docker exec jenkins /bin/bashdocker exec jenkins /bin/bashcat /var/jenkins_home/secrets/initialAdminPassword | pbcopycat /var/jenkins_home/secrets/initialAdminPassword | pbcopyCopy the password and paste it into the Jenkins web UI to unlock Jenkins.
- Install Plugins
After unlocking Jenkins, you will be prompted to install plugins. Select the "Install suggested plugins" option to install the recommended plugins.
- Create Admin User
Once the plugins are installed, you will be prompted to create an admin user. Fill in the required details to create the admin user.
- Jenkins Dashboard
After creating the admin user, you will be redirected to the Jenkins dashboard, where you can create a new pipeline.
Setting up a Jenkins Pipeline
The following Jenkins plugins are recommended for setting up the CI/CD pipeline:
Adding SSH Key to Jenkins
To add an SSH key to Jenkins, follow these steps:
- Go to
Manage Jenkins>Manage Credentials. - Click on
Jenkins>Global credentials. - Click on
Add Credentials. - Select
SSH Username with private keyas the kind. - Enter the credentials and click
OK. - If the SSH key doesn't work, you can add the SSH key to the Jenkins container by running the following command:
docker exec -it jenkins bashdocker exec -it jenkins bashssh-keyscan github.com >> /var/jenkins_home/.ssh/known_hostsssh-keyscan github.com >> /var/jenkins_home/.ssh/known_hostsRole-based Authorization Strategy
Allows you to define roles and assign permissions to users.
How to install:
- Go to
Manage Jenkins>Manage Plugins. - Click on the
Availabletab. - Search for
Role-based Authorization Strategy. - Restart Jenkins after installing the plugin, use the following command:
docker restart jenkinsdocker restart jenkins- Go to
Manage Jenkins>Security>Authorizationand selectRole-Based Strategy.
Conclusion
Setting up a Jenkins CI/CD pipeline is essential for automating the deployment process and ensuring consistency across different environments. By following the steps outlined in this document, you can create a Jenkins pipeline for your project and automate the build, test, and deployment process.
Have fun delivering your project! 🚀