forked from socrata-platform/soda-fountain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
161 lines (142 loc) · 5.56 KB
/
Jenkinsfile
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Set up the libraries
@Library('socrata-pipeline-library')
// set up service and project variables
def service = "soda-fountain"
def project_wd = "soda-fountain-jetty"
def deploy_service_pattern = "soda-fountain"
def deploy_environment = "staging"
def service_sha = env.GIT_COMMIT
// variables that determine which stages we run based on what triggered the job
def boolean stage_cut = false
def boolean stage_build = false
def boolean stage_dockerize = false
def boolean stage_deploy = false
// instanciate libraries
def sbtbuild = new com.socrata.SBTBuild(steps, service, project_wd)
def dockerize = new com.socrata.Dockerize(steps, service, BUILD_NUMBER)
def deploy = new com.socrata.MarathonDeploy(steps)
pipeline {
options {
ansiColor('xterm')
}
parameters {
booleanParam(name: 'RELEASE_CUT', defaultValue: false, description: 'Are we cutting a new release candidate?')
booleanParam(name: 'FORCE_BUILD', defaultValue: false, description: 'Force build from latest tag if sbt release needed to be run between cuts')
string(name: 'AGENT', defaultValue: 'build-worker', description: 'Which build agent to use?')
}
agent {
label params.AGENT
}
environment {
PATH = "${WORKER_PATH}"
}
stages {
stage('Setup') {
steps {
script {
// checkout the repo with whatever branch/sha provided by the github hook
checkout scm
// set the service sha to what was checked out (GIT_COMMIT isn't always set)
service_sha = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
// determine what triggered the build and what stages need to be run
if (params.RELEASE_CUT == true) { // RELEASE_CUT parameter was set by a cut job
stage_cut = true // other stages will be turned on in the cut step as needed
deploy_environment = "rc"
}
else if (env.CHANGE_ID != null) { // we're running a PR builder
stage_build = true
}
else if (BRANCH_NAME == "master") { // we're running a build on master branch to deploy to staging
stage_build = true
stage_dockerize = true
stage_deploy = true
}
else {
// we're not sure what we're doing...
echo "Unknown build trigger - Exiting as Failure"
currentBuild.result = 'FAILURE'
return
}
}
}
}
stage('Cut') {
when { expression { stage_cut } }
steps {
script {
def cutNeeded = false
// get a list of all files changes since the last tag
files = sh(returnStdout: true, script: "git diff --name-only HEAD `git describe --match \"v*\" --abbrev=0`").trim()
echo "Files changed:\n${files}"
if (files == 'version.sbt') {
// Build anyway using latest tag - needed if sbt release had to be run between cuts
// This parameter will need to be set by the cut job in Jenkins
if(params.FORCE_BUILD) {
cutNeeded = true
}
else {
echo "No build needed, skipping subsequent steps"
}
}
else {
echo 'Running sbt-release'
// The git config setup required for your project prior to running 'sbt release with-defaults' may vary:
sh(returnStdout: true, script: "git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*")
sh(returnStdout: true, script: "git config branch.master.remote origin")
sh(returnStdout: true, script: "git config branch.master.merge refs/heads/master")
echo sh(returnStdout: true, script: "echo y | sbt \"release with-defaults\"")
cutNeeded = true
}
if(cutNeeded == true) {
echo 'Getting release tag'
release_tag = sh(returnStdout: true, script: "git describe --abbrev=0 --match \"v*\"").trim()
branchSpecifier = "refs/tags/${release_tag}"
echo branchSpecifier
// checkout the tag so we're performing subsequent actions on it
sh "git checkout ${branchSpecifier}"
// set the service_sha to the current tag because it might not be the same as env.GIT_COMMIT
service_sha = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
// set later stages to run since we're cutting
stage_build = true
stage_dockerize = true
stage_deploy = true
}
}
}
}
stage('Build') {
when { expression { stage_build } }
steps {
script {
// perform any needed modifiers on the build parameters here
sbtbuild.setNoSubproject(true)
sbtbuild.setScalaVersion("2.12")
sbtbuild.setSubprojectName("sodaFountainJetty")
// build
echo "Building sbt project..."
sbtbuild.build()
}
}
}
stage('Dockerize') {
when { expression { stage_dockerize } }
steps {
script {
echo "Building docker container..."
dockerize.docker_build(sbtbuild.getServiceVersion(), service_sha, "./docker", sbtbuild.getDockerArtifact())
}
}
}
stage('Deploy') {
when { expression { stage_deploy } }
steps {
script {
// Checkout and run bundle install in the apps-marathon repo
deploy.checkoutAndInstall()
// deploy the service to the specified environment
deploy.deploy(deploy_service_pattern, deploy_environment, dockerize.getDeployTag())
}
}
}
}
}