avo*_*avo 6 javascript version-control continuous-integration node.js npm
我似乎看不到树后的森林。我想要一个简单的CI管道来构建和发布NPM软件包。我使用appveyor,但我认为我的问题并不特定于此。我只希望我的CI脚本执行以下操作:
git clone "https://git_repo_url" .
npm run build
npm run test
npm version patch --git-tag-version
npm publish -tag beta
Run Code Online (Sandbox Code Playgroud)
问题是:
如果我不执行此npm version patch步骤,则发布将失败并显示feed already contains the package 'abc' at version 'x.y.z'错误。
如果执行该步骤,则必须将新提交(版本更改)推回到git repo中。否则,它将在我或其他人下次构建时失败,如上。但是我不认为git push在后端管道中做是正确的事情。
最后,如果此CI脚本只是构建NPM包而不发布它,我如何在依赖它的其他项目中使用它?
行业标准的方式是什么?
例如,如果我需要与另一个项目一起测试我的软件包的非生产功能版本,是否应该使我的CI脚本package.json使用生成的,与semver兼容的唯一版本(无需提交)来修补该软件包,然后发布它与npm我的git分支名称匹配的标签?这是个好主意吗?
回答我自己。正如上面评论中所建议的,我决定采用分支机构的semantic-release发布方式master。
为了从开发分支进行构建和发布,我创建了一个自定义节点脚本,以基于git commit的哈希,当前major.minor.patch版本和当前时间生成与semver兼容的预发行版本:
const cp = require('child_process');
// get current semver version without prerelease suffix
const pkg = require('./package.json');
const curVer = pkg.version.trim().split(/[.-]/).slice(0, 3).join('.');
// get the commit id
const commit = cp.execSync('git rev-parse --short HEAD', {encoding: 'utf-8'}).trim();
console.log(`Package: ${pkg.name}, version: ${curVer}, commit: ${commit}`);
// generate a new unique semver-compliant version based the commit it and current time
const uniqueVer = `${curVer}-beta-${commit}-${Math.random().toFixed(8).substr(2)}.${Date.now()}`
// use npm version to update package.json
cp.execSync(`npm version ${uniqueVer} --no-git-tag-version`, {stdio: 'inherit'});
// publish and tag with commit id
cp.execSync(`npm publish --tag ${commit}`, {stdio: 'inherit'});
Run Code Online (Sandbox Code Playgroud)
这样,我可以将自己的东西检入dev分支,构建CI管道并为我发布程序包,然后使用来使用它npm install mypackage@commitid。将会生成一个伪唯一的版本,并将其发布到NPM注册表中,但是修改后的版本package.json不会被检入。
目前,这种方法应该对我有用,但是我仍然非常感兴趣学习最佳DevOps做法,以便在正常开发周期内使用CI发布内部/每个版本的NPM软件包。
小智 5
我在我的 javascript 项目中按如下方式进行了操作。
注意:获取您的密钥表单 .npmrc
publish:
image: <your project image>
stage: publish
script:
- echo _auth=$NPM_PUBLSH_KEY >> .npmrc
- echo email=<youremail> >> .npmrc
- echo always-auth=true >> .npmrc
# - cat .npmrc
- npm version --no-git-tag-version $(npm view <yourProject>@latest version)
- npm version --no-git-tag-version prerelease
- npm publish
dependencies:
- build
build:
image: <your project image>
stage: build
script:
- node --version
- npm --version
- npm ci
- npm run build -- --watch=false
- npm run build:prod
Run Code Online (Sandbox Code Playgroud)