Skip to content

Commit eab5a5a

Browse files
author
Jared Murrell
authored
Merge pull request #218 from github/primetheus/jira-workflow
Create jira-workflow example
2 parents 60b35f7 + c464b21 commit eab5a5a

File tree

3 files changed

+607
-0
lines changed

3 files changed

+607
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project:
2+
- name: GitHub-Demo
3+
org: GitHub-Demo
4+
repos:
5+
- sample-core
6+
- sample-api
7+
- sample-ui
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
3+
*/
4+
// Define variables that we'll set values to later on
5+
// We only need to define the vars we'll use across stages
6+
def settings
7+
def projectInfo
8+
// This is an array we'll use for dynamic parallization
9+
def repos = [:]
10+
def githubUrl = "https://github.example.com/api/v3"
11+
//def githubUrl = "https://api.github.com/"
12+
13+
/*
14+
node {
15+
// useful debugging info
16+
echo sh(returnStdout: true, script: 'env')
17+
}
18+
*/
19+
20+
pipeline {
21+
// This can run on any agent... we can lock it down to a
22+
// particular node if we have multiple nodes, but we won't here
23+
agent any
24+
triggers {
25+
GenericTrigger(
26+
genericVariables: [
27+
[key: 'event', value: '$.webhookEvent'],
28+
[key: 'version', value: '$.version'],
29+
[key: 'projectId', value: '$.version.projectId'],
30+
[key: 'name', value: '$.version.name'],
31+
[key: 'description', value: '$.version.description']
32+
],
33+
34+
causeString: 'Triggered on $ref',
35+
// This token is arbitrary, but is used to trigger this pipeline.
36+
// Without a token, ALL pipelines that use the Generic Webhook Trigger
37+
// plugin will trigger
38+
token: '6BE4BF6E-A319-40A8-8FE9-D82AE08ABD03',
39+
printContributedVariables: true,
40+
printPostContent: true,
41+
silentResponse: false,
42+
regexpFilterText: '',
43+
regexpFilterExpression: ''
44+
)
45+
}
46+
stages {
47+
// We'll read our settings in this step
48+
stage('Get our settings') {
49+
steps {
50+
script {
51+
try {
52+
settings = readYaml(file: '.github/jira-workflow.yml')
53+
//sh("echo ${settings.project}")
54+
} catch(err) {
55+
echo "Please create .github/jira-workflow.yml"
56+
throw err
57+
//currentBuild.result = 'ABORTED'
58+
//return
59+
//currentBuild.rawBuild.result = Result.ABORTED //This method requires in-process script approval, but is nicer than what's running currently
60+
}
61+
}
62+
}
63+
}
64+
stage('Get project info') {
65+
steps {
66+
script {
67+
// echo projectId
68+
projectInfo = jiraGetProject(idOrKey: projectId, site: 'Jira')
69+
// echo projectInfo.data.name.toString()
70+
}
71+
}
72+
}
73+
stage('Create Release Branches') {
74+
when {
75+
// Let's only run this stage when we have a 'version created' event
76+
expression { event == 'jira:version_created' }
77+
}
78+
steps {
79+
script {
80+
// Specify our credentials to use for the steps
81+
withCredentials([usernamePassword(credentialsId: '<github_credentials_id>',
82+
passwordVariable: 'githubToken',
83+
usernameVariable: 'githubUser')]) {
84+
// Loop through our list of Projects in Jira, which will map to Orgs in GitHub.
85+
// We're assigning it 'p' since 'project' is assigned as part of the YAML structure
86+
settings.project.each { p ->
87+
// Only apply this release to the proper Org
88+
if (p.name.toString() == projectInfo.data.name.toString()) {
89+
// Loop through each repo in the Org
90+
p.repos.each { repo ->
91+
// Create an array that we will use to dynamically parallelize the
92+
// actions with.
93+
repos[repo] = {
94+
node {
95+
// Get the master refs to create the branches from
96+
httpRequest(
97+
contentType: 'APPLICATION_JSON',
98+
consoleLogResponseBody: true,
99+
customHeaders: [[maskValue: true, name: 'Authorization', value: "token ${githubToken}"]],
100+
httpMode: 'GET',
101+
outputFile: "${p.org}_${repo}_master_refs.json",
102+
url: "${githubUrl}/repos/${p.org}/${repo}/git/refs/heads/master")
103+
// Create a variable with the values from the GET response
104+
masterRefs = readJSON(file: "${p.org}_${repo}_master_refs.json")
105+
// Define the payload for the GitHub API call
106+
payload = """{
107+
"ref": "refs/heads/${name}",
108+
"sha": "${masterRefs['object']['sha']}"
109+
}"""
110+
// Create the new branches
111+
httpRequest(
112+
contentType: 'APPLICATION_JSON',
113+
consoleLogResponseBody: true,
114+
customHeaders: [[maskValue: true, name: 'Authorization', value: "token ${githubToken}"]],
115+
httpMode: 'POST',
116+
ignoreSslErrors: false,
117+
requestBody: payload,
118+
responseHandle: 'NONE',
119+
url: "${githubUrl}/repos/${p.org}/${repo}/git/refs")
120+
}
121+
}
122+
}
123+
// Execute the API calls simultaneously for each repo in the Org
124+
parallel repos
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}
131+
stage('Create Release') {
132+
when {
133+
// Let's only run this stage when we have a 'version created' event
134+
expression { event == 'jira:version_released' }
135+
}
136+
steps {
137+
script {
138+
// Specify our credentials to use for the steps
139+
withCredentials([usernamePassword(credentialsId: '<github_credentials_id>',
140+
passwordVariable: 'githubToken',
141+
usernameVariable: 'githubUser')]) {
142+
// Loop through our list of Projects in Jira, which will map to Orgs in GitHub.
143+
// We're assigning it 'p' since 'project' is assigned as part of the YAML structure
144+
settings.project.each { p ->
145+
// Only apply this release to the proper Org
146+
if (p.name.toString() == projectInfo.data.name.toString()) {
147+
// Loop through each repo in the Org
148+
p.repos.each { repo ->
149+
// Create an array that we will use to dynamically parallelize the actions with.
150+
repos[repo] = {
151+
node {
152+
// Get the current releases
153+
httpRequest(
154+
contentType: 'APPLICATION_JSON',
155+
consoleLogResponseBody: true,
156+
customHeaders: [[maskValue: true, name: 'Authorization', value: "token ${githubToken}"]],
157+
httpMode: 'GET',
158+
outputFile: "${p.org}_${repo}_releases.json",
159+
url: "${githubUrl}/repos/${p.org}/${repo}/releases")
160+
// Create a variable with the values from the GET response
161+
releases = readJSON(file: "${p.org}_${repo}_releases.json")
162+
// Define the payload for the GitHub API call
163+
def payload = """{
164+
"tag_name": "${name}",
165+
"target_commitish": "${name}",
166+
"name": "${name}",
167+
"body": "${description}",
168+
"draft": false,
169+
"prerelease": false
170+
}"""
171+
// Create the new release
172+
httpRequest(
173+
contentType: 'APPLICATION_JSON',
174+
consoleLogResponseBody: true,
175+
customHeaders: [[maskValue: true, name: 'Authorization', value: "token ${githubToken}"]],
176+
httpMode: 'POST',
177+
ignoreSslErrors: false,
178+
requestBody: payload,
179+
responseHandle: 'NONE',
180+
url: "${githubUrl}/repos/${p.org}/${repo}/releases")
181+
}
182+
}
183+
}
184+
// Execute the API calls simultaneously for each repo in the Org
185+
parallel repos
186+
}
187+
}
188+
}
189+
}
190+
}
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)