-
Notifications
You must be signed in to change notification settings - Fork 570
/
Jenkinsfile
98 lines (84 loc) · 3.48 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
/*
* Copyright (c) 2016-present Sonatype, Inc. All rights reserved.
* Includes the third-party code listed at http://links.sonatype.com/products/nxrm/attributions.
* "Sonatype" is a trademark of Sonatype, Inc.
*/
@Library(['private-pipeline-library', 'jenkins-shared']) _
import com.sonatype.jenkins.pipeline.GitHub
import com.sonatype.jenkins.pipeline.OsTools
import com.sonatype.jenkins.shared.Expectation
node('ubuntu-zion') {
def commitId, commitDate, imageId, branch
def organization = 'sonatype',
gitHubRepository = 'docker-nexus3',
imageName = 'sonatype/nexus3',
archiveName = 'docker-nexus3',
dockerHubRepository = 'nexus3'
GitHub gitHub
try {
stage('Preparation') {
deleteDir()
OsTools.runSafe(this, 'docker system prune -a -f')
def checkoutDetails = checkout scm
branch = checkoutDetails.GIT_BRANCH == 'origin/main' ? 'main' : checkoutDetails.GIT_BRANCH
commitId = checkoutDetails.GIT_COMMIT
commitDate = OsTools.runSafe(this, "git show -s --format=%cd --date=format:%Y%m%d-%H%M%S ${commitId}")
OsTools.runSafe(this, 'git config --global user.email sonatype-ci@sonatype.com')
OsTools.runSafe(this, 'git config --global user.name Sonatype CI')
def apiToken
withCredentials([usernamePassword(credentialsId: 'jenkins-github',
usernameVariable: 'GITHUB_APP',
passwordVariable: 'GITHUB_ACCESS_TOKEN')]) {
apiToken = env.GITHUB_ACCESS_TOKEN
}
gitHub = new GitHub(this, "${organization}/${gitHubRepository}", apiToken)
}
stage('Build') {
gitHub.statusUpdate commitId, 'pending', 'build', 'Build is running'
def hash = OsTools.runSafe(this, "docker build --quiet --no-cache --tag ${imageName} .")
imageId = hash.split(':')[1]
if (currentBuild.result == 'FAILURE') {
gitHub.statusUpdate commitId, 'failure', 'build', 'Build failed'
return
} else {
gitHub.statusUpdate commitId, 'success', 'build', 'Build succeeded'
}
}
stage('Test') {
gitHub.statusUpdate commitId, 'pending', 'test', 'Tests are running'
validateExpectations([
new Expectation('Has user nexus in group nexus present',
'id', '-ng nexus', 'nexus'),
new Expectation('Has nexus user java process present',
'ps', '-e -o command,user | grep -q ^/usr/lib/jvm/java.*nexus$ | echo $?', '0')
])
if (currentBuild.result == 'FAILURE') {
gitHub.statusUpdate commitId, 'failure', 'test', 'Tests failed'
return
}
gitHub.statusUpdate commitId, 'success', 'test', 'Tests succeeded'
}
stage('Evaluate Policies') {
runEvaluation({ stage ->
nexusPolicyEvaluation(
iqStage: stage,
iqApplication: 'docker-nexus3',
iqScanPatterns: [[scanPattern: "container:${imageName}"]],
failBuildOnNetworkError: true,
)}, (branch == 'main') ? 'build' : 'develop')
}
if (currentBuild.result == 'FAILURE') {
return
}
stage('Archive') {
dir('build/target') {
OsTools.runSafe(this, "docker save ${imageName} | gzip > ${archiveName}.tar.gz")
archiveArtifacts artifacts: "${archiveName}.tar.gz", onlyIfSuccessful: true
}
}
} finally {
OsTools.runSafe(this, 'docker logout')
OsTools.runSafe(this, 'docker system prune -a -f')
OsTools.runSafe(this, 'git clean -f && git reset --hard origin/main')
}
}