sh 'alias' 没有在 Jenkinsfile 中给出任何输出

Jul*_*oro 3 jenkins docker devops jenkins-pipeline

我有一个运行 Jenkins 的 docker 容器。

在 Jenkinsfile 中,我尝试定义一个别名并打印这个别名。

我手动尝试过,连接到 Jenkins 容器,我能够做到:

alias foo='bar'
Run Code Online (Sandbox Code Playgroud)

然后,如果我执行,alias我可以看到别名列表(我有 7 个预设别名和新别名)

但是当我在 Jenkinsfile 中执行同样的事情时,我的foo命令不会响应......

这是我的管道代码:

#!/bin/groovy

pipeline {
agent any
stages {
  stage("Use alias command"){
    steps { 
        sh 'alias foo="bar"'
        sh 'foo'
    }
  }
}}
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?

BMi*_*tch 5

每个sh命令都在它自己的 shell 中运行。它在同一个代理/工作区中,但由于它是一个新的 shell,环境变量、别名等将丢失。您需要将这些行合并为一个sh

#!/bin/groovy

pipeline {
agent any
stages {
  stage("Use alias command"){
    steps { 
        sh '''
          alias foo="bar"
          foo
        '''
    }
  }
}}
Run Code Online (Sandbox Code Playgroud)