2020-11-10 11:10:11 +02:00

142 lines
8.2 KiB
Java

package buildjob;
import com.atlassian.bamboo.specs.api.BambooSpec;
import com.atlassian.bamboo.specs.api.builders.BambooKey;
import com.atlassian.bamboo.specs.api.builders.docker.DockerConfiguration;
import com.atlassian.bamboo.specs.api.builders.permission.PermissionType;
import com.atlassian.bamboo.specs.api.builders.permission.Permissions;
import com.atlassian.bamboo.specs.api.builders.permission.PlanPermissions;
import com.atlassian.bamboo.specs.api.builders.plan.Job;
import com.atlassian.bamboo.specs.api.builders.plan.Plan;
import com.atlassian.bamboo.specs.api.builders.plan.PlanIdentifier;
import com.atlassian.bamboo.specs.api.builders.plan.Stage;
import com.atlassian.bamboo.specs.api.builders.plan.artifact.Artifact;
import com.atlassian.bamboo.specs.api.builders.plan.branches.BranchCleanup;
import com.atlassian.bamboo.specs.api.builders.plan.branches.PlanBranchManagement;
import com.atlassian.bamboo.specs.api.builders.project.Project;
import com.atlassian.bamboo.specs.builders.task.CheckoutItem;
import com.atlassian.bamboo.specs.builders.task.ScriptTask;
import com.atlassian.bamboo.specs.builders.task.TestParserTask;
import com.atlassian.bamboo.specs.builders.task.VcsCheckoutTask;
import com.atlassian.bamboo.specs.builders.task.VcsCommitTask;
import com.atlassian.bamboo.specs.builders.trigger.BitbucketServerTrigger;
import com.atlassian.bamboo.specs.util.BambooServer;
/**
* Plan configuration for Bamboo.
* Learn more on: <a href="https://confluence.atlassian.com/display/BAMBOO/Bamboo+Specs">https://confluence.atlassian.com/display/BAMBOO/Bamboo+Specs</a>
*/
@BambooSpec
public class PlanSpec {
/**
* Run main to publish plan on Bamboo
*/
public static void main(final String[] args) throws Exception {
//By default credentials are read from the '.credentials' file.
BambooServer bambooServer = new BambooServer("http://localhost:8085");
Plan buildPlan = new PlanSpec().createDockerBuildPlan();
bambooServer.publish(buildPlan);
PlanPermissions buildPlanPermissions = new PlanSpec().createPlanPermission(buildPlan.getIdentifier());
bambooServer.publish(buildPlanPermissions);
Plan testPlan = new PlanSpec().createCypressTestPlan();
bambooServer.publish(testPlan);
PlanPermissions testPlanPermissions = new PlanSpec().createPlanPermission(testPlan.getIdentifier());
bambooServer.publish(testPlanPermissions);
}
private PlanPermissions createPlanPermission(PlanIdentifier planIdentifier) {
Permissions permission = new Permissions()
.userPermissions("atlbamboo", PermissionType.EDIT, PermissionType.VIEW, PermissionType.ADMIN,
PermissionType.CLONE, PermissionType.BUILD)
.userPermissions("tbejan", PermissionType.ADMIN, PermissionType.EDIT, PermissionType.VIEW,
PermissionType.CLONE, PermissionType.BUILD)
.groupPermissions("red-ui", PermissionType.EDIT, PermissionType.VIEW, PermissionType.BUILD)
.loggedInUserPermissions(PermissionType.VIEW).anonymousUserPermissionView();
return new PlanPermissions(planIdentifier.getProjectKey(), planIdentifier.getPlanKey()).permissions(permission);
}
private Project project() {
return new Project().name("RED").key(new BambooKey("RED"));
}
public Plan createDockerBuildPlan() {
return new Plan(project(), "Redaction UI", new BambooKey("UI"))
.description("Docker build for Redaction UI.").stages(
new Stage("Build Stage").jobs(creatGinCloudPlatformImagesJob("red-ui")))
.stages(new Stage("Upstream Push Stage").jobs(creatGinCloudPlatformUpstreamJob()))
.linkedRepositories("RED / ui").triggers(new BitbucketServerTrigger()).planBranchManagement(
new PlanBranchManagement().createForVcsBranch()
.delete(new BranchCleanup().whenInactiveInRepositoryAfterDays(30)).notificationForCommitters());
}
public Job creatGinCloudPlatformImagesJob(String project) {
return new Job("Build Job: " + project, new BambooKey(project.toUpperCase().replaceAll("-", ""))).tasks(
new VcsCheckoutTask().description("Checkout Default Repository")
.checkoutItems(new CheckoutItem().defaultRepository()), new ScriptTask().description("Build")
.inlineBody(
"#!/bin/bash\n" + "set -e\n" + "imageName=\"nexus.iqser.com:5001/red/" + project + "\"\n"
+ "dockerfileLocation=\"docker/" + project + "/Dockerfile\"\n"
+ "docker build -t ${imageName}:latest -f ${dockerfileLocation} .\n"
+ "if [[ \"${bamboo.planRepository.branchName}\" == \"master\" ]]\n" + "then\n"
+ " ./versions.sh patch\n" + " version=$(cat package.json | jq -r '.version')\n"
+ " echo \"Publishing Images with version $version\"\n"
+ " echo ${bamboo.bamboo_download_pass} | docker login -u ${bamboo.bamboo_download_user} --password-stdin nexus.iqser.com:5001\n"
+ " docker push ${imageName}:latest\n"
+ " docker tag ${imageName}:latest ${imageName}:${version}\n"
+ " docker push ${imageName}:${version}\n" + "fi\n")).dockerConfiguration(
new DockerConfiguration().image("nexus.iqser.com:5001/infra/release_build:2.9.1")
.volume("/var/run/docker.sock", "/var/run/docker.sock"));
}
public Job creatGinCloudPlatformUpstreamJob() {
return new Job("Upstream Push", new BambooKey("USPSH")).tasks(
new VcsCheckoutTask().description("Checkout Default Repository")
.checkoutItems(new CheckoutItem().defaultRepository()), new ScriptTask().description("Build")
.inlineBody(
"#!/bin/bash\n" + "set -e\n" + "if [[ \"${bamboo.planRepository.branchName}\" == \"master\" ]]\n"
+ "then\n" + " ./versions.sh patch\n" + "fi\n"),
new VcsCommitTask().commitMessage("chore(release)").repository("RED / ui")).dockerConfiguration(
new DockerConfiguration().image("nexus.iqser.com:5001/infra/release_build:2.9.1")
.volume("/var/run/docker.sock", "/var/run/docker.sock"));
}
public Plan createCypressTestPlan() {
return new Plan(project(), "redaction-ui-tests", new BambooKey("UI-TESTS"))
.description("Cypress UI tests for redaction-ui.")
.stages(new Stage("UI Test Stage").jobs(createCypressJob()))
.linkedRepositories("RED / ui").triggers(new BitbucketServerTrigger()).planBranchManagement(
new PlanBranchManagement().createForVcsBranch()
.delete(new BranchCleanup().whenInactiveInRepositoryAfterDays(30)).notificationForCommitters());
}
private Job createCypressJob() {
return new Job("Cypress UI Tests Job", new BambooKey("UI-TESTS-CYPRESS")).tasks(
new VcsCheckoutTask().description("Checkout Default Repository")
.checkoutItems(new CheckoutItem().defaultRepository()),
new ScriptTask().description("Build").inlineBody(
"#!/bin/bash\n" + "set -e\n" + "imageName=\"nexus.iqser.com:5001/red/ui-cypress\"\n"
+ "dockerfileLocation=\"build/docker/cypress/Dockerfile\"\n"
+ "docker build -t ${imageName}:latest -f ${dockerfileLocation} .\n"),
new ScriptTask().description("Run Tests").inlineBody(
"#!/bin/bash\n" + "set -e\n" + "imageName=\"nexus.iqser.com:5001/red/ui-cypress\"\n"
+ "docker run -v $PWD:/e2e -w /e2e ${imageName}:latest\n" + "sleep 10")
// for some reason disk isn't synced in real-time from docker
).finalTasks(
TestParserTask.createJUnitParserTask().description("Cypress Test Results").defaultResultDirectory())
.dockerConfiguration(new DockerConfiguration().image("nexus.iqser.com:5001/infra/release_build:2.9.1")
.volume("/var/run/docker.sock", "/var/run/docker.sock"))
.artifacts(new Artifact("videos").location("./cypress/videos").copyPattern("**/*.mp4").shared(true),
new Artifact("screenshots").location("./cypress/screenshots").copyPattern("**/*.png").shared(true));
}
}