Use GitHub and Docker to cooperate with Jenkins to build a construction and deployment pipeline

Introduction: I recently learned how to build and deploy pipelines to realize automatic deployment of source code. I chose GitHub as the source code management warehouse and Jenkins as the construction engine.

Installing Jenkins server in Linux

1.1 pull the official image of Jenkins from Docker Hub

docker pull jenkins
copy

1.2 running image

docker run -itd --name jenkins_ci -u root --restart unless-stopped \
-p 8080:8080 -p 50001:50000 \
-v /usr/bin/docker:/usr/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/local/dockerinfo/jenkins:/var/jenkins_home \
jenkins/jenkins
copy

Note: because we need to run the Docker command inside the Jenkins container, we will use the Docker of the host The sock file and / usr/bin/docker are mounted inside the Jenkins container, so there is no need to install additional Docker in the Jenkins container.

Visit http://{your ip}:8080. If you respond to the login interface, the server will be installed successfully.

Configure Jenkins server

2.1 unlock Jenkins

Copy the password in {Jenkins Home}/secrets/initialAdminPassword and log in to Jenkins as root.

2.2 create administrator user and configure instance

Fill in the user name, password and mailbox according to the wizard to create an administrator user. The instance configuration uses the default configuration.

Create automated pipeline

Take the construction and deployment pipeline of Eureka Server module under my Book Store project as an example. Book Store is a multi module aggregation project of distributed micro service architecture. The project structure is as follows:

book-store    
||
||
|---eureka-server
||    |
||    ---pom.xml
||
|---pom.xml
|
---...
copy

Where, the POM of the book store root directory XML defines that book store is an aggregation parent module, and Eureka server, as the registration service of microservices, is an aggregation sub module under book store.

3.1 installing Git and Maven Integration plug-ins

Select "system management" - "plug-in management", search and download Git and Maven Integration plug-ins, and restart Jenkins after installation to make the plug-ins take effect.

3.2 using Git as source code management repository

Use SSH keygen to generate public and private key pairs:

ssh-keygen
copy

Enter the key directory, view the public key file, and copy the public key in it:

cat {jenkins_home}/.ssh/id_rsa.pub
copy

Where {jenkins_home} represents the root directory path of Jenkins.

Click "New SSH key" under the "SSH and GPG keys" menu in GitHub, fill in the public key and save it.

3.3 global credential configuration

Select system management - Manage Credentials, click Add credentials, select the credential type, and fill in the credential content of GitHub.

3.4 global tool configuration

Click "global tool configuration" to configure JDK and Maven environment.

3.5 tell Jenkins how to construct Docker image of Eureka Server application

Edit Dockerfile new file:

vim Dockerfile
copy

Dockerfile file content, take my Eureka Server as an example:

FROM java:8

MAINTAINER key "user's email address"

ADD target/eureka-server-0.0.1-SNAPSHOT.jar erksvr.jar

EXPOSE 7100

ENTRYPOINT ["java","-jar","/erksvr.jar"]
copy

Copy to the task workspace inside the Jenkins container:

docker cp Dockerfile jenkins:/var/jenkins_home/workspace/{Jenkins Task name}/
copy

3.6 creating Jenkins build and deployment tasks

Click "new task", enter the task name, select "build a Maven project", select "Git" in the "source code management" column, fill in the Git warehouse address and specify the version branch, and select the pre configured global credentials in 3.3 for the authentication credentials of GitHub.

Select "Git" in "source code management" and fill in the GitHub warehouse URL of the Book Store project and specify the branch.

In build trigger, select the default build whenever a snapshot dependency is build.

In the "Root POM" item of "Build", enter "pom.xml", which means pom.xml under the root directory of book store XML file; Fill in "Goals and options":

install -pl eureka-server
copy

Indicates that only the Eureka server module will be built.

Select "run only if build successes" in "Post Steps" and click to add the "execute shell" step. The shell script is as follows:

echo "Trying to delete existed Eureka Server docker container..."
cname="eureka-server"
cid=$(docker ps -a -f "name=${cname}"  --format {{.ID}})
if ["$cid" != ""]; then
	docker stop "$cid"
	docker rm "$cid"
fi

echo "Making Eureka Server docker image..."
cd "$WORKSPACE"/eureka-server
docker build -t eureka-server .
echo "Maken Eureka Server docker image successfully."
docker rmi $(docker images -q -f dangling=true)

echo "Running Eureka Server docker container..."
docker run -itd --name eureka-server -p 7100:7100 eureka-server
echo "Run Eureka Server docker container successfully."
copy

Click "save" to save the configuration of the task.

Use pipeline

Select the created task, click "build now" and wait for the build result. If the task is successful, it means that our build and deployment pipeline has been successfully built; If the prompt fails, check the log in the build details to check which step went wrong.

Posted by JellyFish on Sat, 07 May 2022 06:25:07 +0300