blob: ebf77691c69b931198bd64f0c90af424a7b371a1 [file] [log] [blame]
Zack Williams96e11462019-10-10 09:04:47 -07001// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13// fossa-verify.groovy
14// checks a single repo against fossa
15
16pipeline {
17
18 /* no label, executor is determined by JJB */
19 agent {
20 label "${params.executorNode}"
21 }
22
23 // Set so that fossa will know where to run golang tools from
24 environment {
25 PATH = "$PATH:/usr/lib/go-1.12/bin:/usr/local/go/bin/:$WORKSPACE/go/bin"
26 GOPATH = "$WORKSPACE/go"
27 }
28
29 options {
Zack Williamsa59b29c2019-10-10 09:54:01 -070030 timeout(15)
Zack Williams96e11462019-10-10 09:04:47 -070031 }
32
33 stages {
34
35 stage ("Clean workspace") {
36 steps {
37 sh 'rm -rf *'
38 }
39 }
40
41 stage('Checkout') {
42 steps {
43 checkout([
44 $class: 'GitSCM',
45 userRemoteConfigs: [[ url: "${params.gitUrl}", ]],
46 branches: [[ name: "${params.gitRef}", ]],
47 extensions: [
48 [$class: 'WipeWorkspace'],
49 [$class: 'RelativeTargetDirectory', relativeTargetDir: "${params.projectName}"],
50 [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false],
51 ],
52 ])
53
Zack Williamsa59b29c2019-10-10 09:54:01 -070054 // Used later to set the branch/tag
Zack Williams96e11462019-10-10 09:04:47 -070055 script {
56 git_tag_or_branch = sh(script:"cd $projectName; if [[ \$(git tag -l --points-at HEAD) ]]; then git tag -l --points-at HEAD; else echo ${branchName}; fi", returnStdout: true).trim()
57 }
58 }
59 }
60
61 stage ("Prepare") {
62 steps {
63
64 // change the path tested if for golang projects which expect to be found in GOPATH
65 script {
66 test_path = sh(script:"if [ -f \"$projectName/Gopkg.toml\" ] || [ -f \"$projectName/go.mod\" ] ; then echo $WORKSPACE/go/src/github.com/opencord/$projectName; else echo $projectName; fi", returnStdout: true).trim()
67 }
68
69 sh returnStdout: true, script: """
70 if [ -f "$projectName/package.json" ]
71 then
72 echo "Found '$projectName/package.json', assuming a Node.js project, running npm install"
73 pushd "$projectName"
74 npm install
75 popd
76 elif [ -f "$projectName/Gopkg.toml" ]
77 then
78 echo "Found '$projectName/Gopkg.toml', assuming a golang project using dep"
79 mkdir -p "\$GOPATH/src/github.com/opencord/"
80 mv "$WORKSPACE/$projectName" "$test_path"
81 pushd "$test_path"
82 dep ensure
83 popd
84 elif [ -f "$projectName/go.mod" ]
85 then
86 echo "Found '$projectName/go.mod', assuming a golang project using go modules"
87 mkdir -p "\$GOPATH/src/github.com/opencord/"
88 mv "$WORKSPACE/$projectName" "$test_path"
89 pushd "$test_path"
90 make dep
91 popd
92 fi
93 """
94 }
95 }
96
97 stage ("fossa") {
98 steps {
99 // catch any errors that occur so that logs can be saved in the next stage
100 // docs: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#catcherror-catch-error-and-set-build-result-to-failure
101 catchError {
102
103 sh returnStdout: true, script: """
104 echo "Creating debug output directory"
105 mkdir -p "$WORKSPACE/debug"
106
107 echo "Download/install fossa tool"
108 curl -H 'Cache-Control: no-cache' -O https://raw.githubusercontent.com/fossas/fossa-cli/master/install.sh
109 mkdir -p "$WORKSPACE/go/bin"
110 bash install.sh -b "$WORKSPACE/go/bin"
111
112 echo "Fossa version:"
113 fossa -v
114
115 echo "Testing project: $projectName located in $test_path"
116 pushd "$test_path"
117
118 echo "Fossa init"
119 fossa init --no-ansi --debug 2>"$WORKSPACE/debug/init_stderr.log"
120
121 echo "Fossa analyze"
122 fossa analyze --no-ansi --debug \
123 -T "$fossaProject"
124 -t "$projectName" \
125 -L "$GERRIT_CHANGE_URL" \
126 -b "$git_tag_or_branch" \
127 -r "$GERRIT_CHANGE_NUMBER.$GERRIT_PATCHSET_NUMBER" \
128 2>"$WORKSPACE/debug/analyze_stderr.log"
129
130 echo "Fossa test results"
131 fossa test --no-ansi --debug \
132 -T "$fossaProject"
133 -t "$projectName" \
134 -L "$GERRIT_CHANGE_URL" \
135 -b "$git_tag_or_branch" \
136 -r "$GERRIT_CHANGE_NUMBER.$GERRIT_PATCHSET_NUMBER" \
137 2>"$WORKSPACE/debug/test_stderr.log"
138
139 popd
140 """
141 }
142 }
143 }
144
145 stage ("Save Logs") {
146 steps {
147 sh returnStdout: true, script: """
148 echo "Logs:"
149 ls -l $WORKSPACE/debug
150 """
151 archiveArtifacts artifacts:'debug/*.log'
152 }
153 }
154 }
155}
156