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

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

问题描述

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

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 或使用注入插件从文件中注入它)该变量来自脚本.

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'
      }
    }
  }
}

我在其他帖子中尝试了其他方法来测试环境变量的工作方式,例如:

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"返回空值.

but "echo env.VERSION" return null.

还尝试了相同的示例:- VERSION=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)

如果这是不可能的,我会以 ansible 的角色来做.

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

更新

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

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 返回标准输出以分配给这些变量.您没有同样的灵活性来分配 environment 节中的环境变量.因此,在 python3.5 get_version.py 中替换我在此处的脚本中具有 echo 0.0.1 的位置(并确保您的 python 脚本仅将版本返回到标准输出):

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').trim()
        }
      }
    }
    stage('output_version') {
      steps {
        echo "awesomeVersion: ${awesomeVersion}"
      }
    }
  }
}

上述管道的输出为:

awesomeVersion: 0.0.1

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

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