background

Jenkins’s official website

Build great things at any scale

The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.

It is an open source automated deployment server written in Java for persistent integration.

Specific steps

  • Projects and remote warehouses
    • Push the latest code to the remote repository
  • Remote warehouse and Jenkins
    • Periodically get the latest complete project downloaded locally from the remote repository
  • Jenkins and the remote server
    • Upload to the remote server via Jenkins

The installation

First add its Debian package, then update the repository, and finally install Jenkins using the repository apt-get.

Install operating environment

Install the JDK

sudo apt-get install openjdk-8-jdk
Copy the code

Installation completed as follows

update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/appletviewer to provide /usr/bin/appletviewer (appletviewer) in auto mode update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/jconsole to provide /usr/bin/jconsole (jconsole) in auto mode Processing Triggers for LibC-bin (2.23-0ubuntu10)... Processing Triggers for Systemd (229-4ubuntu21.2)... Processing Triggers for ureadahead (0.100.0-19)... Processing Triggers for CA-certificates (20170717~16.04.1)... Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done. Running hooks in /etc/ca-certificates/update.d... done. done.Copy the code

Add repository keys to the system

wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
Copy the code

Ok is displayed, adding succeeded

Debian package repository address added to the serversources.list

echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
Copy the code

Add success as follows

deb http://pkg.jenkins.io/debian-stable binary/
Copy the code

Update repository

sudo apt-get update
Copy the code

The update succeeded as follows

Ign:17 http://pkg.jenkins.io/debian-stable binary/ InRelease Get:18 http://pkg.jenkins.io/debian-stable binary/ Release [2042 B] Get:19 http://pkg.jenkins.io/debian-stable binary/ Release.gpg [181 B] Ign:20 https://repo.mongodb.org/apt/ubuntu xenial/mongo - org/InRelease Hit 4.0:21 https://repo.mongodb.org/apt/ubuntu Xenial/mongo - org / 4.0 Release the Get: 23 http://pkg.jenkins.io/debian-stable binary/Packages Fetched 6813 kB in [14.7 kB] 3s (2187 kB/s) Reading package lists... DoneCopy the code

Install Jenkins

sudo apt-get install jenkins
Copy the code

The installation is successful as follows

perl: warning: Falling back to a fallback locale ("en_US.UTF-8"). locale: Cannot set LC_ALL to default locale: No such file or directory Selecting previously unselected package daemon. (Reading database ... 136881 files and directories currently installed.) Preparing to unpack ... / daemon_0. 6.4 1 _amd64. Deb... Unpacking the daemon (0.6.4-1)... Selecting previously unselected package jenkins. Preparing to unpack ... / jenkins_2. 164.2 _all. Deb... Unpacking Jenkins (2.164.2)... Processing Triggers for man-DB (7.5-1)... Processing Triggers for Systemd (229-4ubuntu21.2)... Processing Triggers for ureadahead (0.100.0-19)... Setting up daemon (0.6.4-1)... Setting up Jenkins (2.164.2)... Processing Triggers for Systemd (229-4ubuntu21.2)... Processing Triggers for ureadahead (0.100.0-19)...Copy the code

Example Change the default port 8080

  • Modify the/etc/init.d/jenkinsThe script
sudo vim /etc/init.d/jenkins
Copy the code

Modify $HTTP_PORT to the desired port

# Verify that the jenkins port is not already in use, Winstone does not exit # even for BindException check_tcp_port "HTTP" "$HTTP_PORT" "1314" "$HTTP_HOST" "0.0.0.0" | | return 2 # If the var MAXOPENFILES is enabled in /etc/default/jenkins then set the max open files to the # proper valueCopy the code
  • Modify the/etc/default/jenkinsfile
sudo vim  /etc/default/jenkins
Copy the code

Modify HTTP_PORT to the desired port

ration, build records,
#   that sort of things.
#
#   If commented out, the value from the OS is inherited,  which is normally 022 (as of Ubuntu 12.04,
#   by default umask comes from pam_umask(8) and /etc/login.defs

# UMASK=027

# port for HTTP connector (default 8080; disable with -1)
HTTP_PORT=1314


# servlet context, important if you want to use apache proxying
PREFIX=/$NAME

