【发布时间】:2021-12-20 19:33:10
【问题描述】:
为了更流畅的 CI 体验,我制作了一个 Github Action 工作流程来发布带有预发布版本的 monorepo 包,每次任何成员打开一个带有特定标签“发布”的针对 master 的 PR。此工作流程
理想情况下,应该使用 preid -pr{pr#} 发布自上次发布以来所有更改的包,例如 package-pr1049.0。在这里也添加了 dist-tag 和 predist-tag。
背景: 在发布包之前,我还运行了一个 MAKE 可执行文件(make -j init 脚本)来清理和引导所有包。发布这个,它将获取 repo,结帐到所需的分支,并使用 PR 号参数运行发布命令。
我在这个工作流程中面临两个问题:
- 在 PR 的第一次提交中发布所有包:
为了调试问题,还添加了一个记录器来查看它是否具有最近 10 次提交的正确记录,这反映了正确的提交集。
第二次提交之后,只有更改的包会按预期发布。参考日志
lerna notice cli v3.20.2
lerna info versioning independent
lerna info ci enabled
lerna info Assuming all packages changed
lerna info getChangelogConfig Successfully resolved preset "conventional-changelog-angular"
Changes:
- @swiggy-private/package-1: 1.0.4 => 1.1.0-pr19000.0
- @swiggy-private/package-2: 1.1.4 => 1.2.0-pr19000.0
- @swiggy-private/package-3: 2.41.2 => 2.42.0-pr19000.0 (private)
- 始终将补丁版本更新为 0,并递增 preid(带有 -pr 前缀的 PR 编号)PACKAGEv0-pr{##}.0, PACKAGEv0-pr{##}.1, ....
为了加快调试过程,将 lerna.json 中的 monorepo 包限制为仅 3 个包。
我的 GH 工作流程
name: Branch Publish
on:
pull_request:
types: [opened, synchronize, reopened, labeled]
branches:
- master
jobs:
check:
runs-on: ubuntu-latest
timeout-minutes: 15
outputs:
author: ${{ steps.step1.outputs.author }}
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- id: "step1"
run: |
AUTHOR_NAME=$(git show ${{ github.event.pull_request.head.sha }} | grep Author)
echo "::set-output name=author::$AUTHOR_NAME"
init:
if: "!contains(needs.check.outputs.author, 'GitHub Action Branch') && !contains(github.event.head_commit.message, '[skip ci]')"
runs-on: ubuntu-latest
timeout-minutes: 15
needs: [check]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "12.x"
- run: git fetch --prune --unshallow
- run: |
make -j init
env:
NPM_TOKEN: ${{ secrets.GH_TOKEN }}
- uses: actions/cache@v1
id: cache-build
with:
path: "."
key: ${{ github.sha }}
release:
if: "contains(github.event.pull_request.labels.*.name, 'publish')"
runs-on: ubuntu-latest
timeout-minutes: 15
needs: [init]
steps:
- uses: actions/checkout@v2
with:
fetch-depth: "0"
- uses: actions/setup-node@v1
with:
node-version: "12.x"
- uses: actions/cache@v1
id: restore-build
with:
path: "."
key: ${{ github.sha }}
- name: Setup Git
uses: webfactory/ssh-agent@v0.4.1
with:
ssh-private-key: ${{ secrets.GHA_DEPLOY_KEY }}
- name: Lerna Publish
if: success()
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
NODE_ENV: production
run: |
git config user.email "action@github.com"
git config user.name "GitHub Action Branch"
git remote set-url origin "git@github.com:${{ github.repository }}"
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
git checkout -- .
git log --pretty=oneline -n 10
git checkout --track origin/$(echo $GITHUB_HEAD_REF | cut -d'/' -f 3)
NUMBER=${{ github.event.number }} npm run publish-branch
- name: Possible Package lock update
if: success()
run: |
git config user.email "action@github.com"
git config user.name "GitHub Action Branch"
git remote set-url origin "git@github.com:${{ github.repository }}"
npx lerna clean -y
npx lerna exec -- npm i --package-lock-only --ignore-scripts --no-audit
echo `git add . && git commit -m "chore: package lock update" --no-verify && git push`
发布命令
"publish-branch": "lerna publish --conventional-prerelease --exact --no-changelog --preid pr$NUMBER --dist-tag beta --pre-dist-tag beta --no-verify-access --yes"
Lerna.json
{
"packages": ["*"],
"version": "independent",
"command": {
"publish": {
"npmClient": "npm",
"graphType": "all",
"allowBranch": ["master", "integration", "*"],
"conventionalCommits": true,
"message": "chore(release): publish",
"includeMergedTags": true,
"ignoreChanges": ["**/__tests__/**", "**/*.md"]
}
}
}
制作引导包的脚本
init: clean-all
$(MAKE) create-npmrc-all
npm ci
npm run bootstrap:ci
NODE_ENV=production npm run prepare:all
create-npmrc-all:
echo $(GITHUB_SCOPE_REGISTRY) >> .npmrc
echo $(GITHUB_REGISTRY_TOKEN) >> .npmrc
$(foreach source, $(DIRECTORY), $(call pass-to-npmrc, $(source), $(GITHUB_SCOPE_REGISTRY)))
$(foreach source, $(DIRECTORY), $(call pass-to-npmrc, $(source), $(GITHUB_REGISTRY_TOKEN)))
clean-all:
rm -rf node_modules
$(foreach source, $(SOURCES), \
$(call clean-source-all, $(source)))
rm -rf .npmrc
rm -rf packages/*/.npmrc
rm -rf coverage
rm -rf packages/*/npm-debug*
【问题讨论】:
标签: javascript frontend github-actions monorepo lerna