coverity: allow running on macOS
[git.git] / .github / workflows / coverity.yml
blob53f9ee6a418b6585e90df7334f63f1deb57b7098
1 name: Coverity
3 # This GitHub workflow automates submitting builds to Coverity Scan. To enable it,
4 # set the repository variable `ENABLE_COVERITY_SCAN_FOR_BRANCHES` (for details, see
5 # https://docs.github.com/en/actions/learn-github-actions/variables) to a JSON
6 # string array containing the names of the branches for which the workflow should be
7 # run, e.g. `["main", "next"]`.
9 # In addition, two repository secrets must be set (for details how to add secrets, see
10 # https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions):
11 # `COVERITY_SCAN_EMAIL` and `COVERITY_SCAN_TOKEN`. The former specifies the
12 # email to which the Coverity reports should be sent and the latter can be
13 # obtained from the Project Settings tab of the Coverity project).
15 # The workflow runs on `ubuntu-latest` by default. This can be overridden by setting
16 # the repository variable `ENABLE_COVERITY_SCAN_ON_OS` to a JSON string array specifying
17 # the operating systems, e.g. `["ubuntu-latest", "windows-latest"]`.
19 # By default, the builds are submitted to the Coverity project `git`. To override this,
20 # set the repository variable `COVERITY_PROJECT`.
22 on:
23   push:
25 defaults:
26   run:
27     shell: bash
29 jobs:
30   coverity:
31     if: contains(fromJSON(vars.ENABLE_COVERITY_SCAN_FOR_BRANCHES || '[""]'), github.ref_name)
32     strategy:
33       matrix:
34         os: ${{ fromJSON(vars.ENABLE_COVERITY_SCAN_ON_OS || '["ubuntu-latest"]') }}
35     runs-on: ${{ matrix.os }}
36     env:
37       COVERITY_PROJECT: ${{ vars.COVERITY_PROJECT || 'git' }}
38       COVERITY_LANGUAGE: cxx
39       COVERITY_PLATFORM: overridden-below
40     steps:
41       - uses: actions/checkout@v3
42       - name: install minimal Git for Windows SDK
43         if: contains(matrix.os, 'windows')
44         uses: git-for-windows/setup-git-for-windows-sdk@v1
45       - run: ci/install-dependencies.sh
46         if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
47         env:
48           runs_on_pool: ${{ matrix.os }}
50       # The Coverity site says the tool is usually updated twice yearly, so the
51       # MD5 of download can be used to determine whether there's been an update.
52       - name: get the Coverity Build Tool hash
53         id: lookup
54         run: |
55           case "${{ matrix.os }}" in
56           *windows*)
57             COVERITY_PLATFORM=win64
58             COVERITY_TOOL_FILENAME=cov-analysis.zip
59             MAKEFLAGS=-j$(nproc)
60             ;;
61           *macos*)
62             COVERITY_PLATFORM=macOSX
63             COVERITY_TOOL_FILENAME=cov-analysis.dmg
64             MAKEFLAGS=-j$(sysctl -n hw.physicalcpu)
65             ;;
66           *ubuntu*)
67             COVERITY_PLATFORM=linux64
68             COVERITY_TOOL_FILENAME=cov-analysis.tgz
69             MAKEFLAGS=-j$(nproc)
70             ;;
71           *)
72             echo '::error::unhandled OS ${{ matrix.os }}' >&2
73             exit 1
74             ;;
75           esac
76           echo "COVERITY_PLATFORM=$COVERITY_PLATFORM" >>$GITHUB_ENV
77           echo "COVERITY_TOOL_FILENAME=$COVERITY_TOOL_FILENAME" >>$GITHUB_ENV
78           echo "MAKEFLAGS=$MAKEFLAGS" >>$GITHUB_ENV
79           MD5=$(curl https://scan.coverity.com/download/$COVERITY_LANGUAGE/$COVERITY_PLATFORM \
80                    --fail \
81                    --form token='${{ secrets.COVERITY_SCAN_TOKEN }}' \
82                    --form project="$COVERITY_PROJECT" \
83                    --form md5=1) &&
84           echo "hash=$MD5" >>$GITHUB_OUTPUT
86       # Try to cache the tool to avoid downloading 1GB+ on every run.
87       # A cache miss will add ~30s to create, but a cache hit will save minutes.
88       - name: restore the Coverity Build Tool
89         id: cache
90         uses: actions/cache/restore@v3
91         with:
92           path: ${{ runner.temp }}/cov-analysis
93           key: cov-build-${{ env.COVERITY_LANGUAGE }}-${{ env.COVERITY_PLATFORM }}-${{ steps.lookup.outputs.hash }}
94       - name: download the Coverity Build Tool (${{ env.COVERITY_LANGUAGE }} / ${{ env.COVERITY_PLATFORM}})
95         if: steps.cache.outputs.cache-hit != 'true'
96         run: |
97           curl https://scan.coverity.com/download/$COVERITY_LANGUAGE/$COVERITY_PLATFORM \
98             --fail --no-progress-meter \
99             --output $RUNNER_TEMP/$COVERITY_TOOL_FILENAME \
100             --form token='${{ secrets.COVERITY_SCAN_TOKEN }}' \
101             --form project="$COVERITY_PROJECT"
102       - name: extract the Coverity Build Tool
103         if: steps.cache.outputs.cache-hit != 'true'
104         run: |
105           case "$COVERITY_TOOL_FILENAME" in
106           *.tgz)
107             mkdir $RUNNER_TEMP/cov-analysis &&
108             tar -xzf $RUNNER_TEMP/$COVERITY_TOOL_FILENAME --strip 1 -C $RUNNER_TEMP/cov-analysis
109             ;;
110           *.dmg)
111             cd $RUNNER_TEMP &&
112             attach="$(hdiutil attach $COVERITY_TOOL_FILENAME)" &&
113             volume="$(echo "$attach" | cut -f 3 | grep /Volumes/)" &&
114             mkdir cov-analysis &&
115             cd cov-analysis &&
116             sh "$volume"/cov-analysis-macosx-*.sh &&
117             ls -l &&
118             hdiutil detach "$volume"
119             ;;
120           *.zip)
121             cd $RUNNER_TEMP &&
122             mkdir cov-analysis-tmp &&
123             unzip -d cov-analysis-tmp $COVERITY_TOOL_FILENAME &&
124             mv cov-analysis-tmp/* cov-analysis
125             ;;
126           *)
127             echo "::error::unhandled archive type: $COVERITY_TOOL_FILENAME" >&2
128             exit 1
129             ;;
130           esac
131       - name: cache the Coverity Build Tool
132         if: steps.cache.outputs.cache-hit != 'true'
133         uses: actions/cache/save@v3
134         with:
135           path: ${{ runner.temp }}/cov-analysis
136           key: cov-build-${{ env.COVERITY_LANGUAGE }}-${{ env.COVERITY_PLATFORM }}-${{ steps.lookup.outputs.hash }}
137       - name: build with cov-build
138         run: |
139           export PATH="$RUNNER_TEMP/cov-analysis/bin:$PATH" &&
140           cov-configure --gcc &&
141           cov-build --dir cov-int make
142       - name: package the build
143         run: tar -czvf cov-int.tgz cov-int
144       - name: submit the build to Coverity Scan
145         run: |
146           curl \
147             --fail \
148             --form token='${{ secrets.COVERITY_SCAN_TOKEN }}' \
149             --form email='${{ secrets.COVERITY_SCAN_EMAIL }}' \
150             --form file=@cov-int.tgz \
151             --form version='${{ github.sha }}' \
152             "https://scan.coverity.com/builds?project=$COVERITY_PROJECT"