Skip to content

Commit

Permalink
[JENKINS-29863] Add dedicated support for extra variables in build wi…
Browse files Browse the repository at this point in the history
…dget and pipeline
  • Loading branch information
jcsirot committed May 5, 2016
1 parent 245bb4a commit b8fc86d
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
@@ -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.
Expand All @@ -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;
Expand All @@ -47,6 +48,7 @@ abstract class AbstractAnsibleInvocation<T extends AbstractAnsibleInvocation<T>>
protected boolean sudo;
protected String sudoUser;
protected StandardUsernameCredentials credentials;
protected List<ExtraVar> extraVars;
protected String additionalParameters;

private FilePath key = null;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class AnsibleAdHocCommandBuilder extends Builder implements SimpleBuildSt

public String additionalParameters = null;

public List<ExtraVar> extraVars;

@Deprecated
public AnsibleAdHocCommandBuilder(String ansibleName, String hostPattern, Inventory inventory, String module,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
Expand Up @@ -95,6 +95,7 @@ protected ArgumentListBuilder buildCommandLine()
appendSudo(args);
appendForks(args);
appendCredentials(args);
appendExtraVars(args);
appendAdditionalParameters(args);
return args;
}
Expand Down
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -82,6 +83,8 @@ public class AnsiblePlaybookBuilder extends Builder implements SimpleBuildStep

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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
@@ -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.
Expand Down Expand Up @@ -121,6 +121,7 @@ protected ArgumentListBuilder buildCommandLine() throws InterruptedException, An
appendSudo(args);
appendForks(args);
appendCredentials(args);
appendExtraVars(args);
appendAdditionalParameters(args);
return args;
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/org/jenkinsci/plugins/ansible/ExtraVar.java
@@ -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.