Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/xebialabs/deployit/ci/RepositoryUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xebialabs.deployit.ci;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import com.google.common.collect.Ordering;
import com.xebialabs.deployit.ci.Credential.SecondaryServerInfo;
import com.xebialabs.deployit.ci.server.DeployitDescriptorRegistry;
Expand All @@ -12,6 +14,16 @@

public class RepositoryUtils {

public static DeployitServer getDeployitServer(String credentialName, String overridingCredentialUsername, String overridingCredentialPassword, boolean overridingCredentialUseGlobalCredential) {
Credential overridingCredential = null;

if (overridingCredentialUsername != null && overridingCredentialPassword != null) {
UsernamePasswordCredentialsImpl cred = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL,java.util.UUID.randomUUID().toString(), "Overriding Credential", overridingCredentialUsername, overridingCredentialPassword);
overridingCredential = new Credential(credentialName, cred.getUsername(), cred.getPassword(), cred.getId(), null, overridingCredentialUseGlobalCredential);
}
return getDeployitServer(credentialName, overridingCredential);
}

public static DeployitServer getDeployitServer(String credentialName, Credential overridingCredential) {
Credential credential = findCredential(credentialName);
DeployitNotifier.DeployitDescriptor descriptor = (DeployitNotifier.DeployitDescriptor) Hudson.getInstance().getDescriptor(DeployitNotifier.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class XLDeployDeployStep extends AbstractStepImpl {
public final String serverCredentials;
public final String packageId;
public final String environmentId;
public String overridingCredentialUsername;
public String overridingCredentialPassword;
public boolean overridingCredentialUseGlobalCredential;

@DataBoundConstructor
public XLDeployDeployStep(String serverCredentials, String packageId, String environmentId) {
Expand All @@ -32,6 +35,21 @@ public XLDeployDeployStep(String serverCredentials, String packageId, String env
this.packageId = packageId;
}

@DataBoundSetter
public void setOverridingCredentialUsername(String overridingCredentialUsername) {
this.overridingCredentialUsername = overridingCredentialUsername;
}

@DataBoundSetter
public void setOverridingCredentialPassword(String overridingCredentialPassword) {
this.overridingCredentialPassword = overridingCredentialPassword;
}

@DataBoundSetter
public void setOverridingCredentialUseGlobalCredential(boolean overridingCredentialUseGlobalCredential) {
this.overridingCredentialUseGlobalCredential = overridingCredentialUseGlobalCredential;
}

@Extension
public static final class XLDeployDeployStepDescriptor extends AbstractStepDescriptorImpl {

Expand Down Expand Up @@ -80,7 +98,7 @@ protected Void run() throws Exception {
String resolvedPackageId = envVars.expand(step.packageId);
JenkinsDeploymentListener deploymentListener = new JenkinsDeploymentListener(listener, false);
JenkinsDeploymentOptions deploymentOptions = new JenkinsDeploymentOptions(resolvedEnvironmentId, VersionKind.Other, true, false , false, true);
DeployitServer deployitServer = RepositoryUtils.getDeployitServer(step.serverCredentials, null);
DeployitServer deployitServer = RepositoryUtils.getDeployitServer(step.serverCredentials, step.overridingCredentialUsername, step.overridingCredentialPassword, step.overridingCredentialUseGlobalCredential);
deployitServer.deploy(resolvedPackageId,resolvedEnvironmentId,deploymentOptions,deploymentListener);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@ public class XLDeployPublishStep extends AbstractStepImpl {

public final String serverCredentials;
public final String darPath;
public String overridingCredentialUsername;
public String overridingCredentialPassword;
public boolean overridingCredentialUseGlobalCredential;

@DataBoundConstructor
public XLDeployPublishStep(String darPath, String serverCredentials) {
this.darPath = darPath;
this.serverCredentials = serverCredentials;
}

@DataBoundSetter
public void setOverridingCredentialUsername(String overridingCredentialUsername) {
this.overridingCredentialUsername = overridingCredentialUsername;
}

@DataBoundSetter
public void setOverridingCredentialPassword(String overridingCredentialPassword) {
this.overridingCredentialPassword = overridingCredentialPassword;
}

@DataBoundSetter
public void setOverridingCredentialUseGlobalCredential(boolean overridingCredentialUseGlobalCredential) {
this.overridingCredentialUseGlobalCredential = overridingCredentialUseGlobalCredential;
}

@Extension
public static final class XLDeployPublishStepDescriptor extends AbstractStepDescriptorImpl {
Expand Down Expand Up @@ -81,7 +99,7 @@ protected Void run() throws Exception {
final String path = ArtifactView.findFilePathFromPattern(envVars.expand(step.darPath), ws, deploymentListener);
RemoteAwareLocation location = getRemoteAwareLocation(path);
try {
DeployitServer deployitServer = RepositoryUtils.getDeployitServer(step.serverCredentials, null);
DeployitServer deployitServer = RepositoryUtils.getDeployitServer(step.serverCredentials, step.overridingCredentialUsername, step.overridingCredentialPassword, step.overridingCredentialUseGlobalCredential);
deployitServer.importPackage(location.getDarFileLocation(ws, deploymentListener, envVars));
} finally {
location.cleanup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,25 @@ public void before() {
@LocalData
public void shouldStartReleaseWithJenkinsFile() throws Exception {
WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "rest-o-rant-api");
job.setDefinition(new CpsFlowDefinition(getJenkinsFileScript(), true));
job.setDefinition(new CpsFlowDefinition(getJenkinsFileScript("Jenkinsfile"), true));
jenkins.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
}

@Test
@LocalData
public void shouldStartReleaseWithJenkinsFileOverridingDeployCredentials() throws Exception {
WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "rest-o-rant-api-override-deploy-creds");
job.setDefinition(new CpsFlowDefinition(getJenkinsFileScript("Jenkinsfile-deployOverriding"), true));
jenkins.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
}


private String getJenkinsFileScript() throws IOException {
private String getJenkinsFileScript(String fileName) throws IOException {
String jenkinsFile = "";

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

Files.copy(Paths.get(getClass().getClassLoader().getResource("JenkinsFile").getFile()), outputStream);
Files.copy(Paths.get(getClass().getClassLoader().getResource(fileName).getFile()), outputStream);
jenkinsFile = new String(outputStream.toByteArray());
}
return jenkinsFile;
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/JenkinsFile-deployOverriding
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

node {
git url:'https://github.com/xebialabs/rest-o-rant-api.git'
sh './gradlew clean build'
env.WORKSPACE = pwd()
def projectVersion = 'v1'
xldCreatePackage artifactsPath: 'build', manifestPath: 'deployit-manifest.xml', darPath: '$JOB_NAME-$BUILD_NUMBER.dar'
xldPublishPackage serverCredentials: 'admin_credential', darPath: '$JOB_NAME-$BUILD_NUMBER.dar'
xldDeploy serverCredentials: 'admin_credential', overridingCredentialUsername: 'admin', overridingCredentialPassword: 'admin', packageId: 'Applications/$JOB_NAME/$BUILD_NUMBER.0', environmentId: 'Environments/localhost'
}