-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoJenkinsfile
115 lines (104 loc) · 4.47 KB
/
AutoJenkinsfile
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
def archivedArtifact = 'marketplace-front-SNAPSHOT-bin.zip'
pipeline {
agent any
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(daysToKeepStr: '15', numToKeepStr: '5'))
}
triggers {
gitlab(triggerOnPush: true, branchFilterType: 'All')
}
tools {
nodejs "nodejs-14.15.0"
}
parameters {
booleanParam(name: 'cleanup', defaultValue: false, description: 'Cleanup workspace before build')
}
stages {
stage('Dependencies') {
options {
timeout(time: 20, unit: 'MINUTES')
}
steps {
sh 'git status'
sh 'node -v'
sh 'npm --version'
sh 'npm config list'
script {
if (params.cleanup) {
echo 'Cleaning node_modules, dist & package-lock.json'
sh "rm -rf node_modules || true"
sh "rm -rf dist || true"
sh "rm -rf package-lock.json || true"
}
}
sh 'npm install'
// Using this path instead of directly "gridsome info" prevents "command not found" on Jenkins
sh 'node ./node_modules/gridsome/bin/gridsome.js info'
}
}
stage('Build') {
options {
timeout(time: 10, unit: 'MINUTES')
}
steps {
sh 'npm run build'
zip dir: 'dist', zipFile: archivedArtifact, archive: true
}
}
stage('QA') {
options {
timeout(time: 15, unit: 'MINUTES')
}
steps {
echo 'Dependency check for vulnerabilities'
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
sh 'npm audit --production --audit-level=critical'
}
echo 'Source code quality check'
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
sh 'npm run lint'
}
/* FIXME re-enable on first tests added to the project
echo 'Unit tests with Jest'
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
sh 'npm run test'
}*/
}
}
}
post {
unsuccessful {
script {
def commitMessage = sh (script: 'git log -1 --pretty=%B', returnStdout: true).trim()
def commitAuthor = sh (script: 'git log -1 --pretty=%aN', returnStdout: true).trim()
def commitEmail = sh (script: 'git log -1 --pretty=%aE', returnStdout: true).trim()
def commitHash = sh (script: 'git log -1 --pretty=%h', returnStdout: true).trim()
def jobNameParts = env.JOB_NAME.tokenize('/') as String[]
def shortJobName = jobNameParts.length < 2 ? env.JOB_NAME : jobNameParts[jobNameParts.length - 2]
mail subject: "${currentBuild.currentResult}: ${shortJobName}",
from: 'Jenkins <[email protected]>',
to: commitEmail,
mimeType: 'text/html',
body: """
<html><body>
<h1>Branch ${env.BRANCH_NAME} is ${currentBuild.currentResult}</h1>
<h2>Suggested action</h2>
<p><a href="${env.BUILD_URL}console">Check job execution log</a> and fix build issues.</p>
<h2>Execution</h2>
<p>Build <strong>#${env.BUILD_NUMBER}</strong> run by the job <strong>${env.JOB_NAME}</strong> was
triggered by commit <strong>${commitHash}</strong> from <strong>${commitAuthor}</strong>: </p>
<pre>${commitMessage}</pre>
<h2>Outcome</h2>
<p>${currentBuild.currentResult == 'UNSTABLE' ? 'Web app archived here: <a href="' + env.BUILD_URL + '/artifact/' + archivedArtifact + '">' + archivedArtifact + '</a>.' : 'Build failed entirely.'}</p>
</body></html>
"""
}
}
always {
// Don't delete 'node_modules' to speed-up builds
sh "rm -f ${archivedArtifact} || true"
sh "rm -rf dist || true"
}
}
}