Skip to content
Permalink
Browse files

Set SCM Environment for Jenkins 2.60+

JENKINS-37584
JENKINS-40885
  • Loading branch information...
p4paul committed Jun 21, 2017
1 parent 5a074dd commit 0ef5086068dd34198e20839544fe87761d252756
@@ -249,7 +249,7 @@ public String getKey() {
? ConnectionHelper.findCredential(scmCredential, Jenkins.getActiveInstance())
: ConnectionHelper.findCredential(scmCredential, job);

if(credentials == null) {
if (credentials == null) {
logger.fine("Could not retrieve credentials from id: '${scmCredential}");
return null;
}
@@ -416,7 +416,7 @@ public void checkout(Run<?, ?> run, Launcher launcher, FilePath buildWorkspace,
Workspace ws = task.setEnvironment(run, workspace, buildWorkspace);

// Add review to environment, if defined
if(review != null) {
if (review != null) {
ws.addEnv(ReviewProp.REVIEW.toString(), review.getId());
ws.addEnv(ReviewProp.STATUS.toString(), CheckoutStatus.SHELVED.toString());
}
@@ -534,11 +534,22 @@ boolean isBuildParent(Job<?, ?> job) {
return list;
}

// Pre Jenkins 2.60
@Override
public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
super.buildEnvVars(build, env);

TagAction tagAction = build.getAction(TagAction.class);
buildEnvironment(tagAction, env);
}

// Post Jenkins 2.60 JENKINS-37584 JENKINS-40885
public void buildEnvironment(Run<?, ?> run, java.util.Map<String, String> env) {
TagAction tagAction = run.getAction(TagAction.class);
buildEnvironment(tagAction, env);
}

private void buildEnvironment(TagAction tagAction, Map<String, String> env) {
if (tagAction != null) {
// Set P4_CHANGELIST value
String change = tagAction.getRefChange().toString();
@@ -585,7 +596,7 @@ public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
private String getChangeNumber(TagAction tagAction, Run<?, ?> run) {
List<P4Ref> builds = tagAction.getRefChanges();

for(P4Ref build : builds) {
for (P4Ref build : builds) {
if (build instanceof P4ChangeRef) {
// its a change, so return...
return build.toString();
@@ -854,7 +865,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
/**
* Credentials list, a Jelly config method for a build job.
*
* @param project Jenkins project item
* @param project Jenkins project item
* @param credential Perforce credential ID
* @return A list of Perforce credential items to populate the jelly
* Select list.
@@ -867,7 +878,7 @@ public ListBoxModel doFillCredentialItems(@AncestorInPath Item project, @QueryPa
* Credentials list, a Jelly config method for a build job.
*
* @param project Jenkins project item
* @param value credential user input value
* @param value credential user input value
* @return A list of Perforce credential items to populate the jelly
* Select list.
*/
@@ -46,7 +46,7 @@ public void testWorkflow() throws Exception {
+ " publisher = [$class: 'SubmitImpl', description: 'Submitted by Jenkins', onlyOnSuccess: false, reopen: false]\n"
+ " buildWorkspace = [$class: 'TemplateWorkspaceImpl', charset: 'none', format: 'jenkins-${NODE_NAME}-${JOB_NAME}', pinHost: false, templateName: 'test.ws']\n"
+ " p4publish credential: '" + id + "', publish: publisher, workspace: buildWorkspace" + " \n"
+ "}"));
+ "}", false));
WorkflowRun run = jenkins.assertBuildStatusSuccess(job.scheduleBuild2(0));
jenkins.assertLogContains("P4 Task: syncing files at change", run);
jenkins.assertLogContains("P4 Task: tagging build.", run);
@@ -63,7 +63,7 @@ public void testWorkflowEnv() throws Exception {
+ "node {\n"
+ " p4sync credential: '" + id + "', template: 'test.ws'\n"
+ " println \"P4_CHANGELIST: ${env.P4_CHANGELIST}\"\n"
+ "}"));
+ "}", false));
WorkflowRun run = jenkins.assertBuildStatusSuccess(job.scheduleBuild2(0));
jenkins.assertLogContains("P4 Task: syncing files at change", run);
jenkins.assertLogContains("P4_CHANGELIST: 40", run);
@@ -83,7 +83,7 @@ public void testManualP4Sync() throws Exception {
+ " def syncOptions = [$class: 'org.jenkinsci.plugins.p4.populate.SyncOnlyImpl',\n"
+ " revert:true, have:true, modtime:true]\n"
+ " p4sync workspace:workspace, credential: '" + id + "', populate: syncOptions\n"
+ "}"));
+ "}", false));
WorkflowRun run = jenkins.assertBuildStatusSuccess(job.scheduleBuild2(0));
jenkins.assertLogContains("P4 Task: syncing files at change", run);
}
@@ -96,7 +96,7 @@ public void testP4GroovyConnectAndSync() throws Exception {
+ " ws = [$class: 'StreamWorkspaceImpl', charset: 'none', format: 'jenkins-${NODE_NAME}-${JOB_NAME}', pinHost: false, streamName: '//stream/main']\n"
+ " p4 = p4(credential: '" + auth.getId() + "', workspace: ws)\n"
+ " p4.run('sync', '//...')\n"
+ "}"));
+ "}", false));
job.save();

