如何在PR合并后触发GitHub CI

6
我希望在合并请求被合并后触发我的GitHub CI,因为我想在GitHub中的合并请求合并后将我的源代码从GitHub发送到gitlab。CI会在创建拉取请求时触发,但在合并后不会触发CI,因此我无法将源代码从GitHub发送到gitlab。示例-https://github.com/kumaresan-subramani/ej2-blaz-doc/pull/7

enter image description here

我的yml文件:

name: Node.js CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
    types: [opened, closed]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - name: set environment variables
      uses: allenevans/set-env@v1.0.0
      with:
          MY_ENV_VAR: 'my value'
          GIT_USER: ${{ secrets.USER }}
          GIT_TOKEN: ${{ secrets.TOKEN }}
    - name: Install
      run: npm i
    - name: Publish
      run: npm run publish
      if: github.event.pull_request.merged == 'true'
1个回答

12

你需要将字段值merged与布尔值进行比较,而不是字符串。

github.event.pull_request.merged == true

您可以这样写:

github.event.pull_request.merged == 'true' 改为

if: github.event.pull_request.merged

例如,下面的工作流程会比较打开/关闭PR时的值:
on: 
  pull_request:
    types: [opened, closed]
name: build
jobs:
  build:
    name: Input check
    runs-on: ubuntu-latest
    steps:
      - name: Checking your input
        run: |
          echo "github.event.pull_request.merged           : $MERGED_RAW"
          echo "github.event.pull_request.merged == 'true' : $MERGED_TRUE_STR"
          echo "github.event.pull_request.merged  == true  : $MERGED_TRUE_BOOL"
        env:
          MERGED_RAW: ${{ github.event.pull_request.merged }}
          MERGED_TRUE_STR: ${{ github.event.pull_request.merged == 'true' }}
          MERGED_TRUE_BOOL: ${{ github.event.pull_request.merged == true }}

当您合并PR时,会得到以下结果:

github.event.pull_request.merged           : true
github.event.pull_request.merged == 'true' : false
github.event.pull_request.merged == true   : true

这对我解决更复杂的触发器情况非常有帮助,需要关注PR和合并以及其他一些事情。人们认为“true”也可以工作,但很快就发现不是这种情况。 - Jacob
@Bertrand 当我们有多个作业时,如何仅在合并(推送)时运行部署?作业: 测试和构建://在拉取和合并(推送)时运行,这很好用。 //构建代码 部署://我只想在合并(推送)时运行部署 要求:测试和构建 //部署代码 - Ankit Pandey
在撰写本文时(2022年12月),如果不指定 types: [closed],它对我来说无法正常工作。查看文档后,它还指出:“默认情况下,仅当 pull_request 事件的活动类型为 opened、synchronize 或 reopened 时,工作流才会运行。”https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request - Beolap
@Beolap,这在他们的文档中已经说明了,所以我认为这是预期行为 https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-your-workflow-when-a-pull-request-merges - javierlga
@javierlga,是的,之前没有阅读文档,期望默认行为是所有类型。 - Beolap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接