forked from atomist/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsJob.ts
94 lines (87 loc) · 3.13 KB
/
jenkinsJob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
GoalInvocation,
hasFile,
isMaterialChange,
} from "@atomist/sdm";
import { configure } from "@atomist/sdm-core";
import {
jenkins,
JenkinsRegistration,
} from "@atomist/sdm-pack-jenkins";
import * as fs from "fs-extra";
import * as hbx from "handlebars";
import * as path from "path";
/**
* Atomist SDM Sample
* @description SDM to demonstrate how to run and converge Jenkins jobs
* @tag sdm,jenkins,maven
* @instructions <p>Now that the SDM is up and running, make a commit to a Maven
* repository that has an Atomist webhook configured. You can observe
* the Jenkins goal from chat or https://app.atomist.com.
*
* Note: Please configure the following environment variables so that
* this SDM can access your Jenkins instance: JENKINS_URL, JENKINS_USER
* and JENKINS_PASSWORD.</p>
*/
// atomist:code-snippet:start=sdm
/**
* Main entry point into the SDM
*/
export const configuration = configure(async () => {
// The Jenkins goal needs access to the Jenkins master which
// can be configured below
const options: Pick<JenkinsRegistration, "server"> = {
server: {
url: process.env.JENKINS_URL || "http://127.0.0.1:8080",
user: process.env.JENKINS_USER || "admin",
password: process.env.JENKINS_PASSWORD || "123456",
},
};
// Jenkins goal that runs a job named <repo_name>-build which will be
// created or updated with a job definition returned by the mavenPipeline
// function
const build = jenkins("build", {
...options,
job: async gi => process.env.JENKINS_JOB || `${gi.goalEvent.repo.name}-build`,
definition: !process.env.JENKINS_JOB ? async gi => mavenPipeline(gi) : undefined,
});
// Single push rule that runs the build goal when the push is material and the project
// has a pom.xml file
return {
"ci/cd": {
test: [
hasFile("pom.xml"),
isMaterialChange({
extensions: ["java", "properties", "yaml"],
files: ["pom.xml"],
})],
goals: [
build,
],
},
};
}, { name: "jenkins" });
/**
* Load the job definition from a local XML template
*/
async function mavenPipeline(gi: GoalInvocation): Promise<string> {
const template = (await fs.readFile(path.join(__dirname, "maven.pipeline.xml"))).toString();
const hb = hbx.compile(template);
return hb({ gi });
}
// atomist:code-snippet:end