-
Notifications
You must be signed in to change notification settings - Fork 104
/
Jenkinsfile-deploy.example
159 lines (155 loc) · 8.4 KB
/
Jenkinsfile-deploy.example
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
/* The following example Jenkinsfile is a skeleton to copy into your CI
* scripts repository and configure a MultiBranch Pipeline job from it.
* This would create a job instance from every branch that contains this
* file, so you can adapt the content to your installation's tools and
* call this job to deploy projects generated with zproject.
*
* Then in your Jenkins Management Web-GUI go to Configure System /
* / Global properties / Environment variables and Add a new variable
* DEFAULT_DEPLOY_JOB_NAME with value like '/my-deploy/master', and a
* DEFAULT_DEPLOY_BRANCH_PATTERN like '^(master|feature/,*|release/.*)$'
*
* Contributed to zproject, Copyright (C) 2017 by Eaton
* See the reference version of this file at
* https://github.com/zeromq/zproject/blob/master/Jenkinsfile-deploy.example
* Author: Jim Klimov <EvgenyKlimov@eaton.com>
*/
pipeline {
// Select an agent with proper tools and access needed for deployment
agent { label "deploy-tools" }
parameters {
string (
defaultValue: '',
description: 'Git repo URL with sources',
name: 'DEPLOY_GIT_URL')
string (
defaultValue: '',
description: 'The branch which passed tests (e.g. "master") - influences which OBS project to push into',
name: 'DEPLOY_GIT_BRANCH')
string (
defaultValue: '',
description: 'The commit ID of the sources to check out (may be not current HEAD of that repo/branch)',
name: 'DEPLOY_GIT_COMMIT')
string (
defaultValue: '',
description: 'URL to a "dist" archive prepared by the build (e.g. with pregenerated docs and config script) which will be fetched, and used instead of (or combined with) git-archive checkout contents during further packaging; note all the DEPLOY_GIT_* arguments are still required if this feature is used.',
name: 'DEPLOY_DIST_ARCHIVE')
string (
defaultValue: 'https://github.com/myorg/myjenkinsscripts.git',
description: 'GIT Repo with CI scripts and tools',
name: 'CI_REPO_FORK')
string (
defaultValue: 'origin/master',
description: '',
name: 'CI_REPO_BRANCH')
string (
defaultValue: '',
description: '',
name: 'CI_REPO_REFSPEC')
// You may have more parameters passed here, to pass them down to your
// deployment script; make sure to set some sane defaults or guess blank
// values in your scripts
}
stages {
stage ('Sanity check') {
steps {
script {
if ( "${params["CI_REPO_BRANCH"]}" == "" || "${params["CI_REPO_FORK"]}" == "" ||
"${params["DEPLOY_GIT_URL"]}" == "" || "${params["DEPLOY_GIT_BRANCH"]}" == ""
) {
echo "BUILD_CAUSE : ${env.BUILD_CAUSE}"
script {
if ( env.BUILD_CAUSE_SCMTRIGGER == "true" || env.BUILD_CAUSE == "SCMTRIGGER" ) {
// Not quite green, but not an outright failure either
manager.addShortText("Sanity: Missing critical arguments, SCM triggered run")
currentBuild.result = 'ABORTED'
manager.buildUnstable()
error("This run did not have critical arguments set; it looks like an automatic build of refreshed CI repo.")
} else {
// Bad arguments on manual/scripted invokation = FAIL
manager.addShortText("Sanity: Missing critical arguments, normal build was attempted")
currentBuild.result = 'FAILURE'
manager.buildAborted()
error("This run did not have critical arguments set; it looks like an operator error.")
}
return
}
}
}
// Comment away the block below if you have some need
// to publish the codebase of ephemeral PR branches.
// This is a Jenkins server instance implementation
// detail; with proper DEPLOY_BRANCH_PATTERN settings
// you would not even hit this clause unless desired.
script {
if ( "${params["DEPLOY_GIT_BRANCH"]}" =~ /^PR-[0-9]+/ ) {
manager.addShortText("Sanity: ephemeral PR branches should not be published")
currentBuild.result = 'ABORTED'
manager.buildAborted()
error("This run tried to publish an ephemeral PR branch; it looks like an operator error.")
}
}
}
}
stage ('Checkout') {
parallel {
stage ('Checkout SRC') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params["DEPLOY_GIT_BRANCH"]}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'WipeWorkspace'],
[$class: 'RelativeTargetDirectory', relativeTargetDir: 'project'],
[$class: 'CloneOption', noTags: false, shallow: false]],
submoduleCfg: [],
userRemoteConfigs: [[url: "${params["DEPLOY_GIT_URL"]}", refspec: "${params["DEPLOY_GIT_COMMIT"]}"]]
// Maybe also pass a credentialsId with account who may read your repos, if access is restricted
])
}
}
stage ('Checkout CI scripts') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: "${params["CI_REPO_BRANCH"]}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'WipeWorkspace'],
[$class: 'RelativeTargetDirectory', relativeTargetDir: 'ci'],
[$class: 'CloneOption', depth: 3, noTags: true, shallow: true, reference: "${env.WORKSPACE}/.git"]],
// Optimization above assumes that this Jenkinsfile-deploy lives in the same repo, so we know it is already checked out
submoduleCfg: [],
userRemoteConfigs: [[url: "${params["CI_REPO_FORK"]}", refspec: "${params["CI_REPO_REFSPEC"]}"]]
])
}
}
}
}
// Certainly, customize the code below to match your tools and their params
stage ('Deploy') {
steps {
script {
manager.addShortText("Pushing: " + ( (params.DEPLOY_GIT_URL).substring( (params.DEPLOY_GIT_URL).lastIndexOf("/") + 1 ).replace(".git", "") ) + " / " + params.DEPLOY_GIT_BRANCH + " @ " + ( (params.DEPLOY_GIT_COMMIT).take(7) ) + ( (params.DEPLOY_DIST_ARCHIVE == "") ? "(no dist tarballs)" : (" using " + params.DEPLOY_DIST_ARCHIVE) ) )
def statusCode = sh returnStatus:true, script: """
DEPLOY_GIT_URL="${params["DEPLOY_GIT_URL"]}"
DEPLOY_GIT_BRANCH="${params["DEPLOY_GIT_BRANCH"]}"
DEPLOY_GIT_COMMIT="${params["DEPLOY_GIT_COMMIT"]}"
DEPLOY_DIST_ARCHIVE="${params["DEPLOY_DIST_ARCHIVE"]}"
export DEPLOY_GIT_URL DEPLOY_GIT_BRANCH DEPLOY_GIT_COMMIT DEPLOY_DIST_ARCHIVE
cd project && \
../ci/update-deployment.sh
"""
if ( statusCode == 42 ) {
manager.addShortText("Sanity: update-deployment.sh refused the git URL/branch: " + params.DEPLOY_GIT_URL + " / " + params.DEPLOY_GIT_BRANCH)
currentBuild.result = 'ABORTED'
manager.buildUnstable()
error("Sanity: update-deployment.sh refused the git URL/branch; it looks like an automatic build of a master/release branch in a developer's repo: " + params.DEPLOY_GIT_URL + " / " + params.DEPLOY_GIT_BRANCH)
} else {
if ( statusCode != 0 ) {
currentBuild.result = 'FAILURE'
manager.buildAborted()
}
}
}
}
}
}
}