Jenkins内置变量
pipeline { agent any stages { stage ('mcwtest') { steps { echo "Running ${env.BUILD_NUMBER} on ${env.JENKINS_URL}" // 方法一 echo "Running $env.BUILD_NUMBER on $env.JENKINS_URL" // 方法二 echo "Running ${BUILD_NUMBER} on ${JENKINS_URL}" // 方法三 echo "========>printenv:" sh "printenv" } } } }
可以看到三种方式都获取到了内置变量
我们看下打印的所有内置变量
Started by GitLab push by Administrator Obtained Jenkinsfile from git http://10.0.0.13/root/javademo.git [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/hello-world-pipeline [Pipeline] { [Pipeline] stage [Pipeline] { (Declarative: Checkout SCM) [Pipeline] checkout The recommended git tool is: NONE using credential 104b2169-f64c-4c0d-a1d0-22e43914f73e > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/hello-world-pipeline/.git # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://10.0.0.13/root/javademo.git # timeout=10 Fetching upstream changes from http://10.0.0.13/root/javademo.git > git --version # timeout=10 > git --version # 'git version 1.8.3.1' using GIT_ASKPASS to set credentials > git fetch --tags --progress http://10.0.0.13/root/javademo.git +refs/heads/*:refs/remotes/origin/* # timeout=10 skipping resolution of commit remotes/origin/main, since it originates from another repository > git rev-parse refs/remotes/origin/main^{commit} # timeout=10 Checking out Revision eacde33e2e2ca4b80ed81d6f2fef9736f46cbdf0 (refs/remotes/origin/main) > git config core.sparsecheckout # timeout=10 > git checkout -f eacde33e2e2ca4b80ed81d6f2fef9736f46cbdf0 # timeout=10 Commit message: "Update Jenkinsfile" > git rev-list --no-walk 277bb380ff8b4ab792f3234ad8c432ff9306ddaa # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (mcwtest) [Pipeline] echo Running 41 on http://10.0.0.25:8080/ [Pipeline] echo Running 41 on http://10.0.0.25:8080/ [Pipeline] echo Running 41 on http://10.0.0.25:8080/ [Pipeline] echo ========>printenv: [Pipeline] sh + printenv JENKINS_NODE_COOKIE=a34f85be-8e93-4dec-b86d-aaf97f8c3e4f BUILD_URL=http://10.0.0.25:8080/job/hello-world-pipeline/41/ SHELL=/bin/false HUDSON_SERVER_COOKIE=b318ad38d4dc712d STAGE_NAME=mcwtest BUILD_TAG=jenkins-hello-world-pipeline-41 GIT_PREVIOUS_COMMIT=277bb380ff8b4ab792f3234ad8c432ff9306ddaa [email protected]:root/javademo.git JOB_URL=http://10.0.0.25:8080/job/hello-world-pipeline/ WORKSPACE=/var/lib/jenkins/workspace/hello-world-pipeline RUN_CHANGES_DISPLAY_URL=http://10.0.0.25:8080/job/hello-world-pipeline/41/display/redirect?page=changes gitlabBefore=277bb380ff8b4ab792f3234ad8c432ff9306ddaa USER=jenkins gitlabTargetBranch=main RUN_ARTIFACTS_DISPLAY_URL=http://10.0.0.25:8080/job/hello-world-pipeline/41/display/redirect?page=artifacts gitlabMergeRequestLastCommit=eacde33e2e2ca4b80ed81d6f2fef9736f46cbdf0 JENKINS_HOME=/var/lib/jenkins GIT_COMMIT=eacde33e2e2ca4b80ed81d6f2fef9736f46cbdf0 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin RUN_DISPLAY_URL=http://10.0.0.25:8080/job/hello-world-pipeline/41/display/redirect _=/usr/bin/printenv PWD=/var/lib/jenkins/workspace/hello-world-pipeline gitlabSourceNamespace=Administrator HUDSON_URL=http://10.0.0.25:8080/ LANG=en_US.UTF-8 gitlabSourceRepoHttpUrl=http://10.0.0.13/root/javademo.git gitlabAfter=eacde33e2e2ca4b80ed81d6f2fef9736f46cbdf0 JOB_NAME=hello-world-pipeline gitlabUserName=Administrator gitlabSourceRepoName=Javademo BUILD_DISPLAY_NAME=#41 gitlabActionType=PUSH JENKINS_URL=http://10.0.0.25:8080/ BUILD_ID=41 NOTIFY_SOCKET=/run/systemd/notify GIT_PREVIOUS_SUCCESSFUL_COMMIT=277bb380ff8b4ab792f3234ad8c432ff9306ddaa JOB_BASE_NAME=hello-world-pipeline RUN_TESTS_DISPLAY_URL=http://10.0.0.25:8080/job/hello-world-pipeline/41/display/redirect?page=tests gitlabUserUsername=root SHLVL=2 HOME=/var/lib/jenkins gitlabSourceBranch=main gitlabSourceRepoHomepage=http://10.0.0.13/root/javademo GIT_BRANCH=origin/main CI=true WORKSPACE_TMP=/var/lib/jenkins/workspace/hello-world-pipeline@tmp EXECUTOR_NUMBER=1 JENKINS_SERVER_COOKIE=durable-ac58e08ad80c9815284465d058cecee77a7bcc018cc857b2be762f1e77af8f73 NODE_LABELS=built-in GIT_URL=http://10.0.0.13/root/javademo.git LOGNAME=jenkins HUDSON_HOME=/var/lib/jenkins NODE_NAME=built-in JOB_DISPLAY_URL=http://10.0.0.25:8080/job/hello-world-pipeline/display/redirect BUILD_NUMBER=41 HUDSON_COOKIE=5e1354a6-51ee-4ec5-a9d4-6afeca268b6c gitlabBranch=main [email protected]:root/javademo.git [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
自定义pipeline环境变量
日常
pipeline { agent any environment { CC = 'python' } stages { stage ('mcwtest') { environment { DEBUG_FLAGS = '-V' } steps { echo "${CC}" echo "${DEBUG_FLAGS}" sh "${CC} ${DEBUG_FLAGS}" echo "========>printenv:" sh "printenv" } } } }
没有clang命令,所以执行失败,pipeline失败了,后面的打印环境变量也没执行
比如修改为python -V
都往下执行了
查看自定义的环境变量
一个环境变量引用另一个环境变量
pipeline { agent any environment { __server_name = 'mail-server' __virsion = "$BUILD_NUMBER" __artifact_name = "${__server_name}-${__virsion}.jar" } stages { stage ('mcwtest') { environment { DEBUG_FLAGS = '-V' } steps { echo "${__server_name}" echo "${__artifact_name}" } } } }
environment中定义的变量与env中变量重名时
为避免命名冲突,可以在变量前加__下划线
正常情况下,是有这个内置变量的
我们自定义将它覆盖
报错
加下划线不覆盖Jenkins内置变量
还是报错,是我们缺少步骤了
添加上步骤
自定义的覆盖内置变量
下面再执行shell命令,直接卡住了
卡了四分多钟
自定义全局环境变量
系统设置里,添加全局环境变量
打印定义的全局环境变量
成功获取到它的值
构建工具
jdk环境安装
全局工具安装,需要输入Oracle账号密码
maven
配置的maven,默认有个本地仓库,在里面存了一份
/var/lib/jenkins/.m2/repository/org/example/demo1/0.0.1-SNAPSHOT/demo1-0.0.1-SNAPSHOT.war
标签:10.0,pipeline,git,http,Pipeline,world,Jenkins,环境变量 From: https://www.cnblogs.com/machangwei-8/p/18333786