前言
本文简单来说就是对jenkins官方案例文档的一个“翻译”
注意,初学jenkins,请直接看英文文档~
一、安装jenkins
我用的是mac,以下命令基于mac,windows去看下官方文档,部分命令有差别
- 打开一个终端
- 创建network
docker network create jenkins
- 创建docker:dind容器,为了在容器中再执行docker命令,(官方抄的,可能有更新)
docker run --name jenkins-docker --rm --detach \ --privileged --network jenkins --network-alias docker \ --env DOCKER_TLS_CERTDIR=/certs \ --volume jenkins-docker-certs:/certs/client \ --volume jenkins-data:/var/jenkins_home \ --publish 3000:3000 --publish 5000:5000 --publish 2376:2376 \ docker:dind --storage-driver overlay2
- 创建一个定制化的jenkins镜像
a. 新建一个文本文件,命名为Dockerfile,内容如下:(官方抄的,可能有更新)
b. 构建这个镜像FROM jenkins/jenkins:2.361.2-jdk11 USER root RUN apt-get update && apt-get install -y lsb-release RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \ https://download.docker.com/linux/debian/gpg RUN echo "deb [arch=$(dpkg --print-architecture) \ signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \ https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list RUN apt-get update && apt-get install -y docker-ce-cli USER jenkins RUN jenkins-plugin-cli --plugins "blueocean:1.25.8 docker-workflow:521.v1a_a_dd2073b_2e"
docker build -t myjenkins-blueocean:2.361.2-1 .
- 启动jenkins,下面这个命令我简单改了下,和官方有点区别
docker run -u root \ --name jenkins-blueocean --detach \ --network jenkins --env DOCKER_HOST=tcp://docker:2376 \ --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \ --publish 8080:8080 --publish 50000:50000 \ --volume jenkins-data:/var/jenkins_home \ --volume jenkins-docker-certs:/certs/client:ro \ --volume "$HOME":/home \ --restart=on-failure \ --env JAVA_OPTS="-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true" \ myjenkins-blueocean:2.361.2-1
二、访问jenkins
这步比较简单,直接看官网也行
- 登录http://localhost:8080
- 创建用户
三、Fork官方案例
这步也简单,看官网吧。自己弄个github账号,然后fork官方的案例就行。然后再克隆到本地,建议直接用github客户端克隆,这样你会把这个方法案例下载到这里:
/Users/XXXXXXX/Documents/GitHub/simple-java-maven-app
四、创建项目并运行
接下来就可以上手了。
- 新建任务,给个名字,选择流水线
- 接下来主要配置流水线,这里最需要注意的就是Repositories URL,要填docker里面的地址:
/home/Documents/GitHub/simple-java-maven-app
- 新建Jenkinsfile
在克隆下来的代码中,新建一个文件,命名为Jenkinsfile,内容如下:pipeline { agent { docker { image 'maven:3.8.1-adoptopenjdk-11' args '-v /root/.m2:/root/.m2' } } options { skipStagesAfterUnstable() } stages { stage('Build') { steps { sh 'mvn -B -DskipTests clean package' } } stage('Test') { steps { sh 'mvn test' } post { always { junit 'target/surefire-reports/*.xml' } } } stage('Deliver') { steps { sh './jenkins/scripts/deliver.sh' } } } }
- 执行git命令,如果改了Jenkinsfile,一定要commit下
git add . git commit -m "修改 Jenkinsfile"
- 然后去Open Blue Ocean,点运行就搞定了。
总结
- system property ‘hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT’ to true.
–env JAVA_OPTS=“-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true” \ - Invalid agent type “docker” specified. Must be one of [any, label, none]
You have to install 2 plugins: Docker plugin and Docker Pipeline. - stderr: fatal: ‘/Users/billyshen/Documents/GitHub/simple-java-maven-app’ does not appear to be a git
这里要填docker容器的路径:/home/Documents/GitHub/simple-java-maven-app - Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
权限问题:https://blog.csdn.net/ZY_FlyWay/article/details/124652089 - error during connect: Post “https://docker:2376/v1.24/images/create?fromImage=maven&tag=3.8.1-adoptopenjdk-11”: dial tcp: lookup docker on 127.0.0.11:53: no such host
要先启动 docker:dind
参考文档:
Jenkins官方文档执行的几个错误解决