Timo Bejan 65a30e8d1c update
2021-09-30 11:27:31 +03:00

129 lines
7.0 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.credentials.SharedCredentialsIdentifier;
import com.atlassian.bamboo.specs.api.builders.credentials.SharedCredentialsScope;
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.repository.git.GitRepository;
import com.atlassian.bamboo.specs.builders.task.*;
import com.atlassian.bamboo.specs.builders.trigger.BitbucketServerTrigger;
import com.atlassian.bamboo.specs.model.task.ScriptTaskProperties;
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);
}
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("devplant", 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("Release")
.manual(true)
.jobs(createRelease()))
.linkedRepositories("RED / ui")
.linkedRepositories("Shared Libraries / common-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 CleanWorkingDirectoryTask().description("My clean working directory task"),
// Checkout
new VcsCheckoutTask().description("Checkout Default Repository")
.checkoutItems(new CheckoutItem().defaultRepository().path("redaction-ui")),
new VcsCheckoutTask().description("Checkout UI Shared Lib")
.checkoutItems(new CheckoutItem().repository("Shared Libraries / common-ui").path("common-ui")),
// Build
new ScriptTask().description("Build")
.location(ScriptTaskProperties.Location.FILE)
.workingSubdirectory("redaction-ui")
.fileFromPath("bamboo-specs/src/main/resources/scripts/build.sh")
.environmentVariables(
"PROJECT=\"" + project + "\" " +
"BAMBOO_DOWNLOAD_PASS=\"${bamboo.bamboo_download_pass}\" " +
"BAMBOO_DOWNLOAD_USER=\"${bamboo.bamboo_download_user}\" "),
// read version from artifact
new InjectVariablesTask().path("redaction-ui/version.properties"),
// commit release
new VcsCommitTask().commitMessage("chore(release)").repository("RED / ui"),
// create tag with this version
new VcsTagTask().tagName("${bamboo.inject.APP_VERSION}").repository("RED / ui")
).dockerConfiguration(
new DockerConfiguration().image("nexus.iqser.com:5001/infra/release_build:4.2.0")
.volume("/var/lib/docker", "/var/lib/docker")
.volume("/var/run/docker.sock", "/var/run/docker.sock"))
.artifacts(new Artifact("version").location(".").copyPattern("**/version.properties").shared(true),
new Artifact("paligo-theme.tar.gz").location(".").copyPattern("**/paligo-theme.tar.gz").shared(true));
}
public Job createRelease() {
return new Job("Create Release", new BambooKey("CRLS"))
.tasks(
new CleanWorkingDirectoryTask().description("My clean working directory task"),
new VcsCheckoutTask().description("Checkout Default Repository")
.checkoutItems(new CheckoutItem().defaultRepository()).cleanCheckout(true),
new ArtifactDownloaderTask().description("Download version artifact")
.sourcePlan(new PlanIdentifier("RED", "UI"))
.artifacts(new DownloadItem().artifact("version")),
// read version from artifact
new InjectVariablesTask().path("redaction-ui/version.properties"),
new ScriptTask().description("checkout tag").inlineBody("git checkout tags/${bamboo.inject.APP_VERSION}"),
new VcsBranchTask().branchName("release/${bamboo.inject.APP_VERSION}").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"));
}
}