Skip to content

Commit

Permalink
gh-133 Integrate Jenkinsfile during preSCM step and reuse the SHA1 - …
Browse files Browse the repository at this point in the history
…part 1
  • Loading branch information
Claus Schneider (Praqma) authored and MadsNielsen committed Aug 15, 2018
1 parent a2b4a86 commit aea10ca
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public String invoke(Repository repo, VirtualChannel channel) throws IOException
sb.append(String.format("%n"));

Integer secondsSinceUnixEpoch = rev.getCommitTime();

// Note that the git log shows different date formats, depending on configuration.
// The choices in the git commit message below matches the squashed commit message
// that git generates on a Ubuntu Linux 14.04 with default git installation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ public GitBridge(IntegrationStrategy integrationStrategy, final String integrati

public static void pushToIntegrationBranchGit(Run<?, ?> run, TaskListener listener, GitClient client, String expandedRepo, String expandedBranch) throws PushFailedException {
try {
pushToBranch(listener, client, expandedBranch, expandedRepo);
pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to integration branch. Exception:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to integration branch. Exception %s", ex));
throw new PushFailedException(String.format("Failed to push changes to integration branch, message was:%n%s", ex));
LOGGER.log(Level.SEVERE, "Failed to push changes to branch: " + expandedBranch +". Exception:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to branch: \" + expandedBranch +\". Exception: %s", ex));
throw new PushFailedException(String.format("Failed to push changes to branch: " + expandedBranch + ", message was:%n%s", ex));
}
}

public static void pushToBranch(TaskListener listener, GitClient client, String branchToPush, String expandedRepo) throws PushFailedException {
public static void pushToBranch(TaskListener listener, GitClient client, String sourceLocalBranch, String targetRemoteBranch, String expandedRepo) throws PushFailedException {
try {
LOGGER.log(Level.INFO, "Pushing changes to: " + branchToPush);
listener.getLogger().println(GitMessages.LOG_PREFIX+ "Pushing changes to integration branch:");
client.push(expandedRepo, "HEAD:refs/heads/" + branchToPush);
LOGGER.log(Level.INFO, "Pushing changes from local branch: " + sourceLocalBranch + " to remote branch: " + targetRemoteBranch);
listener.getLogger().println(GitMessages.LOG_PREFIX+ "Pushing changes to branch:");
client.push(expandedRepo, "refs/heads/" + sourceLocalBranch + ":refs/heads/" + targetRemoteBranch.replace(expandedRepo + "/",""));
LOGGER.log(Level.INFO, "Done pushing changes");
listener.getLogger().println(GitMessages.LOG_PREFIX+ "Done pushing changes");
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to: " + branchToPush + ".\nException:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to: " + branchToPush + ".\nException: %s", ex));
throw new PushFailedException(String.format("Failed to push changes to integration branch, message was:%n%s", ex));
LOGGER.log(Level.SEVERE, "Failed to push changes to: " + targetRemoteBranch + ".\nException:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to: " + targetRemoteBranch + ".\nException: %s", ex));
throw new PushFailedException(String.format("Failed to push changes to branch, message was:%n%s", ex));
}
}

