Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional parameters ref and sha #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

elliot-nelson
Copy link

@elliot-nelson elliot-nelson commented Feb 25, 2021

SUMMARY

Add two optional parameters to the task:

  • ref:, which will override the default GITHUB_REF value
  • sha:, which will override the default GITHUB_SHA value

DETAILS

Allowing the user to specify exactly what branch name and commit SHA to pass to the Azure pipeline is necessary for workflows that trigger from non-pull-request events like issue_comment. For security reasons, it's impossible to override the values of GITHUB_REF and GITHUB_SHA in the environment, which means for these workflows these values will always be pointing at the current tip of the default branch (e.g. main/master). That makes it impossible to trigger an Azure pipeline pointing at the current head branch of a pull request from an issue comment.

By supporting these optional parameters, we can allow the workflow to use an inline github script or other avenue to retrieve the desired ref/sha from the event pull request, and then pass them to the Azure pipeline.

An example of what that might look like:

on: issue_comment
jobs:
  pr_command:
    name: PR Command
    if: github.event.issue.pull_request
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v3
        id: get-pr
        with:
          script: |
            const request = {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            };
            core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`);
            try {
              const result = await github.pulls.get(request);
              return result.data;
            } catch (err) {
              core.setFailed(`Request failed with error ${err}`);
            }
      - name: 'Trigger pipeline'
        uses: Azure/pipelines@v1
        with:
          azure-devops-project-url: https://dev.azure.com/ORG/PROJECT
          azure-pipeline-name: my-issue-comment-pipeline
          azure-devops-token: ${{ secrets.AZURE_DEVOPS_TOKEN }}
          ref: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
          sha: ${{ fromJSON(steps.get-pr.outputs.result).head.sha }}

(This example based on the excellent comment here: actions/checkout#331 (comment))

 - Specify optional `ref:` parameter to override GITHUB_REF
 - Specify optional `sha:` parameter to override GITHUB_SHA
@ghost
Copy link

ghost commented Feb 25, 2021

CLA assistant check
All CLA requirements met.

@rjulius23
Copy link

Hey @elliot-nelson are you planning to push this in ? Or can I take it over? :)

@elliot-nelson
Copy link
Author

@rjulius23 I wouldn't mind if you wanted to take it over! Unfortunately my need for the change has passed, as the pipelines we were targeting ended up moving to GitHub Actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants