-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathJenkinsfile
96 lines (90 loc) · 3.61 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
// This pipeline revolves around building a Docker image:
// - Lint: Lints a Dockerfile using hadolint
// - Build and test: Builds and tests a Docker image
// - Push: Pushes the image to the registry
pipeline {
agent any
environment { // Environment variables defined for all steps
DOCKER_IMAGE = "registry.demo.local:5000/tools-image"
}
stages {
stage("Lint") {
agent {
docker {
image "docker.io/hadolint/hadolint:v1.18.0"
reuseNode true
}
}
steps {
script {
def result = sh label: "Lint Dockerfile",
script: """\
hadolint Dockerfile > hadolint-results.txt
""",
returnStatus: true
if (result > 0) {
unstable(message: "Linting issues found")
}
}
}
}
stage("Build and test image") {
steps {
script {
// Use commit tag if it has been tagged
tag = sh(returnStdout: true, script: "git tag --contains").trim()
if ("$tag" == "") {
if ("${BRANCH_NAME}" == "master") {
tag = "latest"
} else {
tag = "${BRANCH_NAME}"
}
}
def image = docker.build("$DOCKER_IMAGE", "--build-arg 'BUILDKIT_INLINE_CACHE=1' --cache-from $DOCKER_IMAGE:$tag --cache-from $DOCKER_IMAGE:latest .")
// Make sure that the user ID exists within the container
image.inside("--volume /etc/passwd:/etc/passwd:ro") {
sh label: "Test anchore-cli",
script: "anchore-cli --version"
sh label: "Test curl",
script: "curl --version"
sh label: "Test cyclonedx",
script: "cyclonedx-py --help"
sh label: "Test detect-secrets",
script: "detect-secrets --version"
sh label: "Test nikto.pl",
script: "nikto.pl -Version"
sh label: "Test for outdated global npm packages",
script: "npm outdated --global"
sh label: "Test sonar-scanner",
script: "sonar-scanner --version"
sh label: "Test trufflehog",
script: "trufflehog --help"
}
}
}
}
stage("Push to registry") {
steps {
script {
// Use commit tag if it has been tagged
tag = sh(returnStdout: true, script: "git tag --contains").trim()
if ("$tag" == "") {
if ("${BRANCH_NAME}" == "master") {
tag = "latest"
} else {
tag = "${BRANCH_NAME}"
}
}
sh "docker tag $DOCKER_IMAGE $DOCKER_IMAGE:$tag"
// By specifying only the image name, all tags will automatically be pushed
sh "docker push $DOCKER_IMAGE"
}
}
}
}
post {
always {
archiveArtifacts artifacts: "*-results.txt"
}
}
}