# arguments to pass to jenkins.
# --javahome=$JAVA_HOME
# --httpListenAddress=$HTTP_HOST (default 0.0.0.0)
# --httpPort=$HTTP_PORT (default 8080; disable with -1)
# --httpsPort=$HTTP_PORT
# --argumentsRealm.passwd.$ADMIN_USER=[password]
     
Copy the code
  • Restarting the Server
sudo systemctl restart jenkins
Copy the code

There is a bug after the restart

Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Copy the code

Solutions:

systemctl daemon-reload
Copy the code
systemctl start jenkins
Copy the code

The new port is 1314

Start the Jenkins

sudo systemctl start jenkins
Copy the code
  • Test either direct IP + port or 0.0.0.0:8080 access with the following commandjenkins
sudo systemctl status jenkins
Copy the code
  • The successes are as follows:
● Jenkins. Service-lsb: Start Jenkins at boot time Loaded: Loaded (/etc/init.d/ Jenkins; bad; Vendor PRESET: Enabled) Active: Active (exited) since four 2019-04-18 09:00:28 CST; 8h ago Docs: man:systemd-sysv-generator(8) Process: 1136 ExecStart=/etc/init.d/jenkins start (code=exited, Status =0/SUCCE 4月 18 09:00:24 systemd[1]: Starting LSB: Start Jenkins 4月 18 09:00:27 Devue-system-product-name Jenkins [1136]: 4月 18 09:00:27 devue-system-product-name Jenkins [1136]: * Starting Jenkins Aut 4月 18 09:00:27 Devue-system-product-name su[1511]: Successful su for Jenkins by 4月 18 09:00:27 devue-system-product-name su[1511]: +?? Root: Jenkins 4月 18 09:00:27 devue-system-product-name su[1511]: pam_unix(su:session): Sessio 4月 18 09:00:28 Devue-system-product-name Jenkins [1136]:... Done. 4月 18 09:00:28 devue-system-product-name Systemd [1]: Started LSB: Start JenkinsCopy the code
  • The default Jenkins port is 8080. You need to unlock Jenkins

  • Enter the password you copied and click Continue
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the code
  • Click to install the suggested plug-in

  • Start installation, it will take a long time

  • When the installation is complete, you will be prompted to set the management user. You can skip it, but the password is unknown. It’s better to create users.

  • After the user is created, enter the instance configuration

  • Welcome to the Jenkins panel to begin our show

Jenkins can’t log in or the page is blank

Solution:

vim /var/lib/jenkins/config.xml
Copy the code

Join after the version information

<authorizationStrategy class="hudson.security.AuthorizationStrategy$Unsecured"/>
  <securityRealm class="hudson.security.SecurityRealm$None"/>
Copy the code

Restart the server.

sudo systemctl restart jenkins
Copy the code

Connect the lot

Action on Jenkin: Install related plug-ins

  • Install the ‘Publish Over SSH’ plug-in to connect to the remote server.

The image below is after installation

  • Install the GitHub Integration Plugin

Action on Github: Configure Webhook

  • Click on the Github projectSettingsTAB
    • Click on thewebhookA menu item
      • addwebhook

Payload URLIn thehttp://+ Jenkins deployment IP and port number +/github-webhook/Content typeSelect theapplication/jsonWhich events would you like to trigger this webhook?choosejust the push event– chooseActive

– click on theUpdate webhook

Jenkins pulled the Vue code from Github and started it locally

  • A new task
    • Task name optional
    • chooseBuild a free-style software projectAnd the lastdetermine

  • Bind to the Github project

  • Bind the download link for the project

  • chooseGit
  • inRepositoriesIn theRepository URLFill in the project download link (HTTP)
  • CredentialsTo add identity information
    • Select from the typeUsername with password
    • The username and password are the Github account and password, and finallydetermine

  • Branches to Build Selected Branches to deploy (*/ Branch name)

  • Build trigger

    • chooseGitHub hook trigger for GITScm polling

  • build
    • Execution of the shell
      • The command
      cd /var/lib/jenkins/workspace/vue 
      npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver 
      npm install
      npm run dev
      Copy the code

      • Command can not be missing, otherwise the package is incomplete
          npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver 
      Copy the code
  • save

Mission creation is complete. Jenkins is done

Jenkins pulled the Vue code from Github and started it on a remote server

  • Connecting to the Remote Server
    • System > System Settings >Publish over SSH
      • Passphrase: Enter Jenkins’ password
      • Key: Jenkins private Key
      • Name: indicates the IP address of the server
      • Hostname: indicates the server IP address
      • Username: indicates the Username on the server
      • Remote Directory: project address
      • Click on theTest ConfigurationAppear,success, then the connection is successful

  • Remote server
    • Set up the public key

Obtain the public and private keys

su jenkins
ssh-keygen -t rsa
Copy the code

Determines whether public and private keys are generated

ls -l /var/lib/jenkins/.ssh/
Copy the code
jenkins@devue-System-Product-Name:/home/devue$ls -l /var/lib/jenkins/. Ssh/total 8 -rw------- 1 Jenkins Jenkins 1675 4月 19 16:26 ID_rsa-rw-r --r-- 1 Jenkins Jenkins 415 4月 19 16:26 id_rsa.pubCopy the code

Copy the public key

cd /var/lib/jenkins/.ssh/
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys
Copy the code

Put the public key into the remote server authorized_keys

  vim ~/.ssh/authorized_keys 
Copy the code
  • Build immediately

The transport failureHere’s the card. Thanks for your help

Connection gitlab

Source code management

Repository URLAn HTTP request is required

build

The installationGitlab Hook

An error will be reported if Gitlab Hook is not installed or webhook is added to Gitlab

  • Gitlab project sidebarSettings-Integrationsincreasewebhook

  • addGitlab HookThe plug-in

Jenkins poll GitLab (must be administrator)

When Jenkins wants to execute the next build task, he has to wait for the previous task to complete (not checking concurrent execution).

Due to NPM run dev, the timing of the build is not as good as it should be. The requirements are as follows: Maintain the process and perform the following steps periodically:

  • Plus pM2 build project, PM2 entry

  • Choose between a trigger, a timed build or a polling SCM

  • The execution shell in the build is
cd /var/lib/jenkins/workspace/ceres-cms-vue
npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver
npm install
npm run build
npm run pm2
pm2 list
Copy the code

Warehouse code updates are built automatically

  • Do not check the timed build and poll SCM
  • Check theGitHub hook trigger for GITScm polling

So your Jenkins can automatically build along with your code

polling

  • Timed Build: Builds on time with or without the latest code
  • Polling SCM: Builds whenever there is an update to the code

  • Construct syntax description:

    • First format: * * * * * (five stars)
options You mean
The first * stands for minutes The value 0 ~ 59
The second * is the hour The value 0 ~ 23
The third * indicates the day of the month The value of 1 ~ 31
The fourth * is the month The value 1 ~ 12
The fifth * represents the day of the week The value ranges from 0 to 7, where 0 and 7 indicate Sunday
  • Example (before time without H) :
options You mean
Build every 1 minute H/1 * * * *
Build every 1 hour H H/1 * * *
Build once on the 1st of every month H H 1 * *
  • The use of timing construction and polling SCM do not conflict with each other. The specific combination needs to be properly configured according to the project situation.

test

  • Local push code to Github

  • Click on theBuild immediately

  • Build execution state

  • Click on the# 13To enter the project details

  • Console output
Started by user unknown or anonymous Started by user unknown or anonymous Started by user unknown or anonymous Building in workspace /var/lib/jenkins/workspace/vue using credential 12dc8386-52e8-4c57-b667-bd8d263626cd > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url https://github.com/qiufeihong2018/vuepress-app.git # timeout=10 Fetching upstream changes from https://github.com/qiufeihong2018/vuepress-app.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --tags --progress https://github.com/qiufeihong2018/vuepress-app.git +refs/heads/*:refs/remotes/origin/* > git  rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} #  timeout=10 Checking out Revision ab0c6b6de9b810dcd9fd107c6329d1e782054976 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f ab0c6b6de9b810dcd9fd107c6329d1e782054976 Commit message: "Merge branch 'master' of github.com:qiufeihong2018/vuepress-app" > git rev-list --no-walk 9b7e2475ffaef9a60cc38cec1c660d0f9d8dc490 # timeout=10 [vue] $ /bin/sh -xe /tmp/jenkins5471132310334499324.sh + cd /var/lib/jenkins/workspace/vue + npm install chromedriver - chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver > [email protected] install /var/lib/jenkins/workspace/vue/node_modules/chromedriver > node install.js ChromeDriver binary exists. Validating... ChromeDriver 73.0.3683.20 (8 e2b610813e167eee3619ac4ce6e42e3ec622017) ChromeDriver is already available at '/ TMP / 73.0.3683.20 chromedriver/chromedriver. Copying to target path /var/lib/jenkins/workspace/vue/node_modules/chromedriver/lib/chromedriver Fixing file permissions Done. ChromeDriver binary available at /var/lib/jenkins/workspace/vue/node_modules/chromedriver/lib/chromedriver/chromedriver npm WARN [email protected] No repository field. NPM WARN [email protected] scripts['server'] should probably be Scripts ['start'].npm WARN optional SKIPPING Optional DEPENDENCY: [email protected] (node_modules/fsevents): NPM WARN Notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {" OS ":" Linux "," ARCH ":" X64 "}) + [email protected] updated 1 Package and Audited 14738 packages in 13.642s found 15 vulnerabilities (1 low, 7 moderate, 7 high) run `npm audit fix` to fix them, Or 'NPM audit' for details + NPM install NPM WARN [email protected] No repository field. NPM WARN [email protected] scripts['server'] should probably be scripts['start']. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents): NPM WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {" OS ":" Darwin ","arch":"any"} (current: {" OS ":" Linux "," ARCH ":" X64 "}) Audited 14738 Packages in 8.37s found 15 December (1 low, 7 Moderate, 7 high) run `npm audit fix` to fix them, The or ` NPM audit ` for details + NPM run dev > [email protected] dev/var/lib/Jenkins/workspace/vue > vuepress dev docs WAIT Extracting site metadata... <button @click.ctrl="onClick">A</button> <button @click.ctrl.exact="onCtrlClick">A</button> <button @click.exact="onClick">A</button> TIP override.styl has been split into 2 APIs, we recommend you upgrade to continue. See: https://v0.vuepress.vuejs.org/default-theme-config/#simple-css-override [2:52:53 PM] Compiling Client [2:52:56 PM] Compiled Client in 4s c DONE [14:52:56] Build 854bb5 finished in 3536 ms! > VuePress dev server listening at http://localhost:7777/ c[2:52:57 PM] Compiling Client [2:52:57 PM] Compiled Client in  204ms c DONE [14:52:57] Build 056d13 finished in 208 ms! (http://localhost:7777/)Copy the code
  • Project started successfully
  • After modification is submitted, clickBuild immediately, you can run the latest committed code

Fixed an issue where the spawn process was killed automatically after the build was completed

  • Modify theBUILD_ID

Jenkins kills spawn processes triggered by shell commands during the build process by default after the build is complete. Jenkins uses BUILD_ID to identify whether a process is a derivative process of the build process. Therefore, after changing BUILD_ID, Jenkins cannot identify whether a process is a derivative process and the process can be kept running in the background

OLD_BUILD_ID=$BUILD_ID echo $OLD_BUILD_ID BUILD_ID=dontKillMe cd /var/lib/jenkins/workspace/ceres-cms-vue npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver npm install npm run build npm run pm2 pm2  list BUILD_ID=$OLD_BUILD_ID echo $BUILD_IDCopy the code

Kill the spawn process that changes the ID

netstat -lntp
kill -g {id}
Copy the code

reference

How to install Jenkins on Ubuntu 16.04

Uninstalling Jenkins (Windows/Linux/MacOS)

Jenkins forgot the admin account password

How to build Jenkins+Github Continuous Integration environment

Integrate with GitHub: build after each commit (Get started with Jenkins, part 13)

Jenkins builds a strong front-end automation workflow

Teach you to use Vue, GitLab, Jenkins, Nginx automatic packaging release online

Jenkins+ Github front-end automation deployment

Jenkins automates building vUE projects and publishing them to remote servers

Jenkins Automated release of front-end code VUE (configuration mode)

Issue with Jenkins configuration publish over SSH

Jenkins reported Message Auth fail when connecting to an application machine using the Publishover SSH plugin

Jenkins+ Git + Webhook automatically triggers deployment and test tasks

Jenkins regularly builds and polls SCM setup instructions

CICD (3) – Jenkins + Gitlab + webhooks + publish-over-ssh (1)

Jenkins does not configure the right to login or blank page solution

Jenkins solved the problem of automatically killing spawn processes after the build is complete

This article was first published on the public account “Front-end Keep”. Welcome to follow it.

Finally, I hope you must point to like three times.

Follow my blog: blog address