首页 > 解决方案 > 在 jenkins 中从 shell 脚本设置环境变量

问题描述

我正在尝试使用Jenkins. 我的构建过程需要执行三个不同的shell scripts. 第一个脚本设置了第二个和第三个脚本使用的一些。 environment variables我正在尝试pipeline在 Jenkins 中的一份工作,其中每个脚本都是分阶段执行的。但是我无法从第一个脚本获取环境变量到下一个脚本。

注意:正在设置一组变量。所以我不觉得使用简单的变量就可以了。

请帮忙

标签: jenkinsenvironment-variablesjenkins-pipelinesh

解决方案


您可能将声明性管道与脚本化管道混淆了

Jenkinsfile(声明式管道)

pipeline {
agent any

environment {
    DISABLE_AUTH = 'true'
    DB_ENGINE    = 'sqlite'
}

stages {
    stage('Build') {
        steps {
            sh 'printenv'
        }
    }
  }
}

Jenkinsfile(脚本流水线)

node {
withEnv(['DISABLE_AUTH=true',
         'DB_ENGINE=sqlite']) {
    stage('Build') {
        sh 'printenv'
    }
  }
}

推荐阅读