LoginSignup
1
0

More than 5 years have passed since last update.

Jenkinsのグローバル環境変数をジョブから変更する

Posted at

Jenkinsのグローバル環境変数をジョブから変更する

EnvInject Pluginなんかはジョブ中の環境変数しか変えてくれません。
一方いろいろな理由でグローバルな環境変数を変更したいよーということがあります。
そういう時のやりかた

やりかた

  1. 以下のコードをPipelineジョブで貼り付けます。
  2. 環境変数名をKey,値をValueとしてパラメータを追加します。
  3. 適当な値で実行してIn-process Script Approvalのメソッドの実行を許可しまくります。(もしくはSandboxを外します。)
  4. おしまい
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*

instance = Jenkins.getInstance()

env_key = Key
env_value = Value

props = instance.getGlobalNodeProperties().getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

if(props.empty) {
  def entry = new EnvironmentVariablesNodeProperty.Entry(env_key, env_value)
  def evnp = new EnvironmentVariablesNodeProperty(entry)
  instance.getGlobalNodeProperties().add(evnp)
} else {
  for (prop in props) {
    prop.envVars.put(env_key, env_value)
  }
}

instance.save()

ただ、jenkinsさん的にはセキュリティ的にちょっと触ってほしくないクラス触ってるので別の方法ないかなーとは思いました。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0