WorkflowRun run = job.scheduleBuild2(0).get();
@@ -116,7 +116,7 @@ public void testP4GroovySpecEdit() throws Exception {
+ " echo \"Client: ${client}\""
+ " client.put('Description', 'foo')"
+ " p4.save('client', client)"
+ "}"));
+ "}", false));
job.save();

WorkflowRun run = job.scheduleBuild2(0).get();
@@ -138,7 +138,7 @@ public void testSyncIDManualP4Sync() throws Exception {
+ " def syncOptions = [$class: 'org.jenkinsci.plugins.p4.populate.SyncOnlyImpl',\n"
+ " revert:true, have:true, modtime:true]\n"
+ " p4sync workspace:workspace, credential: '" + id + "', populate: syncOptions\n"
+ "}"));
+ "}", false));
WorkflowRun run = jenkins.assertBuildStatusSuccess(job.scheduleBuild2(0));
jenkins.assertLogContains("P4 Task: syncing files at change", run);

@@ -157,7 +157,7 @@ public void testP4GroovyMultiArg() throws Exception {
+ " p4 = p4(credential: '" + auth.getId() + "', workspace: ws)\n"
+ " p4.run('sync', '//...')\n"
+ " p4.run('changes', '-m4', '//...@24,27')\n"
+ "}"));
+ "}", false));
job.save();

WorkflowRun run = job.scheduleBuild2(0).get();
@@ -166,4 +166,34 @@ public void testP4GroovyMultiArg() throws Exception {
jenkins.assertLogNotContains("Change 1", run);
jenkins.assertLogNotContains("Change 40", run);
}

@Test
public void testCheckoutEnvironment() throws Exception {
WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "checkoutEnv");
job.setDefinition(new CpsFlowDefinition(""
+ "node() {\n" +
" stage('Sync files...') {\n" +
" checkout([$class: 'PerforceScm', " +
" credential: '" + auth.getId() + "', " +
" populate: [$class: 'ForceCleanImpl', have: false, pin: '', quiet: true], " +
" workspace: [$class: 'StreamWorkspaceImpl', charset: 'none', " +
" format: 'jenkins-${NODE_NAME}-${JOB_NAME}', pinHost: false, " +
" streamName: '//stream/main']])\n" +
" }\n" +
" stage ('Test env...') {\n" +
" println \"P4_CHANGELIST=${env.P4_CHANGELIST}\"\n" +
" println \"P4_CLIENT=${env.P4_CLIENT}\"\n" +
" println \"HUDSON_CHANGELOG_FILE=${env.HUDSON_CHANGELOG_FILE}\"\n" +
" println \"P4_USER=${env.P4_USER}\"\n" +
" println \"P4_TICKET=${env.P4_TICKET}\"\n" +
" }\n" +
"}", false));
job.save();

WorkflowRun run = job.scheduleBuild2(0).get();
jenkins.assertLogContains("P4_CHANGELIST=39", run);
jenkins.assertLogContains("P4_CLIENT=jenkins-master-checkoutEnv", run);
jenkins.assertLogContains("P4_USER=jenkins", run);
jenkins.assertLogNotContains("HUDSON_CHANGELOG_FILE=null", run);
}
}

1 comment on commit 0ef5086

@jglick

This comment has been minimized.

Copy link
Member

commented on 0ef5086 Jun 21, 2017

CC @abayer

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