Docker image pull Jenkins environment command

docker pull jenkins/jenkins:lts
Copy the code

View the pulled mirror

docker images
Copy the code

2. Jenkins container is constructed by container arrangement

Creating a Directory

Mkdir -p /data/ Jenkins /jenkins_home/ chown -r 1000:1000 /data/ Jenkins /jenkins_home/ + data-docker-compose // docker-compose execution file + /data/ jenkins_home // Jenkins mounts the volumeCopy the code

Docker-comemess. yml file configuration

version: "3" services: jenkins: user: root restart: "always" image: jenkins/jenkins:lts container_name: jenkins privileged: true ports: - 8080:8080 - 50000:50000 environment: JAVA_OPTS: -Duser.timezone=Asia/Shanghai volumes: - /data/jenkins/jenkins_home/:/var/jenkins_home - /data/jenkins/workspace/:/root/.jenkins/workspace \ - / usr/local/apache maven - 3.6.0: / usr/local/maven - / usr/local/maven/maven - 3.6.0 / repository: / usr/local/maven repository - / usr/local/jdk1.8.0 _211: / usr/local/JDK - / var/run/docker. The sock: / var/run/docker. The sock - / usr/bin/docker: / usr/bin/docker - /etc/localtime:/etc/localtime - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 - /root/.ssh:/root/.ssh # (Optional) In the encryption-free configuration, the public key can be configured locallyCopy the code

3. Start the container environment

To start the environment, go into the compose directory and type the following command

docker-compose up -d
Copy the code

Docker-compose other operations

The container can be saved as a new image after operations, for example, JDK is installed and environment variables are configured in the container

Docker ps # # to find out the currently running container ID according to containerId, create a new image docker commit 5 d81d56b18b6 Jenkins/Jenkins: centos. V1Copy the code

Modifying docker-compose allows you to modify the container you create based on the new image

version: "3" services: jenkins: user: root image: jenkins/jenkins:lts ports: - 8080:8080 - 50000:50000 restart: "always" container_name: jenkins environment: JAVA_OPTS: -duser. timezone=Asia/Shanghai - /home/jenkins/jenkins_home/:/var/jenkins_home - /var/run/docker.sock:/var/run/docker.sock - /usr/bin/docker:/usr/bin/docker - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose privileged: trueCopy the code

Delete the previous container

docker-compose down
Copy the code

Restart to a new container

docker-compose up -d
Copy the code

5. Configure Jenkins

Prerequisite, Jenkins is deployed to the remote server, so the Jenkins server must be registered on the remote server without secret login, and the remote server must also be registered on the Jenkins server without secret login to copy the corresponding JAR package. Therefore, before configuring Jenkins, ensure that the two servers do not log in to each other in secret. This configuration, you can see the text I wrote before [Linux system two machines mutual non-secret login], very simple.

5.1 Configuring the SSH Plug-in First

1. Install SSH related plug-ins. Directly search for SSH in the plug-in center and install the following two plug-ins

Plug-in, mainly used to link remote server global configuration (common configuration);

② Plug-in, mainly used to link remote server file configuration, configuration in the project.

Publish over SSH, system management -> System Configuration

The first marked place, if the generation of private key, no password is set, do not fill in any content can be left blank;

The second annotation, the alias of the remote server, is arbitrary;

In the third place, the address of the remote server;

In the fourth place, the account name (login name) of the remote server;

The fifth mark is the directory to place the application on the remote server. It should be noted here that the directory filled in the later project configuration is spliced with this one.

At the sixth mark, click Advanced and expand the fill-in option below.

The first note is the password for the remote server

In the second place, the private key of the remote server.

3. Project (Job) Configure the front-end VUE related to SSH plug-in

Create a new job, enter a name, select the pipeline project, and click OK.

This is just to show you how to build the configuration for this step, the rest of the configuration is easy, you can do your own Internet search.

The previous screenshot (1) corresponds to steps (1) and (2) below the screenshot.

In the first step of building the configuration, Jenkins packaged the front-end application of the project:

At the first mark in the figure above, the shell script is as follows.

#! /bin/bash CD /data/ Jenkins /jenkins_home/workspace/data/xxx-all/xxx-admin Start relative to the next level of the package project directory. ./dev-xxx npm install npm run build cd dist tar -cvf dev-xxx.tar *Copy the code

The script,

The second line indicates that the Jenkins service period switches the directory to the project project directory under the Workspace directory configured by Jenkins.

Lines 5 and 6, the front end applies the package command.

In line 7, switch to dist directory (this directory is automatically generated after Jenkins packages the front-end application, and the packaged results are all in this directory)

Line 8, package everything in the dist directory (* important) for easy transfer to a remote server (deployment server).

Build the configuration step 2, upload the packaging results to the remote server;

The above five configurations are explained as follows. If the configuration is not correct, the project cannot be deployed to the remote server.

①, the name (alias) of the remote server has been set in the global configuration.

(2) the directory address of the packaged results (source files), i.e. where Jenkins’ packaged content is placed. This directory is easy to misfill and will kill you. This directory starts from the project directory under Jenkins’ main directory, that is, it starts from the next level of the project directory.

(3) Remove the prefix or your remote server will create dist again.

(4), the remote directory, need to pay attention to here, I configure global above (public) SSH, have to fill in the application deployment of the remote directory, www.yuque.com/u21187464/c… The fifth tag here is the concatenation of directories based on global common configuration.

⑤, the command executed by the remote server, if it is a front-end VUE project, is to decompress the project packaged above, the script is as follows.

# CD /data/xxx-all/xxx-admin/dev-xx.tar # tar -zxvf dev-xx.tar # CD /data/xxx-all/xxx-admin/dev-xx.tar # tar  tar -xvf /data/xxx-all/xxx-admin/dev-xx.tar -C /data/xxx-all/xxx-admin/Copy the code

Script description,

In the first line, switch to the directory where the application is deployed on the remote server.

Second line, unpack the application package.

Here is a summary of the above configuration of the source files (Sourec Files) this directory, many pits.

After Jenkins builds, the folder corresponding to the project will be generated in the workspace directory of Jenkins (the folder name is the project name when the project is created, as shown below). At this time, there will be a folder like api-dev in the workspace directory.

Therefore, the source file directory is filled with the directory after this folder, do not fill this folder.

4. Project (job) Configure the contents related to SSH plug-in java-jar package

I’m just going to take a screenshot of it because I said that,

The API project directory generated by Jenkins’ working directory is as follows.

The jar package will be transferred to the remote server after the build is completed.

② Mark the place, the source file directory, as mentioned above, after the API project name to fill in the directory.

(3) Delete the prefix and the directory in front of the JAR package. Otherwise, the directory will be created on the remote server.

6, summary

1. After Jenkins is compiled and built, application packages (JAR packages, WAR packages, etc.) need to be deployed on the remote server, so it is necessary to ensure that Jenkins server and remote server do not have secret access to each other, as mentioned above.

2. If your Jenkins server and application server are deployed in a Docker container, you need to ensure that the two containers are free of secret login to each other. The specific configuration is the same as the configuration mentioned above, but it only needs to be operated in the container.

3. The Jenkins packaging configuration I exemplify above is the packaging result of front-end VUE, which is compressed into a compressed package and uploaded to a remote server. If API is deployed, the process is about the same, but there is no need to compress and decompress.


Journal entry

Come here to talk to me

  • QQ group: 330374464
  • ID: Testpu
  • CSDN:blog.csdn.net/mcfnhm
  • Language finch: www.yuque.com/testpu/pro