如何在推送的每个提交(含2个以上提交)中运行GitHub工作流
解决方法
当前你的GitHub Action配置仅在推送到main分支时触发,要实现每次推送(无论推送到哪个分支、包含多少提交)都运行Action,需要调整on.push的分支配置:
修改后的完整配置
name: javascript-CI on: push: branches: ['*'] # 监听所有分支的推送事件 pull_request: branches: [main, master] permissions: write-all jobs: ci_to_main: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up node.js uses: actions/setup-node@v4 with: node-version: 20 - name: Install dependencies run: npm ci - name: Run Unit Tests run: npm run test-once - name: Jest coverage report uses: ArtiomTr/jest-coverage-report-action@v2.2.6 id: coverage with: output: report-markdown, comment - uses: marocchino/sticky-pull-request-comment@v2 with: message: ${{ steps.coverage.outputs.report }} ##### #- name: Run E2E Tests # uses: cypress-io/github-action@v4 # with: # start: npm start # wait-on: "http://localhost:1234" ##### #- name: Build for deploy # run: npm run build #- name: Deploy to GitHub Pages # if: success() # uses: crazy-max/ghaction-github-pages@v2 # with: # target_branch: gh-pages # build_dir: dist # env: # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
关键修改说明
- 将
on.push.branches设置为['*'],表示监听所有分支的推送操作,只要有代码推送到仓库的任何分支,这个Action就会自动运行。 - 如果你只想针对特定分支(比如除了main之外,还有dev、feature/*等),可以改成更具体的匹配规则,比如:
这样会监听main、dev分支,以及所有feature前缀的子分支的推送。push: branches: [main, dev, 'feature/**']
注意事项
GitHub的push事件是基于推送动作触发的,而不是单个提交。如果一次性推送多个提交到远程,Action只会运行一次,而不是每个提交单独运行一次。如果确实需要针对单个提交触发,需要结合其他事件规则,但这种场景很少见,大部分情况下监听推送动作就能满足需求。
内容的提问来源于stack exchange,提问作者Dayan Aguilar Alcocer
相关产品推荐
相关产品推荐

