blob: da5c0e902c631843a24b8f6327bcad3102316e22 [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 {
Zack Williamsb3292082019-10-11 17:15:18 -070020 label "${buildNode}"
Zack Williams96e11462019-10-10 09:04:47 -070021 }
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 {
Zack Williams3fafbb72019-10-12 11:47:09 -070043 sh returnStdout: true, script: """
44 #!/usr/bin/env bash
45 set -eu -o pipefail
46
47 echo "Cloning repo: ${gitUrl}"
48 git clone "${gitUrl}"
49 pushd "$projectName"
50 echo "Checking out Gerrit patchset: ${GERRIT_REFSPEC}"
51 git fetch ${gitUrl} ${GERRIT_REFSPEC} && git checkout FETCH_HEAD
52 popd
53 """
Zack Williams96e11462019-10-10 09:04:47 -070054
Zack Williamsa59b29c2019-10-10 09:54:01 -070055 // Used later to set the branch/tag
Zack Williams96e11462019-10-10 09:04:47 -070056 script {
57 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()
58 }
59 }
60 }
61
62 stage ("Prepare") {
63 steps {
64
65 // change the path tested if for golang projects which expect to be found in GOPATH
66 script {
67 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()
68 }
69
70 sh returnStdout: true, script: """
Zack Williams3fafbb72019-10-12 11:47:09 -070071 #!/usr/bin/env bash
72 set -eu -o pipefail
73
74
Zack Williams96e11462019-10-10 09:04:47 -070075 if [ -f "$projectName/package.json" ]
76 then
77 echo "Found '$projectName/package.json', assuming a Node.js project, running npm install"
78 pushd "$projectName"
79 npm install
80 popd
81 elif [ -f "$projectName/Gopkg.toml" ]
82 then
83 echo "Found '$projectName/Gopkg.toml', assuming a golang project using dep"
84 mkdir -p "\$GOPATH/src/github.com/opencord/"
85 mv "$WORKSPACE/$projectName" "$test_path"
86 pushd "$test_path"
87 dep ensure
88 popd
Zack Williams96e11462019-10-10 09:04:47 -070089 fi
90 """
91 }
92 }
93
Zack Williamsb3292082019-10-11 17:15:18 -070094 stage ("Run FOSSA") {
Zack Williams96e11462019-10-10 09:04:47 -070095 steps {
96 // catch any errors that occur so that logs can be saved in the next stage
97 // docs: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#catcherror-catch-error-and-set-build-result-to-failure
98 catchError {
Zack Williams3fafbb72019-10-12 11:47:09 -070099 withCredentials([string(credentialsId: 'fossa-api-key', variable: 'FOSSA_API_KEY')]) {
100 sh returnStdout: true, script: """
101 #!/usr/bin/env bash
102 set -eu -o pipefail
Zack Williams96e11462019-10-10 09:04:47 -0700103
Zack Williams3fafbb72019-10-12 11:47:09 -0700104 echo "Creating debug output directory"
105 mkdir -p "$WORKSPACE/debug"
Zack Williams96e11462019-10-10 09:04:47 -0700106
Zack Williams3fafbb72019-10-12 11:47:09 -0700107 echo "Download/install fossa CLI 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"
Zack Williams96e11462019-10-10 09:04:47 -0700111
Zack Williams3fafbb72019-10-12 11:47:09 -0700112 echo "Fossa version:"
113 fossa -v
Zack Williams96e11462019-10-10 09:04:47 -0700114
Zack Williams3fafbb72019-10-12 11:47:09 -0700115 echo "Testing project: $projectName located in $test_path"
116 pushd "$test_path"
Zack Williams96e11462019-10-10 09:04:47 -0700117
Zack Williams3fafbb72019-10-12 11:47:09 -0700118 echo "Run 'fossa init'"
119 fossa init --no-ansi --verbose
Zack Williams96e11462019-10-10 09:04:47 -0700120
Zack Williams3fafbb72019-10-12 11:47:09 -0700121 echo "Contents of .fossa.yml generated by 'fossa init':"
122 cat .fossa.yml
Zack Williams96e11462019-10-10 09:04:47 -0700123
Zack Williams3fafbb72019-10-12 11:47:09 -0700124 echo "Run 'fossa analyze'"
125 fossa analyze --no-ansi --verbose \
126 -T "$fossaTeam" \
127 -t "$projectName" \
128 -L "$GERRIT_CHANGE_URL" \
129 -b "$git_tag_or_branch" \
130 -r "$GERRIT_CHANGE_NUMBER/$GERRIT_PATCHSET_NUMBER"
Zack Williams96e11462019-10-10 09:04:47 -0700131
Zack Williams3fafbb72019-10-12 11:47:09 -0700132 echo "Get FOSSA test results with 'fossa test'"
133 fossa test --no-ansi --verbose \
134 -T "$fossaTeam" \
135 -t "$projectName" \
136 -L "$GERRIT_CHANGE_URL" \
137 -b "$git_tag_or_branch" \
138 -r "$GERRIT_CHANGE_NUMBER/$GERRIT_PATCHSET_NUMBER"
139
140 popd
141 """
142 }
Zack Williams96e11462019-10-10 09:04:47 -0700143 }
144 }
145 }
146
147 stage ("Save Logs") {
148 steps {
149 sh returnStdout: true, script: """
150 echo "Logs:"
151 ls -l $WORKSPACE/debug
152 """
153 archiveArtifacts artifacts:'debug/*.log'
154 }
155 }
156 }
157}
158