开始前最好,将docker升级到最新版本(至少大于17)
docker pull jenkins
(我安装的是Jenkins 2.275)mkdir -p /data/jenkins_home/
chown -R 1000:1000 /data/jenkins_home/
docker run -d --name jenkins -p 7900:8080 -p 50000:50000 -v /data/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins
xxx:7900
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
docker exec -it jenkins /bin/bash
):cat /data/jenkins_home/secrets/initialAdminPassword
docker exec -it -u root jenkins bash
cp /etc/apt/sources.list /etc/apt/sources.list.bak
lsb_release -c
,并且将下文中替换文件中的对应位置进行修改vim /etc/apt/sources.list
,如果无法拉取:sudo apt install apt-transport-https ca-certificates
,vim中使用shift+insert插入W: Unable to read /etc/apt/preferences.d/ - DirectoryExists (2: No such file or directory)
:这时新建此文件夹即可mkdir /etc/apt/preferences.dapt update
,然后apt install vim
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
apt-get install nodejs
,一定要先更新源apt-get install npm
,npm config set registry http://registry.npm.taobao.org/
docker inspect jenkins
,其中的Env项中包含了JAVA_HOME
等环境信息,如下所示"Env": [
"PATH=/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"JAVA_HOME=/usr/local/openjdk-8",
"JAVA_VERSION=8u242",
"JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u242-b08/OpenJDK8U-jdk_",
"JAVA_URL_VERSION=8u242b08",
"JENKINS_HOME=/var/jenkins_home",
"JENKINS_SLAVE_AGENT_PORT=50000",
"REF=/usr/share/jenkins/ref",
"JENKINS_VERSION=2.275",
"JENKINS_UC=https://updates.jenkins.io",
"JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental",
"JENKINS_INCREMENTALS_REPO_MIRROR=https://repo.jenkins-ci.org/incrementals",
"COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log"
]
Jenkins -> 系统管理 -> 全局工具管理
docker exec -it -u root jenkins bash
wget https://mirrors.aliyun.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tar -zvxf apache-maven-3.6.3-bin.tar.gz
vi /etc/profile
(安装vi:apt-get install vi
)export MAVEN_HOME=/opt/apache-maven-3.6.3
export MAVEN_HOME
export PATH=$PATH:$MAVEN_HOME/bin
settings.xml
来替换源(位置conf/
下),在
标签内添加一下任意一个<mirror>
<id>alimavenid>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<mirrorOf>centralmirrorOf>
mirror>
source /etc/profile
Jenkins -> 系统管理 -> 全局工具管理
继续进行Maven配置docker exec -it -u root jenkins bash
apt-get update && \
apt-get -y install apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common && \
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
$(lsb_release -cs) \
stable" && \
apt-get update && \
apt-get -y install docker-ce
docker
来检查是否成功docker exec -it -u root jenkins /bin/bash
ls -l /var/run/docker.sock
jenkins
用户:chown jenkins: /var/run/docker.sock
ls -l /var/run/docker.sock
docker ps
之类的命令是否可以使用docker exec -it jenkins /bin/bash
ssh-keygen
,一路回车使用默认值cat /var/jenkins_home/.ssh/id_rsa.pub
settings->SSH and GPG keys
中添加密钥。settings -> webhook
,选择Add Webhook
xxx(jenkins访问网址)/github-webhook/
Just the push event.
和Active
后保存即可避免出现如下报错:pending—Waiting for next available executor
系统管理 -> 节点管理
配置主从节点
,设置至少为2个。新建任务
,新建一个名为Jenkins-Demo
的流水线任务构建触发器
选择GitHub hook trigger for GITScm polling
jenkinsfile
文件放置在仓库根目录下)
定义
选择Pipeline script from SCM
SCM
选择Git
Repository URL
输入对应仓库的[email protected]
地址git ls-remote -h -- [email protected]:xxx.git HEAD
:并输入yes
确认无法连接仓库:Command "git ls-remote -h -- [email protected]:xxx.git HEAD" returned status code 128:
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
main
,整体如下图所示立即构建
来查看效果git ls-remote -h -- ssh://[email protected] HEAD
,第一次是需要进入容器执行该命令,并输入yes)Cobertura Plugin
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>cobertura-maven-pluginartifactId>
<configuration>
<formats>
<format>xmlformat>
formats>
<check/>
configuration>
plugin>
mvn cobertura:cobertura
生成相应的测试报告,默认路径为**/target/site/cobertura/
post {
always {
step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/target/site/cobertura/coverage.xml', failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false, enableNewApi: true])
}
}
jacoco
插件System.clearProperty("hudson.model.DirectoryBrowserSupport.CSP")