Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-26100] Return a map of SCM-contributed variables #16

Merged
merged 11 commits into from
Jun 20, 2017
38 changes: 26 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.23</version>
<version>2.27</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -38,17 +38,19 @@
</pluginRepository>
</pluginRepositories>
<properties>
<jenkins.version>1.642.3</jenkins.version>
<jenkins-core.version>2.58-20170502.192524-8</jenkins-core.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/jenkins/pull/2730 is merged and released -->
<jenkins-war.version>2.58-20170502.192544-8</jenkins-war.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/jenkins/pull/2730 is merged and released -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.61

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.60 I meant.

<java.level>8</java.level>
Copy link

@i386 i386 Jul 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Hard dependency on Jenkins 2.60 and JDK 8 means we can no longer bundle this version in Blue Ocean's aggregator :(

Are the minimums here a necessary change?

Somewhat concerned about this as the Blue Ocean ATH won't be run with all the latest Pipeline goodness. From this point onwards we have lost end to end tests for this plugin as well as workflow-job.

See jenkinsci/blueocean-plugin@d942692

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was required. The underlying change in core didn't go in until 2.60.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its going to take years for people to be able to use any changes in this plugin from now onwards

picture1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shrug This required a change in core to work, so...

<no-test-jar>false</no-test-jar>
<scm-api-plugin.version>2.0.8</scm-api-plugin.version>
<git-plugin.version>3.1.0</git-plugin.version>
<subversion-plugin.version>2.7.2</subversion-plugin.version>
<scm-api-plugin.version>2.1.1</scm-api-plugin.version>
<git-plugin.version>3.3.1-20170503.162551-3</git-plugin.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/git-plugin/pull/492 is merged and released -->
<subversion-plugin.version>2.7.3-20170505.145345-1</subversion-plugin.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/subversion-plugin/pull/182 is merged and released -->
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.15</version>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -104,38 +106,50 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.15</version>
<version>2.9</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>1.15</version>
<version>2.29</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-multibranch</artifactId>
<version>2.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>1.15</version>
<version>2.10</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you not need 2.12?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope - this is dependent on the SCM implementation only. It won't do anything with an earlier SCM implementation (i.e., one which doesn't depend on workflow-job 2.12 and friends) but it won't error. That said, might as well bump it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you not need to have an integration test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, you do—it just does not depend on workflow-job changes.

