簡體   English   中英

GitHub Actions 在錯誤的分支上運行工作流

[英]GitHub Actions Running Workflow on Wrong Branch

我有以下用於 GitHub Actions 的.github/workflows/ci.yml文件(刪除了一些代碼以使其更容易理解這個問題):

name: CI
on:
  push:
  release:
    types: [published]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # ...
  deploy-staging:
    runs-on: ubuntu-latest
    needs: test
    if: github.event_name == 'push' && github.ref == 'staging'
    steps:
      # ...

我經歷了以下步驟:

  1. develop分支上進行一些提交,並推送這些更改。
  2. 在 GitHub Actions 上構建通過后,我進行了從developstaging的快速合並。

我希望 GitHub Actions 在第 2 項之后運行testdeploy-staging作業。但它只是再次運行test而沒有運行deploy-staging

GitHub 操作工作流運行

正如您在上面看到的,即使在推送到staging之后,它仍然在develop分支而不是staging分支上運行它。 我有點假設這可能是由於快進合並的一些奇怪行為。 但是 GitHub 顯然意識到我推動了staging因為它提供了從那個分支到master創建一個 PR。

GitHub 提供為 <code>staging</code> 創建 PR

所以這讓我重新思考我的理論,為什么它試圖運行develop而不是staging

為什么會發生這種情況? 有沒有辦法解決這個問題,所以合並到staging實際上在staging上運行工作流,而不是develop

${{ github.ref }}將是refs/heads/staging而不僅僅是staging

在這些情況下,最好的做法是簡單地在它之前的步驟中回顯要檢查的變量值:

    steps:
      - name: Check inputs
        run: |
          echo github.ref is: ${{ github.ref }}
          echo github.event_name is: ${{ github.event_name }}

我的方法是將觸發器和相關作業分離到不同的工作流程中。

因此,為了模仿您的示例,我將有兩個文件而不是ci.yml

  • test.yml
  • deploy-staging.yml

.github/workflows/test.yml

name: Test
on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # ...

.github/workflows/release-staging.yml

name: Release Staging

on:
  push:
    branches:
      - staging

jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    steps:
      # test steps ...
      # release ...

誠然,這很煩人,因為該版本沒有在與測試相同的測試運行中運行,但是您希望在部署之前確保所有測試都通過。

如果您想鏈接測試運行工作流來運行部署工作流,我可能會將release-staging更改為使用檢查套件事件而不是推送。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM