-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
221 lines (167 loc) · 10.7 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
BUILD_NEEDED = false
pipeline {
agent {
node {
label 'aws-ec2'
}
}
environment {
NODEJS_DIR = "/usr/local/lib/nodejs"
NODE_TAR_FILE = "node-v14.18.0-linux-x64.tar.gz"
NODE_VER_BUILD = "node-v14.18.0-linux-x64"
NODE_DIST = "https://nodejs.org/dist/v14.18.0/${NODE_TAR_FILE}"
N8N_SETUP_DIR = "${HOME}/workspace/n8n-setup"
N8N_HOME = "${HOME}/workspace/n8n"
//PROXY_FILE = "/etc/apt/apt.conf.d/00-proxy"
//VERIFY_PEER_CONFIG_FILE = "/etc/apt/apt.conf.d/99-verify-peer"
__REPO_N8N = "https://github.com/krish918/n8n.git"
}
stages {
stage("Build N8N") {
steps {
// We will clone and setup N8N one level above the current working dir
dir("${HOME}/workspace") {
script {
// Check if N8N directory already exists in the workspace , otherwise clone the N8N repo
if ( !fileExists (N8N_HOME) ) {
sh 'git clone "$__REPO_N8N"'
// If cloned, then it needs to be built
BUILD_NEEDED = true
}
}
}
dir( N8N_SETUP_DIR ) {
script {
/*
setup.conf file is created in N8N_SETUP_DIR, once the N8N BUILD is successful.
If it is already available, then we don't need to RE-BUILD.
*/
if ( !fileExists ('./setup.conf') ) {
BUILD_NEEDED = true
// Commenting out, as we don't need to setup proxy on aws node.
/*
if ( !fileExists( PROXY_FILE ) ) {
sh 'echo "Acquire::http::proxy \\"http://proxy-dmz.intel.com:911\\";\nAcquire::https::proxy \\"http://proxy-dmz.intel.com:912\\";" >> "$PROXY_FILE"'
}
*/
sh 'sudo apt-get update -y'
// If nodejs tarball is not available, then download it.
if ( !fileExists ( NODE_TAR_FILE ) ) {
sh 'sudo apt-get install -y curl'
sh 'curl -O "${NODE_DIST}"'
}
// We create the dir, where we will be installing N8N recommended version of nodejs, in case nodejs is not pre-installed.
sh "sudo mkdir -p ${NODEJS_DIR}"
// Extract the nodejs tarball, if it is not already extracted.
dir_empty = sh ( script: 'test -z "$(ls -A $NODEJS_DIR)"', returnStatus: true ) == 0
if ( dir_empty == true ) {
sh 'sudo apt-get install -y tar gzip'
sh "sudo tar -xzf ${NODE_TAR_FILE} -C ${NODEJS_DIR}"
sh 'sudo chown -R $(whoami) "$NODEJS_DIR"'
}
// Install build tools to be able to build N8N
sh 'sudo apt-get install -y build-essential python'
/*
If npm and node commands are not already available, then
make them available via a symbolic link to the nodejs version, which we just
extracted in NODEJS_DIR.
*/
npm_exist = sh ( script: 'test -L /usr/bin/npm', returnStatus: true ) == 0
if ( npm_exist == false ) {
sh "sudo ln -s ${NODEJS_DIR}/${NODE_VER_BUILD}/bin/npm /usr/bin/npm"
}
node_exist = sh ( script: 'test -L /usr/bin/node', returnStatus: true ) == 0
if ( npm_exist == false ) {
sh "sudo ln -s ${NODEJS_DIR}/${NODE_VER_BUILD}/bin/node /usr/bin/node"
}
// Check for existence of lerna (needed to build N8N) and install if not available.
lerna_exist = sh ( script: 'test -L /usr/bin/lerna', returnStatus: true ) == 0
if ( lerna_exist == false ) {
sh 'sudo npm install -g lerna'
//sh "sudo ln -s ${NODEJS_DIR}/${NODE_VER_BUILD}/bin/lerna /usr/bin/lerna"
}
}
}
}
dir( N8N_HOME ) {
script {
UPDATED = false
/*
If N8N is already built, then check if there are new commits on the repo.
If yes, the turn on the UPDATED flag, so that N8N can be re-built.
*/
if ( BUILD_NEEDED == false ) {
sh 'git remote update'
LOCAL = sh( script: 'git rev-parse @', returnStdout: true ).trim()
REMOTE = sh( script: 'git rev-parse @{u}', returnStdout: true ).trim()
if ( LOCAL != REMOTE ) {
sh 'git pull origin master'
UPDATED = true
}
}
/*
Build N8N, either it is first time setup (BUILD_NEEDED = true) or
if there are some new commits on the repo (UPDATED = true)
*/
if ( BUILD_NEEDED == true || UPDATED == true ) {
sh 'lerna bootstrap --hoist'
sh 'sudo npm run build'
sh "$N8N_HOME/packages/cli/bin/n8n --version"
// Create a dummy file in N8N_SETUP_DIR which signals that N8N is built successfully.
if ( !fileExists ("$N8N_SETUP_DIR/setup.conf")) {
sh 'echo "INITIAL_SETUP_DONE" >> "${N8N_SETUP_DIR}/setup.conf"'
}
}
}
}
}
}
stage("Setup Microservices") {
steps {
script {
// Check whether docker exists, otherwise setup docker on the current node.
docker_exist = sh (script : 'command -v docker', returnStatus : true) == 0
if ( !docker_exist ) {
sh 'sudo apt-get install -y ca-certificates gnupg lsb-release'
//sh 'echo "Acquire { https::Verify-Peer \\"false\\" }" >> "$VERIFY_PEER_CONFIG_FILE"'
sh 'curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg'
sh 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null'
sh 'sudo apt-get update -y'
sh 'sudo apt-get install -y docker-ce docker-ce-cli containerd.io'
// Check if a docker group exists, otherwise create it.
docker_grp = sh (script : 'sudo getent group docker', returnStatus : true ) == 0
if ( !docker_grp ) {
sh 'sudo groupadd docker'
}
// Add current user to docker group to allow running docker without sudo
sh 'sudo usermod -aG docker "$(whoami)"'
}
// Start docker daemon
sh 'sudo systemctl start docker'
// Install docker-compose if not available on current node.
docker_compose = sh (script : 'command -v docker-compose', returnStatus : true) == 0
if ( !docker_compose ) {
sh 'sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose'
sh 'sudo chmod +x /usr/local/bin/docker-compose'
sh 'sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose'
}
// If docker-compose file exists in the current repo, spin-up the services.
if ( fileExists ('docker-compose.yml') ) {
sh 'docker-compose -p jenkins-n8n up -d'
}
}
}
}
stage("Import & Execute Workflow") {
steps {
// Import credentials from this repo into N8N
sh "$N8N_HOME/packages/cli/bin/n8n import:credentials --input=credentials.json"
// Copy the encryption key (needed to decrypt credentials) into .n8n directory of current user's home.
sh 'cp config "${HOME}/.n8n/"'
// EXECUTE THE WORKFLOW
sh 'echo "Executing Workflow..."'
sh "$N8N_HOME/packages/cli/bin/n8n execute --file workflow.json"
}
}
}
}