答: Jenkins 是一个开源持续集成的工具(CI&CD)由Java开发而成, 用于自动化各种任务,包括构建、测试和部署软件(自动化服务器); Jenkins 支持各种运行方式,可通过系统包、Docker 或者 通过一个独立的Java程序; 官方介绍 : 全球领先的开源自动化服务器,Jenkins 提供了数以百计的插件来支持构建、部署和自动化任何项目 官方标语 : “Build great things at any scale”-“建造伟大的事情以任何规模”
答: Jenkins 是一个开源持续集成的工具(CI&CD)由Java开发而成, 用于自动化各种任务,包括构建、测试和部署软件(自动化服务器); Jenkins 支持各种运行方式,可通过系统包、Docker 或者 通过一个独立的Java程序; 官方介绍 : 全球领先的开源自动化服务器,Jenkins 提供了数以百计的插件来支持构建、部署和自动化任何项目 官方标语 : “Build great things at any scale”-“建造伟大的事情以任何规模”
# 示例:用 jenkins:2.107.3 创建容器并运行。 docker volume create jenkins-data docker run --name jenkins-production \ --detach \ -p 50000:50000 \ -p 8080:8080 \ -v jenkins-data:/var/jenkins_home \ jenkins/jenkins:2.275-alpine # If run for the first time, just run the following to get the admin # password once it has finished starting docker exec jenkins-production bash -c 'cat $JENKINS_HOME/secrets/initialAdminPassword'
docker stop jenkins-production docker rm jenkins-production # just temporarily docker rename it instead if that makes you worried docker run --name jenkins-production \ --detach \ -p 50000:50000 \ -p 8080:8080 \ -v jenkins-data:/var/jenkins_home \ jenkins/jenkins:2.277-alpine
Credentials Plugin (2.3.14) - This plugin allows you to store credentials in Jenkins. Git client plugin (3.6.0) - Utility plugin for Git support in Jenkins Git plugin (4.5.0) - This plugin integrates Git with Jenkins. Gitlab Authentication plugin (1.10) - This is the an authentication plugin using gitlab OAuth. Gitlab Hook Plugin (1.4.2) - Enables Gitlab web hooks to be used to trigger SMC polling on Gitlab projects GitLab Plugin (1.5.13) - This plugin allows GitLab to trigger Jenkins builds and display their results in the GitLab UI.
# (3) 主机认证指纹绑定 jenkins@jenkins:~$ git ls-remote -h -- git@gitlab.weiyigeek.top:ci-cd/blog.git HEAD # The authenticity of host 'gitlab.weiyigeek.top (10.10.107.201)' can't be established. # ECDSA key fingerprint is SHA256:cmG5Ces+96qG9EEaU1b/tJuTR1re7XDXUU4jg7asna4. # Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
# copy 公钥 到 对端主机 ssh-copy-id -p 20211 weiyigeek@10.10.107.202 # /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/jenkins/.ssh/id_ed25519.pub" # authorized only. All activity will be monitored and reported. # weiyigeek@10.10.107.202's password:
# Number of key(s) added: 1
# Now try logging into the machine, with: "ssh -p '20211' 'weiyigeek@10.10.107.202'" and check to make sure that only the key(s) you wanted were added.
(4) 为我们的Blog打三个Tag并验证构建脚本
1 2 3 4 5 6 7 8 9 10 11 12
$ cd ~/workspace/freestyle-blog jenkins:~/workspace/freestyle-blog$ git config --global user.email "jenkins@weiygeek.top" jenkins:~/workspace/freestyle-blog$ git config --global user.name "Weiyigeek" vim index.html git add . git commit -m "v1.1" git tag v1.1 -m "Jenkins CI test - v1.1" git push origin HEAD:master git push origin v1.1 # Total 0 (delta 0), reused 0 (delta 0) # To gitlab.weiyigeek.top:ci-cd/blog.git # * [new tag] v1.1 -> v1.1
$ git clone git@gitlab.weiyigeek.top:ci-cd/java-maven.git # Cloning into 'java-maven'... # The authenticity of host 'gitlab.weiyigeek.top (10.10.107.201)' can't be established. $ ~/code/java-maven$ ls pom.xml src target
# 项目拉取 Running as SYSTEM Building in workspace /var/lib/jenkins/workspace/Maven-HelloWorld The recommended git tool is: NONE using credential b4c8b4e9-2777-44a1-a1ed-e9dc21d37f4f Cloning the remote Git repository Cloning repository git@gitlab.weiyigeek.top:ci-cd/java-maven.git
<===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /var/lib/jenkins/workspace/Maven-HelloWorld/pom.xml clean package -Dmaven.test.skip=true # 扫描项目并下载依赖 [INFO] Scanning for projects... [INFO] [INFO] -------------------< com.weiyigeek.main:hello-world >------------------- [INFO] Building hello-world Maven Webapp 1.1-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] Downloading from weiyigeek-maven: http://maven.weiyigeek.top:8081/repository/maven-public/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
# 打包应用 [INFO] Packaging webapp [INFO] Assembling webapp [hello-world] in [/var/lib/jenkins/workspace/Maven-HelloWorld/target/hello-world] [INFO] Processing war project [INFO] Copying webapp resources [/var/lib/jenkins/workspace/Maven-HelloWorld/src/main/webapp] [INFO] Webapp assembled in [26 msecs] [INFO] Building war: /var/lib/jenkins/workspace/Maven-HelloWorld/target/hello-world.war [INFO] WEB-INF/web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.756 s [INFO] Finished at: 2020-12-25T13:55:45Z [INFO] ------------------------------------------------------------------------ Waiting for Jenkins to finish collecting data [JENKINS] Archiving /var/lib/jenkins/workspace/Maven-HelloWorld/pom.xml to com.weiyigeek.main/hello-world/1.1-SNAPSHOT/hello-world-1.1-SNAPSHOT.pom [JENKINS] Archiving /var/lib/jenkins/workspace/Maven-HelloWorld/target/hello-world.war to com.weiyigeek.main/hello-world/1.1-SNAPSHOT/hello-world-1.1-SNAPSHOT.war channel stopped Finished: SUCCESS
Step 5.Jenkins CI 部署Shell脚本编写,脚本需求部署、回退、可重复构建; 脚本与权限: su - "jenkins" -c "touch /tmp/script/maven-jenkins-ci-script.sh && chmod a+x /tmp/script/maven-jenkins-ci-script.sh"
// Pipeline Maven Integration 插件使用配置文件 withMaven( // Maven installation declared in the Jenkins "Global Tool Configuration" maven:'maven-3', // Maven settings.xml file defined with the Jenkins Config File Provider Plugin // We recommend to define Maven settings.xml globally at the folder level using // navigating to the folder configuration in the section "Pipeline Maven Configuration / Override global Maven configuration" // or globally to the entire master navigating to "Manage Jenkins / Global Tools Configuration" mavenSettingsConfig:'my-maven-settings') { // Run the maven build sh "mvn clean verify"
} // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs & SpotBugs reports...
Step 1.在 Dashboard -> 系统配置 -> Jenkins Location进行配置系统管理员邮件地址,注意此处管理员邮箱地址必须与smtp服务发信地址一致否则将会报出501 mail from address must be same as authorization user错误;
# 收信人:Recipients # 什么场景发送信息: - 构建失败给每一个人发送发送电子邮件 : Send e-mail for every unstable build - 谁构建失败给谁发送邮件: Send separate e-mails to individuals who broke the build - 为每个失败的模块发送电子邮件 : Send e-mail for each failed module
BUILD_NUMBER #The current build number, such as "153" BUILD_ID # The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds BUILD_DISPLAY_NAME # The display name of the current build, which is something like "#153" by default. JOB_NAME # Name of the project of this build, such as "foo" or "foo/bar". JOB_BASE_NAME # Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo". BUILD_TAG # String of "jenkins-${JOB_NAME}-${BUILD_NUMBER}". All forward slashes ("/") in the JOB_NAME are replaced with dashes ("-"). Convenient to put into a resource file, a jar file, etc for easier identification. EXECUTOR_NUMBER # The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the "build executor status", except that the number starts from 0, not 1. NODE_NAME # Name of the agent if the build is on an agent, or "master" if run on master NODE_LABELS # Whitespace-separated list of labels that the node is assigned. WORKSPACE # The absolute path of the directory assigned to the build as a workspace. WORKSPACE_TMP # A temporary directory near the workspace that will not be browsable and will not interfere with SCM checkouts. May not initially exist, so be sure to create the directory as needed (e.g., mkdir -p on Linux). Not defined when the regular workspace is a drive root. JENKINS_HOME # The absolute path of the directory assigned on the master node for Jenkins to store data. JENKINS_URL # Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration) BUILD_URL # Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set) JOB_URL # Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set) GIT_COMMIT # The commit hash being checked out. GIT_PREVIOUS_COMMIT # The hash of the commit last built on this branch, if any. GIT_PREVIOUS_SUCCESSFUL_COMMIT # The hash of the commit last successfully built on this branch, if any. GIT_BRANCH # The remote branch name, if any. GIT_LOCAL_BRANCH # The local branch name being checked out, if applicable. GIT_CHECKOUT_DIR # The directory that the repository will be checked out to. This contains the value set in Checkout to a sub-directory, if used. GIT_URL # The remote URL. If there are multiple, will be GIT_URL_1, GIT_URL_2, etc. GIT_COMMITTER_NAME # The configured Git committer name, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.name Value field of the Jenkins Configure System page. GIT_AUTHOR_NAME # The configured Git author name, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.name Value field of the Jenkins Configure System page. GIT_COMMITTER_EMAIL # The configured Git committer email, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.email Value field of the Jenkins Configure System page. GIT_AUTHOR_EMAIL # The configured Git author email, if any, that will be used for FUTURE commits from the current workspace. It is read from the Global Config user.email Value field of the Jenkins Configure System page.
# (4) 启动 Jenkins 服务 jenkins:/usr/share/jenkins# systemctl start jenkins jenkins:/usr/share/jenkins# systemctl status jenkins # ● jenkins.service - LSB: Start Jenkins at boot time # Loaded: loaded (/etc/init.d/jenkins; generated) # Active: active (exited) since Thu 2020-12-24 02:38:50 UTC; 4s ago # Docs: man:systemd-sysv-generator(8) # Process: 448375 ExecStart=/etc/init.d/jenkins start (code=exited, status=0/SUCCESS)
# Dec 24 02:38:48 gitlab systemd[1]: Starting LSB: Start Jenkins at boot time... # Dec 24 02:38:48 gitlab jenkins[448375]: Correct java version found # Dec 24 02:38:48 gitlab jenkins[448375]: * Starting Jenkins Automation Server jenkins # Dec 24 02:38:48 gitlab su[448432]: (to jenkins) root on none # Dec 24 02:38:48 gitlab su[448432]: pam_unix(su-l:session): session opened for user jenkins by (uid=0) # Dec 24 02:38:49 gitlab su[448432]: pam_unix(su-l:session): session closed for user jenkins # Dec 24 02:38:50 gitlab jenkins[448375]: ...done. # Dec 24 02:38:50 gitlab systemd[1]: Started LSB: Start Jenkins at boot time.
问题1.jenkins depends on daemon; however Package daemon is not installed. 问题描述: 在Ubuntu 采用 dpkg 安装 jenkins_2.263.1_all.deb 时报错提示 daemon 包未安装 问题复原:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ sudo dpkg -i jenkins_2.263.1_all.deb Selecting previously unselected package jenkins. (Reading database ... 115038 files and directories currently installed.) Preparing to unpack jenkins_2.263.1_all.deb ... Unpacking jenkins (2.263.1) ... dpkg: dependency problems prevent configuration of jenkins: jenkins depends on daemon; however: Package daemon is not installed.
dpkg: error processing package jenkins (--install): dependency problems - leaving unconfigured Processing triggers for systemd (245.4-4ubuntu3.2) ... Errors were encountered while processing: jenkins
解决办法:
1
sudo apt install -y daemon
问题2:Jenkins 启动时显示 ERROR: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin 问题复原:
1 2 3 4 5 6 7
$ systemctl status jenkins Dec 23 14:02:57 gitlab systemd[1]: Starting LSB: Start Jenkins at boot time... Dec 23 14:02:57 gitlab jenkins[356298]: ERROR: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin Dec 23 14:02:57 gitlab jenkins[356298]: If you actually have java installed on the system make sure the executable is in the aforementioned path and that 'type -p ja> Dec 23 14:02:57 gitlab systemd[1]: jenkins.service: Control process exited, code=exited, status=1/FAILURE Dec 23 14:02:57 gitlab systemd[1]: jenkins.service: Failed with result 'exit-code'. Dec 23 14:02:57 gitlab systemd[1]: Failed to start LSB: Start Jenkins at boot time.
②.建立软连接:ln -s /usr/java/jdk1.8/bin/java /usr/bin/java(换成你自己的路径) Please wait while Jenkins is getting ready to work (jenkins) 如果界面提示Jenkins正在启动,请稍后…或者提示 Please wait while Jenkins is getting ready to work…
Jenkins root URL is empty but is required for the proper operation of many Jenkins features like email notifications, PR status update, and environment variables such as BUILD_URL.
Please provide an accurate value in Jenkins configuration.
问题5.无法连接仓库:Command “git ls-remote -h – git@gitlab.weiyigeek.top:ci-cd/blog.git HEAD” returned status code 128: 问题复原:
1 2 3 4
stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. # Please make sure you have the correct access rights and the repository exists.
# 例如 首次连接Gitlab时候需要进行主机于公钥绑定 ssh -T git@gitlab.com # 无法建立主机“gitlab.com(172.65.251.78)”的真实性。 The authenticity of host 'gitlab.com (172.65.251.78)' can\'t be established. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw. Are you sure you want to continue connecting (yes/no/[fingerprint])? $ cat ~/.ssh/known_hosts gitlab.com,172.65.251.78 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAABFSMqzJeV9rUzU4kWitGjeR4PWSa29SPqJ1fVkhtj3Hw9xjLVXVYrU9QOLXBpQ6KWjbjTDTdDkoohFzgbEYI=
问题6.Jenkins 内置邮件通知发信测试 Failed to send out e-mail javax.mail.AuthenticationFailedException: 535 Error: 错误信息:
1 2 3 4 5 6 7
Failed to send out e-mail javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762) at javax.mail.Service.connect(Service.java:364)
问题7.Jenkins 内置邮件通知发信测试 com.sun.mail.smtp.SMTPSenderFailedException: 501 mail from address must be same as authorization user 错误信息:
1 2 3 4 5 6 7 8 9 10 11
Failed to send out e-mail com.sun.mail.smtp.SMTPSenderFailedException: 501 mail from address must be same as authorization user at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1817) Caused: com.sun.mail.smtp.SMTPSendFailedException: 501 mail from address must be same as authorization user ; nested exception is: com.sun.mail.smtp.SMTPSenderFailedException: 501 mail from address must be same as authorization user at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285) at javax.mail.Transport.send0(Transport.java:231)
ERROR: Invalid Addresses javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 501 Bad address syntax at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:2064) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1286) at javax.mail.Transport.send0(Transport.java:231) at javax.mail.Transport.send(Transport.java:100) at hudson.tasks.MailSender.run(MailSender.java:130) at hudson.tasks.MailSender.execute(MailSender.java:105) at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.cleanUp(MavenModuleSetBuild.java:1093) at hudson.model.Run.execute(Run.java:1954) at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 501 Bad address syntax
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1917) ... 10 more Finished: FAILURE `
方式1.请访问本博主的B站【WeiyiGeek】首页关注UP主, 将自动随机获取解锁验证码。
Method 2.Please visit 【My Twitter】. There is an article verification code in the homepage.
方式3.扫一扫下方二维码,关注本站官方公众号
回复:验证码
将获取解锁(有效期7天)本站所有技术文章哟!