GitLab CI / CD 파이프 라인의 반응 프로젝트에 설치된 패키지의 라이선스 검사를 구현하려고합니다. 빌드 단계 전에 파이프 라인은 사용 가능한 모든 라이선스를 확인한 다음 지정된 특정 라이선스를 화이트리스트 또는 블랙리스트에 추가해야합니다.
내가 사용하고 라이센스 검사기 패키지를 JSON 파일에서 사용 가능한 라이센스 목록을 구현합니다. 필수 명령을 실행 한 후 license-checker --json > ./license.json
출력은 다음과 같습니다.
license.json
{
"@babel/[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/babel/babel",
"path": "..../node_modules/@babel/plugin-transform-parameters",
"licenseFile": "...../node_modules/@babel/plugin-transform-parameters/LICENSE"
},
"@babel/[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/babel/babel",
"path": "..../node_modules/@babel/plugin-transform-property-literals",
"licenseFile": "...../node_modules/@babel/plugin-transform-property-literals/LICENSE"
},
"@babel/[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/babel/babel",
"path": "..../node_modules/@babel/plugin-transform-react-constant-elements",
"licenseFile": "...../node_modules/@babel/plugin-transform-react-constant-elements/LICENSE"
}
// .........and list goes on
}
.gitlab-ci.yml
include:
- local: license-checker-config.yml
stages:
- dependency
dependency:
image: node:12
stage: dependency
script:
- npm ci
- echo "main file...."
license-checker-config.yml
before_script:
- ./license.json
- echo "Checking licenses..."
라이센스 스캔은 빌드 프로세스 전에 시작되어야하므로 before_script
. 에서 license-checker-config.yml
JSON 파일을 포함시킨 다음이를 반복하여 확인해야합니다. MIT와 같은 라이선스가 포함되어 있으면 빌드 단계 만 계속되어야합니다. 그렇지 않으면 빌드가 실패합니다.
현재 코드 설정으로 파이프 라인을 실행하고 오류가 발생했습니다.
Executing "step_script" stage of the job script
00:01
$ ./license.json
/bin/bash: line 99: ./license.json: Permission denied
ERROR: Job failed: exit code 1
license.json 파일이 동일한 루트 폴더에 있지만 권한이 거부 된 것으로 표시됩니다. 또한 yml 파일 내에서 JSON 파일 루핑을 구현 한 다음 필요한 것을 달성하는 방법을 알아낼 수 없습니다.
이 문제를 해결하는 데 도움을 주시면 감사하겠습니다.
package.json (앱의 소스 코드)에 validate_licenses.js 라는 이름으로 스크립트를 추가 할 수 있습니다.
"scripts": {
"start": "...",
"build": "...",
"validate_licenses": " node validate_licenses.js"
}
validate_licenses.js에 라이선스 유효성 검사 논리를 넣습니다.
마지막으로 파이프 라인의 어느 부분에서나 실행하십시오.
npm run validate_licenses
이것은 테스트 처럼 실패 하고 빌드 프로세스가 중단됩니다.
const { exec } = require("child_process");
const license_checker = require('license-checker')
var fs = require('fs');
exec("license-checker --json > ./license.json", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`); return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);return;
}
console.log(`json created`);
parseJsonLicenses();
});
function parseJsonLicenses(){
var licenses = JSON.parse(fs.readFileSync('/license.json', 'utf8'));
//iterate licenses and fail if exist one licence differet of MIT
for(var npmModule in licenses){
if(licenses[npmModule].licenses != 'MIT')
throw new Error(npmModule+' has a not allowed license');
}
}
공식 사이트에서 더 많은 정보 :
var checker = require('license-checker');
var config = {
start: '.' ,
onlyAllow: 'MIT'
};
checker.init(config, function(json, err) {
if (err) {
throw new Error(err);
} else {
console.log (JSON.stringify (json))
}
});
내 프로젝트 중 하나에서 테스트했는데 다음 오류가 발생합니다.
Package "@csstools/[email protected]" is licensed under "CC0-1.0" which is not permitted by the --onlyAllow flag. Exiting.
순수 쉘 명령으로 MIT 단어 찾기를 읽고 반복 할 수 있지만 어려운 작업이 될 것입니다.
확인 : 셸 스크립트에서 JSON 배열 반복
성공하면 git lab ci에서 validate_licenses.sh를 호출 할 수 있습니다.
test:
stage: test
script:
- echo 'starting licenses validation'
- ./validate_licenses.sh
이 게시물에 따르면 : https://medium.com/@fokusman/the-easiest-way-to-check-all-your-npm-dependency-licenses-753075ef1d9d
요약 된 라이센스 수를 얻을 수 있습니다.
> license-checker --summary
├─ MIT: 949
├─ ISC: 115
├─ BSD-2-Clause: 24
├─ CC0-1.0: 23
├─ BSD-3-Clause: 18
├─ Apache-2.0: 18
├─ CC-BY-4.0: 2
├─ BSD*: 2
마지막으로 당신은 할 수 있습니다 이 파일이 특정 문자열이 포함되어 있는지 확인 하고 오류가 발생
git lab ci에서 nodejs 명령을 실행할 수있는 경우 validate_licenses.js가있는 경우 직접 호출 할 수 있습니다.
test:
stage: test
script:
- echo 'starting licenses validation'
- node validate_licenses.js
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다