Skip to content

Commit ce4ee80

Browse files
authored
Add support to "Check License" for checking license files in multiple paths
In cases where a project contains distinct components in subfolders, multiple license files might be present. Previously the "Check License" workflow only supported checking the license file in the root of the repository. Support for validating an arbitrary number of license files with arbitary locations, types, and filenames is added. A job matrix is used to provide this support in a manner that makes application-specific configuration of the workflow easy and without code duplication.
1 parent 29d3918 commit ce4ee80

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

.github/workflows/check-license.yml

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-license.md
22
name: Check License
33

4-
env:
5-
# TODO: Define the project's license file name here:
6-
EXPECTED_LICENSE_FILENAME: LICENSE.txt
7-
# SPDX identifier: https://spdx.org/licenses/
8-
EXPECTED_LICENSE_TYPE: CC0-1.0
9-
104
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
115
on:
126
create:
@@ -36,9 +30,9 @@ on:
3630
jobs:
3731
run-determination:
3832
runs-on: ubuntu-latest
33+
permissions: {}
3934
outputs:
4035
result: ${{ steps.determination.outputs.result }}
41-
permissions: {}
4236
steps:
4337
- name: Determine if the rest of the workflow should run
4438
id: determination
@@ -56,15 +50,29 @@ jobs:
5650
RESULT="false"
5751
fi
5852
59-
echo "::set-output name=result::$RESULT"
53+
echo "result=$RESULT" >> $GITHUB_OUTPUT
6054
6155
check-license:
56+
name: ${{ matrix.check-license.path }}
6257
needs: run-determination
6358
if: needs.run-determination.outputs.result == 'true'
6459
runs-on: ubuntu-latest
6560
permissions:
6661
contents: read
6762

63+
strategy:
64+
fail-fast: false
65+
66+
matrix:
67+
check-license:
68+
# TODO: Add additional paths where license files should be
69+
- path: ./
70+
# TODO: Define the project's license file name here:
71+
expected-filename: LICENSE.txt
72+
# SPDX identifier: https://spdx.org/licenses/
73+
# TODO: Define the project's license type here
74+
expected-type: CC0-1.0
75+
6876
steps:
6977
- name: Checkout repository
7078
uses: actions/checkout@v4
@@ -77,23 +85,27 @@ jobs:
7785
- name: Install licensee
7886
run: gem install licensee
7987

80-
- name: Check license file
88+
- name: Check license file for ${{ matrix.check-license.path }}
8189
run: |
8290
EXIT_STATUS=0
91+
92+
# Go into folder path
93+
cd ./${{ matrix.check-license.path }}
94+
8395
# See: https://github.com/licensee/licensee
8496
LICENSEE_OUTPUT="$(licensee detect --json --confidence=100)"
8597
8698
DETECTED_LICENSE_FILE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].filename | tr --delete '\r')"
8799
echo "Detected license file: $DETECTED_LICENSE_FILE"
88-
if [ "$DETECTED_LICENSE_FILE" != "\"${EXPECTED_LICENSE_FILENAME}\"" ]; then
89-
echo "::error file=${DETECTED_LICENSE_FILE}::detected license file $DETECTED_LICENSE_FILE doesn't match expected: $EXPECTED_LICENSE_FILENAME"
100+
if [ "$DETECTED_LICENSE_FILE" != "\"${{ matrix.check-license.expected-filename }}\"" ]; then
101+
echo "::error file=${DETECTED_LICENSE_FILE}::detected license file $DETECTED_LICENSE_FILE doesn't match expected: ${{ matrix.check-license.expected-filename }}"
90102
EXIT_STATUS=1
91103
fi
92104
93105
DETECTED_LICENSE_TYPE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].matched_license | tr --delete '\r')"
94106
echo "Detected license type: $DETECTED_LICENSE_TYPE"
95-
if [ "$DETECTED_LICENSE_TYPE" != "\"${EXPECTED_LICENSE_TYPE}\"" ]; then
96-
echo "::error file=${DETECTED_LICENSE_FILE}::detected license type $DETECTED_LICENSE_TYPE doesn't match expected \"${EXPECTED_LICENSE_TYPE}\""
107+
if [ "$DETECTED_LICENSE_TYPE" != "\"${{ matrix.check-license.expected-type }}\"" ]; then
108+
echo "::error file=${DETECTED_LICENSE_FILE}::detected license type $DETECTED_LICENSE_TYPE doesn't match expected \"${{ matrix.check-license.expected-type }}\""
97109
EXIT_STATUS=1
98110
fi
99111

