window中搭建jenkins_在Jenkins构建步骤(Windows)中,从groovy脚本访问构建环境变量...

I'm using Scriptler plugin, so I can run a groovy script as a build step. My Jenkins slaves are running on windows in service mode. With scriptler, I don't need to use windows batch scripts.

But I have trouble to get the environment variables in a build step... This is working:

System.getenv("BASE")

Where BASE is part of the env-vars on jenkins startup. However, I would like to get

%JOB_NAME%

If I'm adding an "Execute Windows batch command" build step:

echo %JOB_NAME%

It works.

If I'm adding a scriptler script as a build step with the same settings:

println "JOB_NAME: " + System.getenv("JOB_NAME")

I'm getting:

JOB_NAME: null

So how can I reach the injected environment variables from a groovy script as a build step?

解决方案

The Scriptler Groovy script doesn't seem to get all the environment variables of the build. But what you can do is force them in as parameters to the script:

When you add the Scriptler build step into your job, select the option "Define script parameters"

Add a parameter for each environment variable you want to pass in. For example "Name: JOB_NAME", "Value: $JOB_NAME". The value will get expanded from the Jenkins build environment using '$envName' type variables, most fields in the job configuration settings support this sort of expansion from my experience.

In your script, you should have a variable with the same name as the parameter, so you can access the parameters with something like:

println "JOB_NAME = $JOB_NAME"

I haven't used Sciptler myself apart from some experimentation, but your question posed an interesting problem. I hope this helps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值