Jenkins 管道中的导出命令 [英] Export command in Jenkins pipeline

查看:22
本文介绍了Jenkins 管道中的导出命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Jenkins 管道中添加导出"unix 命令?我有一个詹金斯的舞台"和步骤".导出命令的语法是什么.我需要使用 export 命令设置环境变量PATH".

How to add an 'export' unix command in a Jenkins pipeline? I have a Jenkins 'stage' and 'steps' within it. What is the syntax for an export command. I need to set the environment variable 'PATH' using the export command.

推荐答案

你可以像这样更新$PATH:

pipeline {
  agent { label 'docker' }
  stages {
    stage ('build') {
      steps {

        // JENKINSHOME is just a name to help readability
        withEnv(['PATH+JENKINSHOME=/home/jenkins/bin']) {
          echo "PATH is: $PATH"
        }
      }
    }
  }
}

当我运行它时,结果是:

When I run this the result is:

[Pipeline] echo
PATH is: /home/jenkins/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

这个 PATH+JENKINSHOME 语法到底是什么?引用 designhammer.com 上的博客:

What the heck is this PATH+JENKINSHOME syntax? Quoting from a blog at designhammer.com:

这个:

/my/additional/path:$PATH

表示为:

PATH+ANYSTRING=/my/additional/path.

ANYSTRING 只是一个有助于提高可读性的名称.如果它不利于可读性,在您看来,您可以省略它.所以这是等价的:

ANYSTRING is just a name to help readability. If it doesn't help readability, in your view, you can omit it. So this is equivalent:

PATH+=/my/additional/path

上述 (withEnv) 允许您为管道的特定部分更新 $PATH.要更新整个管道的 $PATH,您不能使用 PATH+ANYSTRING 语法,但这有效:

The above (withEnv) allows you to update the $PATH for a specific part of your pipeline. To update the $PATH for the entire pipeline, you can't use the PATH+ANYSTRING syntax, but this works:

pipeline {
  agent { label 'docker' }
  environment {
    PATH = "/hot/new/bin:$PATH"
  }
  stages {
    stage ('build') {
      steps {
        echo "PATH is: $PATH"
      }
    }
  }
}

产生输出:

[Pipeline] echo
PATH is: /hot/new/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

这篇关于Jenkins 管道中的导出命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