In the last article, we talked about the minimum implementation of dockerfile , but it also raised a problem. Even if docker solves the environment and deployment problems, CICD still makes people feel tired. This chapter generates jenkins container through docker and combines jenkins with github , to achieve a certain degree of CI
desired effect
I hope I push the code to jenkins after git push, click publish in jenkins, I can push my code to the server, I browse the page, and I can see the effect immediately
git push to the repository, and click publish on jenkins to publish.
Jenkins binds remote warehouse and server
After git push to the warehouse, jenkins listens and executes the script
Delete the original container and generate a new container to map out the port
Jenkins Quick Start
It is cicd written in java language,
It is a set of cicd system written in java, which can deal with the server through ssh
Realize ideas
jenkins
Implementation steps
Step 1: Pull the jenkins image to generate a container for the image
Step 2: Install and configure jenkins
Step 3: Prepare the node service
Step 4: Jenkins deploys the node service
Step 1: Generate a jenkins container
First pull jenkins from dockerhub
docker pull jenkins/jenkins:lts
Create a jenkins directory
mkdir /home/johan/www/jenkins
Elevate permissions on this file
sudo chmod -R 777 /home/johan/www/jenkins
Generate a container with jenkins/jenkins:lts as the image
docker run --name jenkins -p 8080:8080 -p 50000:50000 -d -v /home/johan/www/jenkins:/var/jenkins_home jenkins/jenkins:lts
-d
: running in the background
-v
: Data volume, which maps the contents of the container to the local machine
View container internal logs
docker logs jenkins
Enter the domain name URL to see if the installation is successful jenkins
See unlocking jenkins, enter the password in the logs to unlock
Step 2: Install and configure jenkins
Install the jenkins plug-in according to the recommended plug-in installation
fit jenkins
It takes a while to download these plugins
If an error is reported, try again, kaka is dry
Create an administrator account
Save successfully, start using jenkins
change source
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
Go to System Management -> Plugin Management -> Advanced -> Pull to the bottom
download ssh plugin
Go to System Administration -> Plugin Management -> Optional Plugins
Search for "Publish over SSH" and install
configure ssh
Go to System Management -> System Configuration -> Pull to the bottom (Publish over SSH)
use password
Click Test Configuration, it has been connected to the server
Install the nodejs plugin
The node plugin needs to be used when running the node project. Install it first here.
Go to System Administration -> Plugin Management -> Optional Plugins
Search for node and install it
Configure node globally
Go to System Administration -> Global Tools Configuration -> NodeJS
apply and save it
The construction of jenkins has come to an end
Step 3: Prepare the node service
Let's take the koa mentioned in the previous chapter as an example to explain
Generate .gitignore
file and write node_modules
Upload dockerfile_koa_demo to github
git init
git add .
git commit -m 'first_commit'
git remote add origin https://github.com/johanazhu/dockerfile_koa_server.git
git push -u origin master
Step 4: Jenkins deploys the node service
Create a task
task name
mission details
code show as below:
docker stop koa_server_container
docker rm koa_server_container
docker rmi johanbo/koa_server:v.1.0.0
cd /home/johan/www/jenkins/workspace/dockerfile_koa_server
docker build . -t johanbo/koa_server:v1.0.0
docker run -d --name koa_server_container -p 3011:3010 johanbo/koa_server:v1.0.0
Interpret the above code:
Line 1: Stop the koa_server_container container
Second line: delete this container
Line 3: Delete the image that generated this container
The fourth line: enter the directory relative to the jenkins data volume in the server
Note: dockerfile_koa_server
the name you used to create the project
Line 5: Generate an image named johanbo/koa_server:v1.0.0
The sixth line: generate a container named koa_server_container with johanbo/koa_server:v1.0.0 as the mirror, and map the 3011 port of the local machine with the 3010 of the container
Apply and save
Build a service now
The discovery is successful, and you can see "hello, docker"
Change the original file app.js locally
app.use(async (ctx) => {
ctx.body = 'hello, docker, jenkins';
});
Submit code to github
Deploy the node service again through jenkins
The build is complete, refresh the browser, and find that the content has been replaced
PS: This service is now off the shelf
Summarize
In actual development, I actually stepped on a lot of pits. Although there are good tutorials written by others, there are various limitations when doing it yourself, such as different operating systems, secret keys, SSH plug-ins of jenkins, and docker commands are supported in jenkins plugin integration issues, etc.
There are also many tutorials about the two types on the Internet, but most of them are default: jenkins and the application are deployed on the same server, so that the mirror can be built in the corresponding file,
But what if jenkins and the application are deployed on two servers? leave it to the next challenger
Reference article
- How to build serverless applications with Jenkins, Docker and CI/CD?
- [Hands-on series] Jenkins+Docker automated deployment of vue projects
- Failed to add SSH key. Message "invalid privatekey" problem
- Problems encountered when jenkins configures publish over ssh
- Build, Publish, Deploy and Test Docker images and containers with Jenkins Workflow
- Building Docker Images using Jenkins step by step | Devops Integration Live Demo | JavaTechie
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。