|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: Tests |
| 16 | + |
| 17 | +on: |
| 18 | + push: |
| 19 | + branches: ["main", "v1", "mcp"] |
| 20 | + paths-ignore: |
| 21 | + - '**/*.md' |
| 22 | + - 'docs/**' |
| 23 | + - '.github/workflows/*' |
| 24 | + - '!.github/workflows/tests.yml' |
| 25 | + - '!.github/workflows/test-action/**' |
| 26 | + - 'scripts/**' |
| 27 | + pull_request: |
| 28 | + # The branches below must be a subset of the branches above |
| 29 | + branches: ["main", "v1", "mcp"] |
| 30 | + paths-ignore: |
| 31 | + - '**/*.md' |
| 32 | + - 'docs/**' |
| 33 | + - '.github/workflows/*' |
| 34 | + - '!.github/workflows/tests.yml' |
| 35 | + - '!.github/workflows/test-action/**' |
| 36 | + - 'scripts/**' |
| 37 | + workflow_dispatch: |
| 38 | + |
| 39 | +concurrency: |
| 40 | + # Pushing new changes to a branch will cancel any in-progress CI runs |
| 41 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 42 | + cancel-in-progress: true |
| 43 | + |
| 44 | +# Restrict jobs in this workflow to have no permissions by default; permissions |
| 45 | +# should be granted per job as needed using a dedicated `permissions` block |
| 46 | +permissions: {} |
| 47 | + |
| 48 | +jobs: |
| 49 | + prepare_test_image_testdata: |
| 50 | + permissions: |
| 51 | + contents: read # to fetch code (actions/checkout) |
| 52 | + runs-on: ubuntu-latest |
| 53 | + steps: |
| 54 | + - name: Check out code |
| 55 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 56 | + with: |
| 57 | + persist-credentials: false |
| 58 | + - run: scripts/build_test_images.sh |
| 59 | + - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 |
| 60 | + with: |
| 61 | + name: image-testdata-${{ github.run_number }}-${{ github.run_attempt }} |
| 62 | + path: cmd/osv-scanner/scan/image/testdata/*.tar |
| 63 | + retention-days: 1 |
| 64 | + tests: |
| 65 | + permissions: |
| 66 | + contents: read # to fetch code (actions/checkout) |
| 67 | + needs: |
| 68 | + - prepare_test_image_testdata |
| 69 | + name: Run unit tests |
| 70 | + strategy: |
| 71 | + fail-fast: false |
| 72 | + matrix: |
| 73 | + os: [ubuntu-latest, macos-latest, windows-latest] |
| 74 | + runs-on: ${{ matrix.os }} |
| 75 | + steps: |
| 76 | + - name: Check out code |
| 77 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 78 | + with: |
| 79 | + persist-credentials: false |
| 80 | + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 81 | + with: |
| 82 | + pattern: image-testdata-${{ github.run_number }}-* |
| 83 | + path: cmd/osv-scanner/scan/image/testdata/ |
| 84 | + - name: Set up Go |
| 85 | + uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0 |
| 86 | + with: |
| 87 | + go-version-file: "go.mod" |
| 88 | + check-latest: true |
| 89 | + - name: Run test action |
| 90 | + uses: ./.github/workflows/test-action |
| 91 | + with: |
| 92 | + codecov_token: ${{ secrets.CODECOV_TOKEN }} |
| 93 | + docker: |
| 94 | + permissions: |
| 95 | + contents: read # to fetch code (actions/checkout) |
| 96 | + runs-on: ubuntu-latest |
| 97 | + env: |
| 98 | + # Required for buildx on docker 19.x |
| 99 | + DOCKER_CLI_EXPERIMENTAL: "enabled" |
| 100 | + steps: |
| 101 | + - name: Checkout |
| 102 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 103 | + with: |
| 104 | + fetch-depth: 0 |
| 105 | + - name: Set up Go |
| 106 | + uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0 |
| 107 | + with: |
| 108 | + go-version-file: "go.mod" |
| 109 | + check-latest: true |
| 110 | + - uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0 |
| 111 | + - uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0 |
| 112 | + - name: Run GoReleaser |
| 113 | + id: run-goreleaser |
| 114 | + uses: goreleaser/goreleaser-action@ec59f474b9834571250b370d4735c50f8e2d1e29 # v7.0.0 |
| 115 | + with: |
| 116 | + distribution: goreleaser |
| 117 | + version: "~> v2" |
| 118 | + args: release --clean --snapshot |
| 119 | + - env: |
| 120 | + ARTIFACTS: ${{ steps.run-goreleaser.outputs.artifacts }} |
| 121 | + run: | |
| 122 | + echo "$ARTIFACTS" > output.json |
| 123 | + jq -r '.[] | select( |
| 124 | + .type == "Docker Image" and |
| 125 | + .goarch == "amd64" and |
| 126 | + .goos == "linux" and |
| 127 | + .extra.DockerConfig.dockerfile == "goreleaser.dockerfile" |
| 128 | + ) | .name' output.json | while read -r image; do |
| 129 | + echo "Testing image $image" |
| 130 | +
|
| 131 | + exit_code=0 |
| 132 | + docker run -v ${PWD}:/src $image -L /src/go.mod || exit_code=$? |
| 133 | +
|
| 134 | + # fail if we get a non-zero exit code other than "vulnerabilities were found" |
| 135 | + if [[ $exit_code -ne 0 && $exit_code -ne 1 ]]; then |
| 136 | + exit $exit_code |
| 137 | + fi |
| 138 | + done |
0 commit comments