Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 1 | // 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 | |
| 16 | pipeline { |
| 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 Williams | a59b29c | 2019-10-10 09:54:01 -0700 | [diff] [blame] | 30 | timeout(15) |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | stages { |
| 34 | |
| 35 | stage ("Clean workspace") { |
| 36 | steps { |
| 37 | sh 'rm -rf *' |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | stage('Checkout') { |
| 42 | steps { |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 43 | 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 Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 54 | |
Zack Williams | a59b29c | 2019-10-10 09:54:01 -0700 | [diff] [blame] | 55 | // Used later to set the branch/tag |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 56 | 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 Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 71 | #!/usr/bin/env bash |
| 72 | set -eu -o pipefail |
| 73 | |
| 74 | |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 75 | 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 Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 89 | fi |
| 90 | """ |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | stage ("fossa") { |
| 95 | 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 Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 99 | 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 Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 103 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 104 | echo "Creating debug output directory" |
| 105 | mkdir -p "$WORKSPACE/debug" |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 106 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 107 | 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 Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 111 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 112 | echo "Fossa version:" |
| 113 | fossa -v |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 114 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 115 | echo "Testing project: $projectName located in $test_path" |
| 116 | pushd "$test_path" |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 117 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 118 | echo "Run 'fossa init'" |
| 119 | fossa init --no-ansi --verbose |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 120 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 121 | echo "Contents of .fossa.yml generated by 'fossa init':" |
| 122 | cat .fossa.yml |
Zack Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 123 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 124 | 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 Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 131 | |
Zack Williams | 3fafbb7 | 2019-10-12 11:47:09 -0700 | [diff] [blame] | 132 | 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 Williams | 96e1146 | 2019-10-10 09:04:47 -0700 | [diff] [blame] | 143 | } |
| 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 | |