|  | 
|  | 1 | +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-license.md | 
|  | 2 | +name: Check License | 
|  | 3 | + | 
|  | 4 | +# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows | 
|  | 5 | +on: | 
|  | 6 | +  create: | 
|  | 7 | +  push: | 
|  | 8 | +    paths: | 
|  | 9 | +      - ".github/workflows/check-license.ya?ml" | 
|  | 10 | +      # See: https://github.com/licensee/licensee/blob/master/docs/what-we-look-at.md#detecting-the-license-file | 
|  | 11 | +      - "[cC][oO][pP][yY][iI][nN][gG]*" | 
|  | 12 | +      - "[cC][oO][pP][yY][rR][iI][gG][hH][tH]*" | 
|  | 13 | +      - "[lL][iI][cC][eE][nN][cCsS][eE]*" | 
|  | 14 | +      - "[oO][fF][lL]*" | 
|  | 15 | +      - "[pP][aA][tT][eE][nN][tT][sS]*" | 
|  | 16 | +  pull_request: | 
|  | 17 | +    paths: | 
|  | 18 | +      - ".github/workflows/check-license.ya?ml" | 
|  | 19 | +      - "[cC][oO][pP][yY][iI][nN][gG]*" | 
|  | 20 | +      - "[cC][oO][pP][yY][rR][iI][gG][hH][tH]*" | 
|  | 21 | +      - "[lL][iI][cC][eE][nN][cCsS][eE]*" | 
|  | 22 | +      - "[oO][fF][lL]*" | 
|  | 23 | +      - "[pP][aA][tT][eE][nN][tT][sS]*" | 
|  | 24 | +  schedule: | 
|  | 25 | +    # Run periodically to catch breakage caused by external changes. | 
|  | 26 | +    - cron: "0 6 * * WED" | 
|  | 27 | +  workflow_dispatch: | 
|  | 28 | +  repository_dispatch: | 
|  | 29 | + | 
|  | 30 | +jobs: | 
|  | 31 | +  run-determination: | 
|  | 32 | +    runs-on: ubuntu-latest | 
|  | 33 | +    permissions: {} | 
|  | 34 | +    outputs: | 
|  | 35 | +      result: ${{ steps.determination.outputs.result }} | 
|  | 36 | +    steps: | 
|  | 37 | +      - name: Determine if the rest of the workflow should run | 
|  | 38 | +        id: determination | 
|  | 39 | +        run: | | 
|  | 40 | +          RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x" | 
|  | 41 | +          # The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead. | 
|  | 42 | +          if [[ | 
|  | 43 | +            "${{ github.event_name }}" != "create" || | 
|  | 44 | +            "${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX | 
|  | 45 | +          ]]; then | 
|  | 46 | +            # Run the other jobs. | 
|  | 47 | +            RESULT="true" | 
|  | 48 | +          else | 
|  | 49 | +            # There is no need to run the other jobs. | 
|  | 50 | +            RESULT="false" | 
|  | 51 | +          fi | 
|  | 52 | +
 | 
|  | 53 | +          echo "result=$RESULT" >> $GITHUB_OUTPUT | 
|  | 54 | +
 | 
|  | 55 | +  check-license: | 
|  | 56 | +    name: ${{ matrix.check-license.path }} | 
|  | 57 | +    needs: run-determination | 
|  | 58 | +    if: needs.run-determination.outputs.result == 'true' | 
|  | 59 | +    runs-on: ubuntu-latest | 
|  | 60 | +    permissions: | 
|  | 61 | +      contents: read | 
|  | 62 | + | 
|  | 63 | +    strategy: | 
|  | 64 | +      fail-fast: false | 
|  | 65 | + | 
|  | 66 | +      matrix: | 
|  | 67 | +        check-license: | 
|  | 68 | +          - path: ./ | 
|  | 69 | +            # TODO: Define the project's license file name here: | 
|  | 70 | +            expected-filename: LICENSE.txt | 
|  | 71 | +            # SPDX identifier: https://spdx.org/licenses/ | 
|  | 72 | +            expected-type: Apache-2.0 | 
|  | 73 | + | 
|  | 74 | +    steps: | 
|  | 75 | +      - name: Checkout repository | 
|  | 76 | +        uses: actions/checkout@v4 | 
|  | 77 | + | 
|  | 78 | +      - name: Install Ruby | 
|  | 79 | +        uses: ruby/setup-ruby@v1 | 
|  | 80 | +        with: | 
|  | 81 | +          ruby-version: ruby # Install latest version | 
|  | 82 | + | 
|  | 83 | +      - name: Install licensee | 
|  | 84 | +        run: gem install licensee | 
|  | 85 | + | 
|  | 86 | +      - name: Check license file for ${{ matrix.check-license.path }} | 
|  | 87 | +        run: | | 
|  | 88 | +          EXIT_STATUS=0 | 
|  | 89 | +
 | 
|  | 90 | +          # Go into folder path | 
|  | 91 | +          cd ./${{ matrix.check-license.path }} | 
|  | 92 | +
 | 
|  | 93 | +          # See: https://github.com/licensee/licensee | 
|  | 94 | +          LICENSEE_OUTPUT="$(licensee detect --json --confidence=100)" | 
|  | 95 | +
 | 
|  | 96 | +          DETECTED_LICENSE_FILE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].filename | tr --delete '\r')" | 
|  | 97 | +          echo "Detected license file: $DETECTED_LICENSE_FILE" | 
|  | 98 | +          if [ "$DETECTED_LICENSE_FILE" != "\"${{ matrix.check-license.expected-filename }}\"" ]; then | 
|  | 99 | +            echo "::error file=${DETECTED_LICENSE_FILE}::detected license file $DETECTED_LICENSE_FILE doesn't match expected: ${{ matrix.check-license.expected-filename }}" | 
|  | 100 | +            EXIT_STATUS=1 | 
|  | 101 | +          fi | 
|  | 102 | +
 | 
|  | 103 | +          DETECTED_LICENSE_TYPE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].matched_license | tr --delete '\r')" | 
|  | 104 | +          echo "Detected license type: $DETECTED_LICENSE_TYPE" | 
|  | 105 | +          if [ "$DETECTED_LICENSE_TYPE" != "\"${{ matrix.check-license.expected-type }}\"" ]; then | 
|  | 106 | +            echo "::error file=${DETECTED_LICENSE_FILE}::detected license type $DETECTED_LICENSE_TYPE doesn't match expected \"${{ matrix.check-license.expected-type }}\"" | 
|  | 107 | +            EXIT_STATUS=1 | 
|  | 108 | +          fi | 
|  | 109 | +
 | 
|  | 110 | +          exit $EXIT_STATUS | 
0 commit comments