-
Notifications
You must be signed in to change notification settings - Fork 9
/
Jenkinsfile
165 lines (159 loc) · 5.9 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
162
163
164
165
#!/usr/bin/env groovy
def loadAppinhouseConf() {
def resource = libraryResource 'appinhouse.yml'
return readYaml(text: resource)
}
def getHost() {
def appinhouseConf = loadAppinhouseConf()
def remote = [:]
remote.name = appinhouseConf.name
remote.host = appinhouseConf.host
remote.allowAnyHosts = true
remote.user = appinhouseConf.userName
return remote
}
remote = getHost()
def deployAppinhouseServer(String secretKey, String domain) {
sshPut remote: remote, from: env.SERVER_TARBALL, into: '/tmp'
sshPut remote: remote, from: env.WORKSPACE + '/src/appinhouse/deploy/appinhouse.service', into: '/tmp'
sshPut remote: remote, from: env.WORKSPACE + '/src/appinhouse/deploy/server.sh', into: '/tmp'
sshCommand remote: remote, command: "bash /tmp/server.sh "+ env.SERVER_TARBALL + " " + secretKey + " " + domain
sshRemove remote: remote, path: '/tmp/' + env.SERVER_TARBALL
}
def deployAppinhouseWeb() {
sshPut remote: remote, from: env.WEB_TARBALL, into: '/tmp'
sshPut remote: remote, from: env.WORKSPACE + '/src/appinhouse/deploy/web.sh', into: '/tmp'
sshCommand remote: remote, command: "bash /tmp/web.sh "+ env.WEB_TARBALL
sshRemove remote: remote, path: '/tmp/' + env.WEB_TARBALL
}
def deployAppinhouseNginxConf() {
sshPut remote: remote, from: env.WORKSPACE + '/src/appinhouse/deploy/appinhouse.conf', into: '/tmp'
sshPut remote: remote, from: env.WORKSPACE + '/src/appinhouse/deploy/nginx.sh', into: '/tmp'
sshCommand remote: remote, command: "bash /tmp/nginx.sh"
}
pipeline {
agent {
label "os:linux"
}
options {
skipDefaultCheckout()
disableConcurrentBuilds()
buildDiscarder(logRotator(
daysToKeepStr: '15',
artifactNumToKeepStr: '20'
))
ansiColor('xterm')
}
parameters {
booleanParam(name: 'DEPLOY_SERVER',
defaultValue: false,
description: 'When checked, will automatically deploy server (backend).')
booleanParam(name: 'DEPLOY_WEB',
defaultValue: false,
description: 'When checked, will automatically deploy web (frontend).')
booleanParam(name: 'DEPLOY_NGINX',
defaultValue: false,
description: 'When checked, will automatically deploy nginx (frontend).')
}
environment {
GITHUB_URL = 'https://github.com/rog2/appinhouse'
GO_VERSION = '1.10'
GOPATH = "${env.WORKSPACE}"
SERVER_ROOT = "${env.WORKSPACE}/src/appinhouse/server"
WEB_ROOT = "${env.WORKSPACE}/src/appinhouse/web"
SERVER_TARBALL = artifactName(name: 'appinhouse', extension: 'server.tar.gz')
WEB_TARBALL = artifactName(name: 'appinhouse', extension: 'web.tar.gz')
}
stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM',
userRemoteConfigs: [[url: env.GITHUB_URL]],
branches: [[name: env.BRANCH_NAME ?: 'master']],
browser: [$class: 'GithubWeb', repoUrl: env.GITHUB_URL],
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'src/appinhouse']]
])
}
}
stage('Remove Last Build') {
steps {
sh 'rm -vf *.tar.gz'
}
}
stage('Build Server') {
steps {
dir (env.SERVER_ROOT) {
withGo(env.GO_VERSION) {
sh """
go get -v github.com/beego/bee
go get -v
go build -o appinhouse
${env.GOPATH}/bin/bee pack -o ${env.WORKSPACE} -exr pack.sh -exr server -exr test.conf -exr bin
"""
}
}
sh "mv server.tar.gz ${env.SERVER_TARBALL}"
}
}
stage('Build Web') {
steps {
dir (env.WEB_ROOT) {
sh "tar czvf ${env.WORKSPACE}/${env.WEB_TARBALL} static"
}
}
}
stage('Archive') {
steps {
archiveArtifacts artifacts: '*.tar.gz', onlyIfSuccessful: true
}
}
stage('Deploy Server') {
when {
expression {
return params.DEPLOY_SERVER
}
}
steps {
script {
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins.ssh', keyFileVariable: 'identity')]) {
remote.identityFile = identity
def appinhouseConf = loadAppinhouseConf()
withCredentials([string(credentialsId: appinhouseConf.credential, variable: 'secretKey')]) {
deployAppinhouseServer(secretKey, appinhouseConf.domain)
}
}
}
}
}
stage('Deploy Web') {
when {
expression {
return params.DEPLOY_WEB
}
}
steps {
script {
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins.ssh', keyFileVariable: 'identity')]) {
remote.identityFile = identity
deployAppinhouseWeb()
}
}
}
}
stage('Deploy Nginx conf') {
when {
expression {
return params.DEPLOY_NGINX
}
}
steps {
script {
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins.ssh', keyFileVariable: 'identity')]) {
remote.identityFile = identity
deployAppinhouseNginxConf()
}
}
}
}
}
}