Expand Down Expand Up @@ -210,7 +210,7 @@ public void pushToIntegrationBranch(AbstractBuild<?, ?> build, BuildListener lis

GitSCM gitSCM = findScm(build, listener);
GitClient client = gitSCM.createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
pushToBranch(listener, client, expandedBranch, expandedRepo);
pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo);
} catch (IOException | InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to integration branch. Exception:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to integration branch. Exception %s", ex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.GitUtils;
import jenkins.model.Jenkins;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.gitclient.ChangelogCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
Expand All @@ -25,11 +27,13 @@
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegrationFailedException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.UnsupportedConfigurationException;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -122,10 +126,30 @@ public Revision decorateRevisionToBuild(
try {
gitBridge.evalBranchConfigurations(triggeredBranch, expandedIntegrationBranch, expandedRepo);
listener.getLogger().println(String.format(LOG_PREFIX + "Checking out integration branch %s:", expandedIntegrationBranch));
try {
List<RefSpec> list = new ArrayList<>();
list.add(new RefSpec(":refs/heads" + triggeredBranch.getName().replace(expandedRepo, "")));
git.fetch_().from(new URIish(expandedRepo), list);
} catch (URISyntaxException ex) {
String logMessage = LOG_PREFIX + "Fetch failed";
listener.getLogger().println(logMessage);
LOGGER.log(Level.SEVERE, logMessage, ex );
}
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
((GitIntegrationStrategy) gitBridge.integrationStrategy).integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge);

if(run.getActions(PretestTriggerCommitAction.class).isEmpty() ) {
GitIntegrationStrategy gitStrategy = (GitIntegrationStrategy)gitBridge.integrationStrategy;
gitStrategy.integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge);

//For AccumulatedCommit and in pipeline, immediately push back the merge result
if(gitStrategy instanceof AccumulatedCommitStrategy && run instanceof WorkflowRun) {
GitBridge.pushToBranch(listener, git, expandedIntegrationBranch, triggeredBranch.getName(), repoName);
}
run.addAction(new PretestTriggerCommitAction(triggeredBranch, expandedIntegrationBranch, expandedRepo, ucCredentialsId));
} else {
String logMessage = LOG_PREFIX + "Checking out SHA1 from first integration";
listener.getLogger().println(logMessage);
git.checkout().branch(expandedIntegrationBranch).ref(triggeredBranch.getSHA1String()).deleteBranchIfExist(true).execute();
}
} catch (NothingToDoException e) {
run.setResult(Result.NOT_BUILT);
String logMessage = LOG_PREFIX + String.format("%s - setUp() - NothingToDoException - %s", LOG_PREFIX, e.getMessage());
Expand All @@ -147,7 +171,6 @@ public Revision decorateRevisionToBuild(
git.checkout().branch(expandedIntegrationBranch).ref(expandedRepo + "/" + expandedIntegrationBranch).deleteBranchIfExist(true).execute();
}

run.addAction(new PretestTriggerCommitAction(triggeredBranch, expandedIntegrationBranch, expandedRepo, ucCredentialsId));
if (run.getResult() == null || run.getResult() == Result.SUCCESS) {
Revision mergeRevision = new GitUtils(listener, git).getRevisionForSHA1(git.revParse(HEAD));
return mergeRevision;
Expand Down
4 changes: 1 addition & 3 deletions src/test/resources/EndUserTesting/JenkinsfileScripted
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ def triggerBranchPattern = '*/readyPipeScriptedSCM/*'
def integrationBranch = 'masterPipeScriptedSCM'
node {
stage ('Checkout'){
checkout scm // just reference SCM in the job executing the Pipeline script.
/**
// checkout scm // just reference SCM in the job executing the Pipeline script.
checkout([
$class: 'GitSCM',
branches: [[name: triggerBranchPattern ]],
Expand All @@ -23,7 +22,6 @@ node {
[$class: 'PruneStaleBranch']
]
])
*/
// try {
// sh '''
// if [ -e ./build_failed.md ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ done
read -n 1 -p "Enter to continue" enter

for branch_prefix in ${branch_prefixes} ; do
# git push origin refs/tags/${branch_prefix}_NOT_BUILT:refs/heads/ready${branch_prefix}/test-05.1-NOT_BUILT
publishAndBuild refs/tags/${branch_prefix}_NOT_BUILT ${branch_prefix} test-05.1-NOT_BUILT
git push origin refs/tags/${branch_prefix}_NOT_BUILT:refs/heads/ready${branch_prefix}/test-05.1-NOT_BUILT
# publishAndBuild refs/tags/${branch_prefix}_NOT_BUILT ${branch_prefix} test-05.1-NOT_BUILT
done
read -n 1 -p "Enter to continue" enter

Expand All @@ -156,7 +156,10 @@ for branch_prefix in ${branch_prefixes} ; do
publishAndBuild HEAD ${branch_prefix} ${text}

done
read -n 1 -p "Enter to continue" enter
read -n 1 -p "Enter to continue and fetch and prune branches" enter

checkoutMyBranch "master" && resetToInit

git fetch -ap

git branch -r
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@2.11.2">
<flow-definition plugin="workflow-job@2.9">
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
Expand All @@ -13,7 +13,7 @@
</triggers>
</org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="workflow-cps@2.39">
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="workflow-cps@2.24">
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
Expand All @@ -29,15 +29,21 @@
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions>
<org.jenkinsci.plugins.pretestedintegration.scm.git.PretestedIntegrationAsGitPluginExt plugin="[email protected].0-SNAPSHOT">
<org.jenkinsci.plugins.pretestedintegration.scm.git.PretestedIntegrationAsGitPluginExt plugin="[email protected].2-SNAPSHOT">
<integrationBranch>masterPipeScriptedSCM</integrationBranch>
<repoName>origin</repoName>
<gitIntegrationStrategy class="org.jenkinsci.plugins.pretestedintegration.scm.git.AccumulatedCommitStrategy"/>
<gitIntegrationStrategy class="org.jenkinsci.plugins.pretestedintegration.scm.git.AccumulatedCommitStrategy">
<shortCommitMessage>false</shortCommitMessage>
</gitIntegrationStrategy>
</org.jenkinsci.plugins.pretestedintegration.scm.git.PretestedIntegrationAsGitPluginExt>
<hudson.plugins.git.extensions.impl.ChangelogToBranch>
<options>
<compareRemote>origin</compareRemote>
<compareTarget>masterPipeScriptedSCM</compareTarget>
</options>
</hudson.plugins.git.extensions.impl.ChangelogToBranch>
</extensions>
</scm>
<scriptPath>JenkinsfileScripted</scriptPath>
<lightweight>false</lightweight>
</definition>
<disabled>false</disabled>
</flow-definition>

0 comments on commit aea10ca

Please sign in to comment.