This article aims to implement semi-automatic project deployment based on Jenkins + Docker without further ado.

Required environment

  • A virtual machine, the company VIRTUAL machine I use, in a virtual machine environmentJenkinsCan always up, not every time to use the time up, if not, local can also be implemented, need to boot when the bootJenkins.
  • The virtual machine isJavaEnvironment (also required if localJavaEnvironment), here firstDemo implementation in virtual machine.
  • The source repository isYards cloud

A, install,

Run the following command in Linux:

  • wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key
  • sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
  • sudo apt-get update
  • sudo apt-get install jenkins

Start after successful installation:

systemctl start jenkins

Modify theJenkinsConfiguration file:

vi /etc/sysconfig/jenkins

To viewJenkinsCheck whether the startup is successful:

systemctl status jenkins

Status displayed after successful startup:

Startup failure:

If the startup fails, the following error message is displayed: Job for jenkins.service failed because the control process exited with error code. See “systemctl status jenkins.service” and “journalctl -xe” for details.

Run the systemctl status jenkins.service command as prompted to check the Jenkins service status:

Cause analysis: Note that /usr/bin/java is the default Java path in the Jenkins configuration file, but the Java path installed on the host is not this path, so we need to replace the Jenkins configuration path:

  1. which javaTo viewJavaLocation and copy:
which java
Copy the code

  1. Modify theJenkinsIn the configuration fileJavaPath:
vi /etc/init.d/jenkins 
Copy the code

findJenkinsIn theJavaSet the path to that of the current host displayed in Step 1JavaPath:

  1. According to theiKey to edit, and then save to exit editing, and then restartJenkinsCan.

Ii. Jenkins configuration

After the above startup is successful, a random password will be generated. The browser will visit localhost:8080, visit Jenkins page, and copy the generated password to enter.

Home page select load plug-in and download.

Configuration items

1. InstallGiteePlugins, select Jenkins’ root directory:

Manage Jenkins => Manage Plugins go to the plugin download pageCopy the code

2. The queryGiteePlug-in and download:

Configuration of 3.GiteeInformation:

Manage Jenkins => Manage Plugins => Configure System => Gitee configurationCopy the code
  • Link name: enter the name you want, this name does not matter, just for display
  • GiteeThe domain nameURL: Enter the complete URL address of the code cloud,https://gitee.com (the domain name that the code cloud privatized customer enters to deploy), generally fill in https://gitee.com
  • Certificate token: Click on the right optionadd, the choice ofjenkins.
  • Domain: Selects global credentials(Global credentials)
  • type(Kind): chooseGitee APIThe token(Gitee API Token)
  • The scope of(Scope): Depending on where you personally want this credential to be available, I chose global
  • Gitee APIV5Private token(Gitee API Token): Enter your code cloud private token to get the addresshttps://gitee.com/profile/personal_access_tokens
  • IDAnd description: Fill in whatever you want
  • When you’re done, select the token you just added yourself
  • Advanced: Can be configured whether to ignoreSSLWrong (for youJenkinsEnvironment support), and can set the link test timeout (depending on your network environment), I was ignored.
  • Test connection: click to confirm whether your configuration is correct, if not, reconfigure.

4. Create a new project and choose free style:

5. Configure General:

6. Source management optionsgit:

If the following problem occurs: Because there is nojenkinsThe public key of the server is configured on the code cloud

Solution:

Ssh-keygen -t rsa // Generate a public key cat /root/.ssh/id_rsa.pub Viewing a public key The following figure is a screenshot of the public key document obtained from the code cloud. For details, see: https://gitee.com/help/articles/4181#article-header0Copy the code

Configuration on the code cloud:

No error is reported after the public key is configured.

7. Build triggers:

Check the Gitee Webhook to trigger build push code, comment on submission information, and create Pull Requests [Ci-skip] directives to skip the build PR does not require filtering when testing build filters The Commit version that has been built allows all branches to trigger build generated Gitee WebHook Password configuration to the code cloud WebHookCopy the code

JenkinsConfiguration:

Yards cloudwebhookConfiguration:

8. Build the environment:

Select Provide Node & NPM bin/ Folder to PATHCopy the code

If you don’t have a Node environment, you need to configure the Node installation:

Manage Jenkins => Global Tool Configuration => NodeJS
Copy the code

9. Construction:

The last construction step: select Execute Shell to add the construction script. Here, take Docker packaging as an example to generate the docker image sourceCopy the code

Shell script, the following command is ok in the VIRTUAL machine environment, if the Windows local command, also need other, do not repeat here:

Docker build-t [project name]: 2020-01-21-10-15-11.2 [project name]: 2020-01-21-10-15-11-T tag [domain name] / [space] / [project name]:Copy the code

At this point, it generatesdockerMirror source, published to the container, personal understanding of the implementationCI/CDIn theCIAutomatic deployment needs to be improved.

If extended, fully automatic deployment is shown in the figure abovebuildafterdisttoossornginx, of course, other operation and maintenance colleagues need to cooperate to achieve.