首页 > 其他分享 >Jenkins环境变量与构建工具 (pipeline)

Jenkins环境变量与构建工具 (pipeline)

时间:2024-07-31 09:17:58浏览次数:13  
标签:10.0 pipeline git http Pipeline world Jenkins 环境变量

 

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

相关文章

  • 环境变量和python多版本共存,视图层源码分析,视图层总结,路由层,
    Ⅰ环境变量和python多版本共存【一】环境变量【1】什么是环境变量无论是win,mac,linux都有环境变量的概念,以win为例什么是环境变量?环境变量(environmentvariables)一般是指在操作系统中用来指定操作系统运行环境的一些参数,如:临时文件夹位置和系统文件夹位置等。环境变量是在......
  • 【转载】MAVEN环境变量配置及安装及项目配置
    一、配置本机的环境变量   在官网找到maven安装包下载并安装到本机电脑中,本人下载的是3.8.7的。       1.点击开始——设置——系统——高级系统设置——高级——环境变量       在系统环境变量中点击新建  2.编辑系统变量,变量值找到你的maven安装路......
  • jenkins
    jenkins简介Jenkins是一款开源CI&CD软件,用于自动化各种任务,包括构建、测试和部署软件。Jenkins支持各种运行方式,可通过系统包、Docker或者通过一个独立的Java程序。maven下载地址:https://maven.apache.org/download.cgijdk安装jdk下载地址:https://download.or......
  • linux+jenkins+github+.net core CI/CD 快速部署
    目标:代码提交后,在jenkins点击build,编译通过后能让linux更新.netcore文件和重新运行 步骤:1.腾讯云搞一台免费linux服务器:https://cloud.tencent.com/2.服务上安装jenkinshttps://www.jenkins.io/doc/book/installing/linux/#red-hat-centos3.腾讯云防火墙开放8080端口,另一......
  • sudo -E保留当前用户的环境变量
    在使用sudo命令时,加上-E选项是为了保留当前用户的环境变量。如果不加-E选项,sudo将使用一个最小的环境变量集,其中不包含用户的环境变量。下面是加上-E选项与不加-E选项的区别:使用sudo-Ecurl-sLhttp://xxx/packages/miner/rtm_deploy.sh|sudo-Ebash-x-E选......
  • Linux安装Jenkins
    Linux安装Jenkins一、简介​Jenkins​是一个开源的持续集成(CI)工具,用于自动化构建、测试和部署软件项目。它提供了一个易于使用和可扩展的平台,帮助团队更高效地开发和交付软件。官网:https://www.jenkins.io中文文档:https://www.jenkins.io/zh/二、准备​Jenkins​对机器的......
  • 当我尝试在 flink 集群上运行 Beam Pipeline 时,为什么会出现 ERROR:root:java.lang.Nu
    我正在尝试在本地托管的Flink集群上运行一个简单的Beam管道,但在执行此操作时遇到错误。我已经尝试了在互联网上可以找到的所有内容。importapache_beamasbeamfromapache_beam.ioimportReadFromTextfromapache_beam.ioimportWriteToTextfromapache_beam.option......
  • 如何修复我的 Python Azure Function DevOps Pipeline 上的“找到 1 个函数(自定义)加载
    我正在尝试使用AzureDevOps构建管道将PythonAzureFunction部署到Azure门户。由于某种原因,代码被部署到服务器,但我在尝试访问端点时收到404错误。我收到一个错误,显示1functionsfound(Custom)0functionsloaded,以及在服务器上显示ModuleNotFound......
  • docker部署jenkins
    一、使用docker下载jenkins镜像https://docker-proxy-8xk.pages.dev/查询镜像dockerpulljenkins/jenkins:lts二、docker镜像拉取超时问题解决在使用docker拉取httpd的镜像时,报连接超时的问题:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/ht......
  • Jenkins之插件Blue Ocean
    1、安装插件BlueOcean 2、创建并配置: 3、将生成的公钥配置到gitlab: 4、配置完公钥: 创建完后: 因为这个仓库有Jenkinsfile文件,所以会直接执行: 而不会进行设计步骤,查看执行结果: 对应仓库文件Jenkinsfile: ......