Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker+Jenkins to build deployment automatically

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Docker+Jenkins to build deployment automatically", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Docker+Jenkins to build deployment automatically" this article.

This article introduces the automatic build and deployment of Docker+Jenkins, which is shared with you, as follows:

Environment

Aliyun ESC, host server installs Docker, and confirm that port 8080 is enabled in the security rules.

Client mac

Run jenkins

Run the jenkins container

Create a directory on the host and add read and write permissions to read and write files while the jenkins application is running, such as:

$mkdir-p / var/jenkins_node$ chmod 777 / var/jenkins_node

Pull jenkins image: docker pull jenkins, currently version 2.60.3. And run:

Docker run-d-- name myjenkins-p 8080 var/jenkins_home jenkins 8080-p 50000 var/jenkins_home jenkins 50000-v:

Mount the previous directory as a data volume. Replace with your directory name, the path needs to be an absolute path.

Wait tens of seconds and check the jenkins_node directory to see if there are any files generated by the jenkins application.

Log in to view via http://you_host:8080. Whether the Getting Started interface appears.

Set up account and log in to SSH

The initial password Unlock Jenkins will be required in the Getting Started interface.

The password will be found in the output terminal or in the jenkins_home of the container as prompted on the page.

So you can

Docker logs myjenkins# or enter the container docker exec-t myjenkins / bin/bash

With the password, type in and install the recommended plug-in.

When finished, follow the prompts to set up a login account.

Install the Publish Over SSH plug-in

Home page-> Click system Administration-> manage plug-ins-> optional plug-ins-> filter: ssh-> Select the Publish Over SSH plug-in, and click install directly.

Set server SSH information

First generate the rsa key in the container:

# enter the container from the host customer and the name of the container is myjenkins. You can also check $docker exec-it myjenkins / bin/bash# through docker ps to create a .ssh directory, create a private key file, id_rsa, and the public key id_rsa.pub~ mkdir ~ / .ssh & & cd ~ / .ssh ~ ssh-keygen-t rsa# can always enter.

Add a public key to the host

Add the string in id_rsa.pub to the end of the authorized_keys file and restart the ssh service sudo service ssh restart

Pay attention to whether the ssh service is enabled by the host.

You can use the following command in the container terminal to add to the host. You can also manually copy the id_rsa.pub to the. ssh/authorized_keys file of the host.

Ssh-copy-id-I ~ / .ssh/id_rsa.pub @

You need to modify the ssh configuration file of the target server, which is / etc/ssh/sshd_config. Set ssh-server to allow login using private and public key pairs, and then restart the ssh service using the sudo / etc/init.d/ssh restart command.

Add private key

Jenkins homepage, system Administration-> system Settings-> drop down, find Publish over SSH, fill in Key and SSH Server-> Save

Advanced options allow you to configure ssh server ports and timeouts. The Test is testable and shows that the success configuration is successful.

Project configuration

First, create a new task. Fill in the project name.

Select Git as the source code management, and fill in the URL of the project library. A Git account needs to be added for private projects.

Build environment: select Send files or execute commands over SSH after the build runs, select server, and add Exec command. Save.

# write sudo docker stop according to your project needs | | true\ & & sudo docker rm | | true\ & & cd / var/jenkins_node/workspace/\ & & sudo docker build-- rm-- no-cache=true-t-

< Dockerfile \ && sudo docker run -d --name -p 3000:3000 -v /var/jenkins_node/workspace/node:/home/project 端口设置的3000,也可以另行设置-p 宿主机端口:容器端口,记得确认服务器端口权限是否开启。 Dockerfile # 根据你的项目需要编写FROM nodeRUN mkdir -p /var/www/html/ RUN npm install -g yarnWORKDIR /var/www/htmlEXPOSE 3000CMD ["npm","start"] ps: npm install -g cnpm --registry=https://registry.npm.taobao.org 返回首页,选择项目,立即构建。成功后可以通过http://you_host:3000端口查看项目。 配置webhook 配置webhook,实现自动部署 获取API tonken:首页 ->

User-> Select current user-> Settings-> Click Show API Token... in the API Tonken tab

Add token: return to home page-> Project-> configuration-> build trigger-> Select "trigger remote build"-> paste "API Token" content to "Authentication token"

Log in to the code hosting platform, find your project, select Management, select webhook, add URL, format http://:/job//build?token=

Set jenkins security policy

Home page-> system Administration-> Configure Global Security->

For authorization policy, check Allow anonymous read access.

At this point, complete the automated build and deployment. When you push the code, it will be built and deployed automatically.

Deployment error lookup

Code push and webhook are not a big problem, and you can't go wrong by following the example.

What goes wrong with the container is the image construction and operation, as well as the communication between containers.

Image build: view the jenkins home page-- > View the project-- > the most recent construction history-- > View the Console Output and view the console output. Here you can see which step went wrong. Generally, Sending build context to Docker daemon is preceded by a jenkins command problem, followed by a Dockerfile problem.

Image operation: if Console shows that the construction of the image is successful (which can also be viewed by docker images), or if the container fails to run, or the container exits after running. If the code runs well locally, it is usually due to CMD startup command error. The foreground runs the container, docker run-it-- name / bin/bash. After entering the container, run CMD manually to see the log output. In addition, docker exec-t / bin/bash can enter the running container to easily view the code; docker logs displays the log output of the run.

These are all the contents of the article "how to build deployment automatically using Docker+Jenkins". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report