<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>1.15</version>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>1.15</version>
<version>2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>1.15</version>
<version>2.14</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import java.io.File;
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Nonnull;
import javax.inject.Inject;
Expand Down Expand Up @@ -70,7 +72,7 @@ public boolean isChangelog() {

protected abstract @Nonnull SCM createSCM();

public static final class StepExecutionImpl extends AbstractSynchronousNonBlockingStepExecution<Void> {
public static final class StepExecutionImpl extends AbstractSynchronousNonBlockingStepExecution<Map<String,String>> {

@Inject private transient SCMStep step;
@StepContextParameter private transient Run<?,?> run;
Expand All @@ -79,15 +81,22 @@ public static final class StepExecutionImpl extends AbstractSynchronousNonBlocki
@StepContextParameter private transient Launcher launcher;

@Override
protected Void run() throws Exception {
protected Map<String,String> run() throws Exception {
step.checkout(run, workspace, listener, launcher);
return null;
Map<String,String> envVars = new TreeMap<>();
step.createSCM().buildEnvironment(run, envVars);
return envVars;
}

private static final long serialVersionUID = 1L;
}

public final void checkout(Run<?,?> run, FilePath workspace, TaskListener listener, Launcher launcher) throws Exception {
doCheckout(createSCM(), isChangelog(), isPoll(), run, workspace, listener, launcher);
}

static final void doCheckout(SCM scm, boolean changelog, boolean poll,
Run<?,?> run, FilePath workspace, TaskListener listener, Launcher launcher) throws Exception {
File changelogFile = null;
if (changelog) {
for (int i = 0; ; i++) {
Expand All @@ -97,7 +106,6 @@ public final void checkout(Run<?,?> run, FilePath workspace, TaskListener listen
}
}
}
SCM scm = createSCM();
SCMRevisionState baseline = null;
Run<?,?> prev = run.getPreviousBuild();
if (prev != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps.scm;

import com.google.common.collect.ImmutableSet;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.SCMListener;
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
* A step which checks out some kind of {@link SCM}, and then runs a body with any environment variables contributed
* by the {@link SCM} added to the environment.
*/
public class WithCheckoutStep extends Step implements Serializable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do without this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okiedokie, will remove.


private boolean poll = true;
private boolean changelog = true;
private final SCM scm;

public boolean isPoll() {
return poll;
}

@DataBoundSetter public void setPoll(boolean poll) {
this.poll = poll;
}

public boolean isChangelog() {
return changelog;
}

@DataBoundSetter public void setChangelog(boolean changelog) {
this.changelog = changelog;
}

public SCM getScm() {
return scm;
}

@DataBoundConstructor
public WithCheckoutStep(SCM s) {
this.scm = s;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new StepExecutionImpl(this, context);
}


public final void checkout(Run<?,?> run, FilePath workspace, TaskListener listener, Launcher launcher) throws Exception {
SCMStep.doCheckout(getScm(), isChangelog(), isPoll(), run, workspace, listener, launcher);
}

public static final class StepExecutionImpl extends StepExecution implements Serializable{

private final WithCheckoutStep step;

public StepExecutionImpl(WithCheckoutStep step, StepContext context) {
super(context);
this.step = step;
}

@Override
public boolean start() throws Exception {
Run<?, ?> run = getContext().get(Run.class);
TaskListener listener = getContext().get(TaskListener.class);
FilePath workspace = getContext().get(FilePath.class);
Launcher launcher = getContext().get(Launcher.class);

step.checkout(run, workspace, listener, launcher);
Map<String,String> envVars = new TreeMap<>();
step.scm.buildEnvironment(run, envVars);

getContext().newBodyInvoker().
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new ExpanderImpl(envVars))).
withCallback(BodyExecutionCallback.wrap(getContext())).
start();

return false;
}

@Override public void stop(Throwable cause) throws Exception {
// should be no need to do anything special (but verify in JENKINS-26148)
}

@Override public void onResume() {}

private static final long serialVersionUID = 1L;
}

private static final class ExpanderImpl extends EnvironmentExpander {
private static final long serialVersionUID = 1;
private final Map<String,String> overrides;
private ExpanderImpl(Map<String,String> overrides) {
this.overrides = new HashMap<>();
this.overrides.putAll(overrides);
}

@Override public void expand(@Nonnull EnvVars env) throws IOException, InterruptedException {
env.overrideAll(overrides);
}
}

@Extension
public static class WithCheckoutStepDescriptor extends StepDescriptor {
@Override
public String getFunctionName() {
return "withCheckout";
}

@Override
public String getDisplayName() {
return "Checkout SCM with environment";
}

@Override
public boolean takesImplicitBlockArgument() {
return true;
}

@Override
public Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Run.class, FilePath.class, TaskListener.class, Launcher.class);
}
}

private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
and check if it is shown in the list below.
Then select the SCM to use from the dropdown list and configure it as needed.
</p>
<p>This step returns a <code>Map</code> of any variables the SCM plugin would
set in a Freestyle job, so if your SCM is git, you can do:
</p>
<pre>
def scmVars = checkout scm
def commitHash = scmVars.GIT_COMMIT

// or

def commitHash = checkout(scm).GIT_COMMIT
</pre>
<p>
Any other specific step to run checkouts (like <code>svn</code> or <code>git</code>)
are simplistic options of this step.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ The MIT License
~
~ Copyright (c) 2017, CloudBees, Inc.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
~ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form">
<f:dropdownDescriptorSelector field="scm" title="SCM" descriptors="${descriptor.applicableDescriptors}"/>
<st:include page="config-generic.jelly" class="org.jenkinsci.plugins.workflow.steps.scm.SCMStep"/>
</j:jelly>
Loading