Skip to content
Permalink
Browse files

[JENKINS-29863] Add dedicated support for extra variables in build wi…

…dget and pipeline
  • Loading branch information
jcsirot committed Feb 3, 2016
1 parent 245bb4a commit b8fc86d508104d494abf99bff125492a193668e1
@@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.580.1</version>
<version>1.596.1</version>
</parent>

<groupId>org.jenkins-ci.plugins</groupId>
@@ -133,7 +133,7 @@
<maven-release-plugin.version>2.5.2</maven-release-plugin.version>
<ssh-credentials.version>1.10</ssh-credentials.version>
<credentials.version>1.16.1</credentials.version>
<workflow-step-api.version>1.4.2</workflow-step-api.version>
<workflow-step-api.version>1.10</workflow-step-api.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<assertj.version>1.7.1</assertj.version>
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Jean-Christophe Sirot <sirot@chelonix.com>
* Copyright 2015-2016 Jean-Christophe Sirot <sirot@chelonix.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,15 +17,16 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.ArgumentListBuilder;
@@ -47,6 +48,7 @@
protected boolean sudo;
protected String sudoUser;
protected StandardUsernameCredentials credentials;
protected List<ExtraVar> extraVars;
protected String additionalParameters;

private FilePath key = null;
@@ -100,6 +102,31 @@ public ArgumentListBuilder appendForks(ArgumentListBuilder args) {
return args;
}

public T setExtraVars(List<ExtraVar> extraVars) {
this.extraVars = extraVars;
return (T) this;
}

public ArgumentListBuilder appendExtraVars(ArgumentListBuilder args) {
if (extraVars != null && ! extraVars.isEmpty()) {
for (ExtraVar var : extraVars) {
args.add("-e");
String value = envVars.expand(var.getValue());
if (Pattern.compile("\\s").matcher(value).find()) {
value = Util.singleQuote(value);
}
StringBuilder sb = new StringBuilder();
sb.append(envVars.expand(var.getKey())).append("=").append(value);
if (var.isHidden()) {
args.addMasked(sb.toString());
} else {
args.add(sb.toString());
}
}
}
return args;
}

public T setAdditionalParameters(String additionalParameters) {
this.additionalParameters = additionalParameters;
return (T) this;
@@ -17,6 +17,7 @@

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.List;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
@@ -74,6 +75,7 @@

public String additionalParameters = null;

public List<ExtraVar> extraVars;

@Deprecated
public AnsibleAdHocCommandBuilder(String ansibleName, String hostPattern, Inventory inventory, String module,
@@ -149,6 +151,11 @@ public void setAdditionalParameters(String additionalParameters) {
this.additionalParameters = additionalParameters;
}

@DataBoundSetter
public void setExtraVars(List<ExtraVar> extraVars) {
this.extraVars = extraVars;
}

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
try {
@@ -168,6 +175,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @Nonnull Launc
invocation.setCredentials(StringUtils.isNotBlank(credentialsId) ?
CredentialsProvider.findCredentialById(credentialsId, StandardUsernameCredentials.class, run) :
null);
invocation.setExtraVars(extraVars);
invocation.setAdditionalParameters(additionalParameters);
invocation.setHostKeyCheck(hostKeyChecking);
invocation.setUnbufferedOutput(unbufferedOutput);
@@ -95,6 +95,7 @@ protected ArgumentListBuilder buildCommandLine()
appendSudo(args);
appendForks(args);
appendCredentials(args);
appendExtraVars(args);
appendAdditionalParameters(args);
return args;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Jean-Christophe Sirot <sirot@chelonix.com>
* Copyright 2015-2016 Jean-Christophe Sirot <sirot@chelonix.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.List;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
@@ -82,6 +83,8 @@

public boolean copyCredentialsInWorkspace = false;

public List<ExtraVar> extraVars;

@Deprecated
public AnsiblePlaybookBuilder(String ansibleName, String playbook, Inventory inventory, String limit, String tags,
String skippedTags, String startAtTask, String credentialsId, boolean sudo,
@@ -181,6 +184,11 @@ public void setAdditionalParameters(String additionalParameters) {
this.additionalParameters = additionalParameters;
}

@DataBoundSetter
public void setExtraVars(List<ExtraVar> extraVars) {
this.extraVars = extraVars;
}

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @Nonnull Launcher launcher, @Nonnull TaskListener listener)
throws InterruptedException, IOException
@@ -210,6 +218,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull Node node, @Nonnull FilePat
invocation.setCredentials(StringUtils.isNotBlank(credentialsId) ?
CredentialsProvider.findCredentialById(credentialsId, StandardUsernameCredentials.class, run) : null,
copyCredentialsInWorkspace);
invocation.setExtraVars(extraVars);
invocation.setAdditionalParameters(additionalParameters);
invocation.setHostKeyCheck(hostKeyChecking);
invocation.setUnbufferedOutput(unbufferedOutput);
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Jean-Christophe Sirot <sirot@chelonix.com>
* Copyright 2015-2016 Jean-Christophe Sirot <sirot@chelonix.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -121,6 +121,7 @@ protected ArgumentListBuilder buildCommandLine() throws InterruptedException, An
appendSudo(args);
appendForks(args);
appendCredentials(args);
appendExtraVars(args);
appendAdditionalParameters(args);
return args;
}
@@ -0,0 +1,69 @@
/*
* Copyright 2015-2016 Jean-Christophe Sirot <sirot@chelonix.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jenkinsci.plugins.ansible;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class ExtraVar extends AbstractDescribableImpl<ExtraVar> {

public String key;

public String value;

public boolean hidden;

@DataBoundConstructor
public ExtraVar() {
}

@DataBoundSetter
public void setKey(String key) {
this.key = key;
}

@DataBoundSetter
public void setValue(String value) {
this.value = value;
}

@DataBoundSetter
public void setHidden(boolean hidden) {
this.hidden = hidden;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

public boolean isHidden() {
return hidden;
}

@Extension
public static class DescriptorImpl extends Descriptor<ExtraVar> {

public String getDisplayName() { return ""; }
}

}

0 comments on commit b8fc86d

Please sign in to comment.
You can’t perform that action at this time.