forked from Fundflow/apollo-redux-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.groovy
97 lines (79 loc) · 3.79 KB
/
pipeline.groovy
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
95
96
97
properties([
disableConcurrentBuilds()
])
node {
String version = ''
try {
stage('Checkout') {
checkout scm
}
// Workaround for https://issues.jenkins-ci.org/browse/JENKINS-35988
if (!skipBuildIfTriggeredByJenkins()) {
docker.image("node:8").inside {
stage('Build') {
// Need this to fetch private github dependencies and version the application
withCredentials([usernamePassword(credentialsId: 'github-fundflow-jenkins', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_PASSWORD')]) {
sh 'git config --global url."https://${GITHUB_USER}:${GITHUB_PASSWORD}@github.com/".insteadOf "git://github.com/"'
sh 'git config --global user.email [email protected]'
sh 'git config --global user.name jenkins'
}
sh 'npm install --verbose'
sh 'CI=true npm run test'
// Workaround for the fact that npm install makes changes to package-lock.json
sh 'git checkout -- package-lock.json'
sh 'git checkout -- package.json'
if (env.BRANCH_NAME in ['master']) {
sh 'npm version patch'
} else {
sh 'npm version prepatch'
}
version = sh(returnStdout: true, script: "npm version | grep \"{\" | tr -s ':' | cut -d \"'\" -f 4").trim()
}
}
if (env.BRANCH_NAME in ['master']) {
stage('Publish Version') {
echo "Tagging repository and updating package.json with release version ${version} ..."
// Required due to https://issues.jenkins-ci.org/browse/JENKINS-28335
withCredentials([usernamePassword(credentialsId: 'github-fundflow-jenkins', usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_PASSWORD')]) {
sh 'git config --global url."https://${GITHUB_USER}:${GITHUB_PASSWORD}@github.com/".insteadOf "https://github.com/"'
sh """git push origin HEAD:${env.BRANCH_NAME}"""
sh """git push origin v${version}"""
}
}
}
}
} finally {
deleteDir()
}
}
@NonCPS
boolean skipBuildIfTriggeredByJenkins() {
boolean validChangeDetected = false
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
if (!"jenkins".equals(entry.author.getFullName())) {
validChangeDetected = true
println "Changes found by ${entry.author} (${entry.commitId})"
} else {
println "Automated change found by Jenkins (${entry.commitId})"
}
}
}
def buildCauses = currentBuild.rawBuild.getCauses()
boolean userTriggeredBuild = buildCauses[buildCauses.size()-1] instanceof Cause.UserIdCause
// We are building if there are some valid changes or if there are no changes
// (so the build was triggered intentionally or it is the first run)
if (!validChangeDetected && !userTriggeredBuild) {
if (changeLogSets.size() != 0) {
println "Skipping build. No changes from authors other than Jenkins."
currentBuild.result = currentBuild.rawBuild.getPreviousBuild()?.result?.toString()
return true;
} else {
println "No changes found. Assuming manual run of build or first run."
}
}
return false;
}