Jenkinsfile定义动态环境变量的声明性管道 [英] Jenkinsfile Declarative Pipeline defining dynamic env vars

查看:731
本文介绍了Jenkinsfile定义动态环境变量的声明性管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是詹金斯管道的新手;我正在定义一个声明性语法管道,但我不知道是否可以解决我的问题,因为我没有找到解决方案.

I'm new to Jenkins pipeline; I'm defining a declarative syntax pipeline and I don't know if I can solve my problem, because I didn't find a solution.

在此示例中,我需要将变量传递给ansible插件(在旧版本中,我使用ENV_VAR或使用inject plugin将其从文件中注入),该变量来自脚本.

In this example, I need to pass a variable to ansible plugin (in old version I use an ENV_VAR or injecting it from file with inject plugin) that variable comes from a script.

这是我的理想情况(但由于环境{}而无法使用):

This is my perfect scenario (but it doesn't work because environment{}):

pipeline {
  agent { node { label 'jenkins-node'}}

  stages {
    stage('Deploy') {
      environment {
        ANSIBLE_CONFIG = '${WORKSPACE}/chimera-ci/ansible/ansible.cfg'
        VERSION = sh("python3.5 docker/get_version.py")
      }
      steps {
        ansiblePlaybook credentialsId: 'example-credential', extras: '-e version=${VERSION}', inventory: 'development', playbook: 'deploy.yml'
      }
    }
  }
}

在其他帖子中,我尝试了其他方法来测试env vars的工作方式,例如:

I tried other ways to test how env vars work in other post, example:

pipeline {
  agent { node { label 'jenkins-node'}}

  stages {
    stage('PREPARE VARS') {
      steps {
        script {
          env['VERSION'] = sh(script: "python3.5 get_version.py")
        }
        echo env.VERSION
      }
    }
  }
}

但是"echo env.VERSION"返回null.

but "echo env.VERSION" return null.

还尝试了以下示例: -版本= python3.5 get_version.py -VERSION = python3.5 get_version.py> props.file(并尝试注入它,但没有找到方法)

Also tried the same example with: - VERSION=python3.5 get_version.py - VERSION=python3.5 get_version.py > props.file (and try to inject it, but didnt found how)

如果这不可能的话,我会扮演角色.

If this is not possible I will do it in the ansible role.

更新

Ansible插件中还有一个问题",要在额外的var中使用var,它必须有双引号而不是单引号.

There is another "issue" in Ansible Plugin, to use vars in extra vars it must have double quotes instead of single.

ansiblePlaybook credentialsId: 'example-credential', extras: "-e version=${VERSION}", inventory: 'development', playbook: 'deploy.yml'

推荐答案

您可以在管道块开始之前创建变量.您可以让sh返回stdout来分配给这些变量.在environment节中分配环境变量的灵活性不同.因此,请替换为python3.5 get_version.py,这里的脚本中有echo 0.0.1(并确保您的python脚本只是将版本返回到stdout):

You can create variables before the pipeline block starts. You can have sh return stdout to assign to these variables. You don't have the same flexibility to assign to environment variables in the environment stanza. So substitute in python3.5 get_version.py where I have echo 0.0.1 in the script here (and make sure your python script just returns the version to stdout):

def awesomeVersion = 'UNKNOWN'

pipeline {
  agent { label 'docker' }
  stages {
    stage('build') {
      steps {
        script {
          awesomeVersion = sh(returnStdout: true, script: 'echo 0.0.1')
        }
      }
    }
    stage('output_version') {
      steps {
        echo "awesomeVersion: ${awesomeVersion}"
      }
    }
  }
}

上述管道的输出为:

awesomeVersion: 0.0.1

这篇关于Jenkinsfile定义动态环境变量的声明性管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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