如何在jenkinsFIle jenkins 2中为构建过程插件调用注入环境变量。x管道

时间:2021-05-19 23:08:03

I'm trying to migrate my project from jenkins 1 to jenkins 2.x using pipeline as code or Jenkinsfile.
But I don't see any option in snippet generator to generate environment injector plugin into a script in Jenkinsfile.

我想把我的项目从jenkins 1移到jenkins 2。使用管道作为代码或Jenkinsfile。但是我没有看到在代码片段生成器中有任何选项可以在Jenkinsfile中生成环境注入器插件。

Anyone can help?

任何人都可以帮忙吗?

3 个解决方案

#1


2  

I'm assuming that you want to read properties from a specific file and inject them as environment variables?

我假设您想要从一个特定的文件读取属性并将其注入到环境变量中?

If so, this is a solution:

如果是这样,这是一个解决方案:

  • Create the file that will contain the environment properties

    创建包含环境属性的文件。

    • You create some properties file called project.properties with following content:

      创建一个名为project的属性文件。属性与以下内容:

      PROJECT_VERSION='1.4.34'

      PROJECT_VERSION = ' 1.4.34 '

Then, on your pipeline code, you've to add the following code in order to be able to read the file and inject read variables as environment variables:

然后,在您的管道代码中,您必须添加以下代码,以便能够读取文件并将读取的变量注入到环境变量中:

node { load "${WORKSPACE}\\project.properties" // assuming that props file is in Jenkins Job's workspace echo "PROJECT VERSION: ${PROJECT_VERSION}" }

节点负载“$ { WORKSPACE } { \ \项目。属性“//假设道具文件在Jenkins工作的工作区echo”项目版本:${PROJECT_VERSION}

  • First line read and inject variable PROJECT_VERSION as environment variable
  • 第一行读取并注入变量PROJECT_VERSION作为环境变量。
  • Second line is just to print read variable to make sure that everything worked seamlessly
  • 第二行是打印读取变量,以确保所有操作都是无缝的。

Result:

结果:

如何在jenkinsFIle jenkins 2中为构建过程插件调用注入环境变量。x管道

#2


1  

Wanted to just comment on your question, but my lack of reputation is hindering me.

我想就你的问题发表评论,但是我的声誉不佳妨碍了我。

The list of supported steps is here: https://jenkins.io/doc/pipeline/steps/

支持的步骤列表在这里:https://jenkins.io/doc/ine/steps/。

In general, you can use other plugins by using their Java style invocation.

通常,您可以使用它们的Java风格调用来使用其他插件。

i.e.

即。

step([$class: 'classname', parametername: 'value'])

#3


0  

I used this example for read a properties file and use it in a pipeline stage:

我使用这个示例来读取属性文件,并将其用于管道阶段:

node(){
    file = readFile('params.txt')
    prop=[:]
    file.eachLine{ line ->
      l=line.split("=")
      prop[l[0]]=l[1]
   }
   withEnv(['MyVar1='+prop["MyVar1"],'MyVar2='+prop["MyVar2"],'MyVar3='+prop["MyVar3]]){        
     stage('RUN_TEST'){
       echo env.MyVar1
       echo env.MyVar2
       echo env.MyVar3
       sh"echo $MyVar1"
     }
  }
}

#1


2  

I'm assuming that you want to read properties from a specific file and inject them as environment variables?

我假设您想要从一个特定的文件读取属性并将其注入到环境变量中?

If so, this is a solution:

如果是这样,这是一个解决方案:

  • Create the file that will contain the environment properties

    创建包含环境属性的文件。

    • You create some properties file called project.properties with following content:

      创建一个名为project的属性文件。属性与以下内容:

      PROJECT_VERSION='1.4.34'

      PROJECT_VERSION = ' 1.4.34 '

Then, on your pipeline code, you've to add the following code in order to be able to read the file and inject read variables as environment variables:

然后,在您的管道代码中,您必须添加以下代码,以便能够读取文件并将读取的变量注入到环境变量中:

node { load "${WORKSPACE}\\project.properties" // assuming that props file is in Jenkins Job's workspace echo "PROJECT VERSION: ${PROJECT_VERSION}" }

节点负载“$ { WORKSPACE } { \ \项目。属性“//假设道具文件在Jenkins工作的工作区echo”项目版本:${PROJECT_VERSION}

  • First line read and inject variable PROJECT_VERSION as environment variable
  • 第一行读取并注入变量PROJECT_VERSION作为环境变量。
  • Second line is just to print read variable to make sure that everything worked seamlessly
  • 第二行是打印读取变量,以确保所有操作都是无缝的。

Result:

结果:

如何在jenkinsFIle jenkins 2中为构建过程插件调用注入环境变量。x管道

#2


1  

Wanted to just comment on your question, but my lack of reputation is hindering me.

我想就你的问题发表评论,但是我的声誉不佳妨碍了我。

The list of supported steps is here: https://jenkins.io/doc/pipeline/steps/

支持的步骤列表在这里:https://jenkins.io/doc/ine/steps/。

In general, you can use other plugins by using their Java style invocation.

通常,您可以使用它们的Java风格调用来使用其他插件。

i.e.

即。

step([$class: 'classname', parametername: 'value'])

#3


0  

I used this example for read a properties file and use it in a pipeline stage:

我使用这个示例来读取属性文件,并将其用于管道阶段:

node(){
    file = readFile('params.txt')
    prop=[:]
    file.eachLine{ line ->
      l=line.split("=")
      prop[l[0]]=l[1]
   }
   withEnv(['MyVar1='+prop["MyVar1"],'MyVar2='+prop["MyVar2"],'MyVar3='+prop["MyVar3]]){        
     stage('RUN_TEST'){
       echo env.MyVar1
       echo env.MyVar2
       echo env.MyVar3
       sh"echo $MyVar1"
     }
  }
}