一、下载Jenkins
docker pull jenkins/jenkins:lts
二、启动Jenkins
docker run \-u root \--rm \-d \-p 8081:8080 \-p 50000:50000 \-v /root/docker/jenkins/var/jenkins_home:/var/jenkins_home \-v /var/run/docker.sock:/var/run/docker.sock \-v /usr/bin/docker:/usr/bin/docker \-v /usr/lib64/libltdl.so.7:/usr/lib/libltdl.so.7 \--name jenkins \jenkins/jenkins:lts
三、配置镜像加速器
cd /root/docker/jenkins/var/jenkins_home/
vi hudson.model.UpdateCenter.xml
目录是启动时挂载的目录,然后将url修改https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json ,内容如下:
四、验证Jenkins
ip + 端口 等待一段时间
http://192.168.32.16:8081
等待之后就是登录界面,docker logs -f jenkins 可以查看启动时的临时密码:
继续安装插件,网络问题,插件安装堪忧啊~~后续使用还是得将插件 安装好。
五、配置jenkins中文模式
Jenkins----基于 CentOS 或 Docker 安装部署Jenkins并完成基础配置_centos7 docker jenkins-CSDN博客
六、pipeline脚本语句
pipeline {agent anyenvironment {NAME = 'CXB' // 全局变量,会在所有的stage中生效//AWS_ACCESS_KEY_ID = credentials('txt1')//AWS_SECRET_ACCESS_KEY = credentials('txt2')}options {timeout(time: 1, unit: 'HOURS') //超时时间1小时,如果不加unit参数默认为1分timestamps() buildDiscarder(logRotator(numToKeepStr: '3')) //保留三个历史构建版本quietPeriod(10) // 流水线静默期,也就是触发流水线后等待一会在执行。 注意手动触发的构建不生效//retry(3) // 流水线失败重试次数}parameters {string(name: 'DEPLOY_ENV', defaultValue: 'st', description: '执行构建时需要手动配置字符串类型参数,之后赋值给变量')text(name: 'DEPLOY_TEXT', defaultValue: 'One\nTwo\nThree\n', description: '执行构建时需要提供文本参数,之后赋值给变量')booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '布尔型参数')choice(name: 'CHOICES', choices: ['one', 'two', 'three'], description: '选择形式列表参数')//imageTag(name: 'DOCKER_IMAGE', image: 'kubernetes/kubectl', filter: '.*', defaultTag: '', registry: 'https://192.168.32.16', credentialId: 'harbor-account', tagOrder: 'NATURAL', description: '获取镜像名称与tag')gitParameter(name: 'BRANCH', branch: 'main', branchFilter: 'origin/(.*)', defaultValue: 'main', description: 'Branch for build and deploy, 获取git仓库分支列表,必须有git引用', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH')}triggers {cron('H */10 * * 1-5') //周一到周五每隔四个小时执行一次//cron('H/2 * * * *') //每隔2分钟执行一次//cron('H * * * *') //每隔1小时执行一次//目前支持的状态有 SUCCESS、UNSTABLE、FAILURE、NOT_BUILT、ABORTED 等。//upstream(upstreamProjects: 'Build', threshold: hudson,model,Result.SUCCESS) //当env构建成功时构建这个流水线}stages {stage("Build") {environment { // 定义在stage 中的变量只会在当前stage生效MY_URL = "WWW.BAIDU.COM"}agent {docker "maven:3-alpine"}input {message "还继续么?"ok "继续"submitter "alice,bob"parameters {string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'who should say hello')}}steps {//echo "${AWS_ACCESS_KEY_ID}"//echo "${AWS_SECRET_ACCESS_KEY}"echo 'hello maven'sh 'mvn -version'echo "${MY_URL}"echo "${NAME}"echo "hello,${PERSON}, nice to meet you."echo "$env"echo "$env.BUILD_ID"echo "$env.BUILD_NUMBER"echo "$env.BUILD_TAG"}}stage('Git') {steps {git branch: "${BRANCH}", credentialsId: 'ebe42f70-146f-4a4b-8090-eded24a77173', url: 'https://gitlab.e-ai-edu.com/zswl-developer/bi/deepbi.git' //使用gitParameter,必须有这个}}stage('Test') {agent { docker 'openjdk:8-jre' }//options { //定义在这里这对这个stage生效// timeout(time: 2, unit: 'SECONDS') //超时时间2秒// timestamps() //所有输出每行都会打印时间戳// retry(3) //流水线失败后重试次数//}steps {echo 'Hello, JDK'sh 'java -version'echo "${NAME}"sh "env"}}stage('When') {when {anyOf {branch 'main' //多分支流水线,分支为才会执行。environment name: 'DEPLOY_ENV', value: 'st'}}steps {echo 'Deployinng 66666'}}}post {success {echo 'success 1234'}failure {echo 'failure 5678'}always {echo 'i will always say 999'}}
}
史上最全 Jenkins Pipeline流水线详解 - 知乎