workflow-templates/check-license.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Install the [`check-license.yml`](check-license.yml) GitHub Actions workflow to
1414

1515
- Configure the license filename in the `env.EXPECTED_LICENSE_FILENAME` field of `check-license.yml`.
1616
- Configure the license type in the `env.EXPECTED_LICENSE_TYPE` field of `check-license.yml`.
17+
- (Optional) If license files should be present outside the root directory, add the configuration for additional paths to the `jobs.check-license.strategy.matrix.licenses` array in `check-license.yml`.
1718

1819
### Readme badge
1920

workflow-templates/check-license.yml

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-license.md
22
name: Check License
33

4-
env:
5-
# TODO: Define the project's license file name here:
6-
EXPECTED_LICENSE_FILENAME: LICENSE.txt
7-
# SPDX identifier: https://spdx.org/licenses/
8-
# TODO: Define the project's license type here
9-
EXPECTED_LICENSE_TYPE: AGPL-3.0
10-
114
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
125
on:
136
create:
@@ -60,12 +53,26 @@ jobs:
6053
echo "result=$RESULT" >> $GITHUB_OUTPUT
6154
6255
check-license:
56+
name: ${{ matrix.check-license.path }}
6357
needs: run-determination
6458
if: needs.run-determination.outputs.result == 'true'
6559
runs-on: ubuntu-latest
6660
permissions:
6761
contents: read
6862

63+
strategy:
64+
fail-fast: false
65+
66+
matrix:
67+
check-license:
68+
# TODO: Add additional paths where license files should be
69+
- path: ./
70+
# TODO: Define the project's license file name here:
71+
expected-filename: LICENSE.txt
72+
# SPDX identifier: https://spdx.org/licenses/
73+
# TODO: Define the project's license type here
74+
expected-type: AGPL-3.0
75+
6976
steps:
7077
- name: Checkout repository
7178
uses: actions/checkout@v4
@@ -78,23 +85,27 @@ jobs:
7885
- name: Install licensee
7986
run: gem install licensee
8087

81-
- name: Check license file
88+
- name: Check license file for ${{ matrix.check-license.path }}
8289
run: |
8390
EXIT_STATUS=0
91+
92+
# Go into folder path
93+
cd ./${{ matrix.check-license.path }}
94+
8495
# See: https://github.com/licensee/licensee
8596
LICENSEE_OUTPUT="$(licensee detect --json --confidence=100)"
8697
8798
DETECTED_LICENSE_FILE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].filename | tr --delete '\r')"
8899
echo "Detected license file: $DETECTED_LICENSE_FILE"
89-
if [ "$DETECTED_LICENSE_FILE" != "\"${EXPECTED_LICENSE_FILENAME}\"" ]; then
90-
echo "::error file=${DETECTED_LICENSE_FILE}::detected license file $DETECTED_LICENSE_FILE doesn't match expected: $EXPECTED_LICENSE_FILENAME"
100+
if [ "$DETECTED_LICENSE_FILE" != "\"${{ matrix.check-license.expected-filename }}\"" ]; then
101+
echo "::error file=${DETECTED_LICENSE_FILE}::detected license file $DETECTED_LICENSE_FILE doesn't match expected: ${{ matrix.check-license.expected-filename }}"
91102
EXIT_STATUS=1
92103
fi
93104
94105
DETECTED_LICENSE_TYPE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].matched_license | tr --delete '\r')"
95106
echo "Detected license type: $DETECTED_LICENSE_TYPE"
96-
if [ "$DETECTED_LICENSE_TYPE" != "\"${EXPECTED_LICENSE_TYPE}\"" ]; then
97-
echo "::error file=${DETECTED_LICENSE_FILE}::detected license type $DETECTED_LICENSE_TYPE doesn't match expected \"${EXPECTED_LICENSE_TYPE}\""
107+
if [ "$DETECTED_LICENSE_TYPE" != "\"${{ matrix.check-license.expected-type }}\"" ]; then
108+
echo "::error file=${DETECTED_LICENSE_FILE}::detected license type $DETECTED_LICENSE_TYPE doesn't match expected \"${{ matrix.check-license.expected-type }}\""
98109
EXIT_STATUS=1
99110
fi
100111

0 commit comments

Comments
 (0)