CircleCI-Public / trigger-circleci-pipeline-action

Trigger a CircleCI pipeline from any GitHub Actions event.
https://github.com/marketplace/actions/trigger-circleci-pipeline
47 stars 43 forks source link

Bug: Triggering via issue_comment Action Kicks Off Workflow For Incorrect Commit/Branch #61

Open jblacker opened 1 year ago

jblacker commented 1 year ago

Is there an existing issue for this?

Current behavior

When I attempt to trigger a workflow in CircleCI from this GitHub Action on the created issue_comment event it works properly, but then starts the workflow on the incorrect branch within CircleCI. It appears to start the pipeline for the commit at HEAD of main rather than on the branch/commit in the Pull Request that triggered the action.

Minimum reproduction code

https://gist.github.com/jblacker/4f8b3d87b3b0b60a00c166e05d6ced81

Steps to reproduce

  1. Create a workflow pipeline in CircleCI using the conditions in the "other" section below
  2. Use the example in my gist to create a GitHub Action that triggers said pipeline on CircleCI
  3. Create a new branch that's not the default and open a PR
  4. Write the comment on the PR "/deploy-staging"
  5. Check CircleCI you'll see that it triggered main (or whatever your default is) rather than the head of the current branch of the PR.

Expected behavior

I would have expected the branch that the PR is for to be triggered in CircleCI.

GitHub Action Version

1.0.5

Other

Here's the trigger on the CircleCI side of things:

when:
      or:
        - and:
          - equal: [<<pipeline.parameters.deploy_branch>>, <<pipeline.git.branch>>]
          - equal: [<<pipeline.parameters.deploy>>, true]
        - and:
          - <<pipeline.parameters.GHA_Action>>
          - equal: [<<pipeline.parameters.GHA_Event>>, "issue_comment"]
          - equal: [<<pipeline.parameters.GHA_Meta>>, "DEPLOY_STAGING"]
jblacker commented 1 year ago

This issue is actually mentioned in PR #11, but after it was merged. Figured it made sense to create a top-level ticket pointing out this issue.

ghost commented 1 year ago

Just ran into the same thing. You can use version 1.1.0 of this action and provide target-branch as parameter. Unfortunately you need to fetch the branch name manually beforehand. (ref: https://github.com/actions/checkout/issues/331)

This works for me:

name: pr comment
on:
  issue_comment:
    types: [created]
jobs:
  deploy-dev:
    if: github.event.issue.pull_request && contains(github.event.comment.body, '/dev')
    name: Deploy dev
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
      pull-requests: write
    steps:
      - id: 'get-branch'
        run: echo ::set-output name=branch::$(gh pr view $PR_NO --repo $REPO --json headRefName --jq '.headRefName')
        env:
          REPO: ${{ github.repository }}
          PR_NO: ${{ github.event.issue.number }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Trigger CircleCI
        id: trigger-circleci
        uses: CircleCI-Public/trigger-circleci-pipeline-action@v1.1.0
        with:
          target-branch: ${{ steps.get-branch.outputs.branch }}
        env:
          CCI_TOKEN: ${{ secrets.CCI_TOKEN }}
GreaseMonk commented 1 year ago

@iwt-denniskribl

Just ran into the same thing. You can use version 1.1.0 of this action and provide target-branch as parameter.

Please could you add this to trigger-circleci-pipeline-action/examples/ ? I have been looking for a solution like this all day and this finally worked! My usecase is also a slash command as comment to trigger a workflow that should not normally run.