Jeremy Gillick
Jeremy Gillick

Reputation: 2700

User input in github actions (specify repo branch, etc)

I want to create a github action to create an integration test environment. We already have a dockerized script which can do this, however, the environment is made up of 2+ repositories. So, in order to make this effective during development, we'd need to specify the branches of the other repos.

For example, let's say I have a PR in repo: frontend, branch: my-feature-brach. It requires repo: backend, branch: their-feature-branch. I'd like to kick off a build from my PR where it uses the branch of that PR (in the frontend repo), and ask me which branch to use for the backend repo.

Is this possible?

Upvotes: 32

Views: 71931

Answers (3)

peterevans
peterevans

Reputation: 41900

Note: This solution predates workflow_dispatch events. For most use cases workflow_dispatch will be the best option, but I leave this solution here as an alternative.

You could use a slash command style "ChatOps" solution. The action slash-command-dispatch can help you trigger workflows with slash commands (e.g. /build) from issue and pull request comments.

Here is a basic example for a build slash command in pull request comments. REPO_ACCESS_TOKEN is a repo scoped Personal Access Token

name: Slash Command Dispatch
on:
  issue_comment:
    types: [created]
jobs:
  slashCommandDispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Slash Command Dispatch
        uses: peter-evans/slash-command-dispatch@v2
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          commands: build
          issue-type: pull-request

The command can be processed in this workflow.

name: Build Command
on:
  repository_dispatch:
    types: [build-command]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # Checkout the pull request branch
      - uses: actions/checkout@v2
        with:
          repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
          ref: ${{ github.event.client_payload.pull_request.head.ref }}
          path: ${{ github.event.repository.name }}
          token: ${{ secrets.REPO_ACCESS_TOKEN }}

If you pass arguments to the slash command they will be passed with the payload. For example, the branch name.

/build ref=their-feature-branch

Then in the workflow you can checkout the branch passed via argument ref.

      - uses: actions/checkout@v2
        with:
          repository: backend-repo
          ref: ${{ github.event.client_payload.slash_command.args.named.ref }}
          path: backend-repo
          token: ${{ secrets.REPO_ACCESS_TOKEN }}

This is just a very brief look at what you could do with slash-command-dispatch action. Please check out the repository for full details.

Upvotes: 4

hEngi
hEngi

Reputation: 915

You can define manually executable workflows, with inputs.

on: 
  workflow_dispatch:
    inputs:
      environment:
        description: 'Define env name'     
        required: true
        default: 'prod'
      branch:
        description: 'Define branch name'     
        required: true
        default: 'master'

Than you can use these predefined parameters like:

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Env: ${{ github.event.inputs.environment }}" 
        echo "Branch: ${{ github.event.inputs.branch }}"

I think you can support your use case with that. More details here.

Upvotes: 38

dan1st
dan1st

Reputation: 16338

GitHub Actions can interact with PRs using the GitHub API.

That means, your script could create a comment on the repo and it would be re-activated(specifically, the real action is activated) on a comment by the PR creator(or whoever you want) If it mentions the branch(you decide the format), it starts the test with the branch.

Another possibility is to require a specified format of the PR(e.g. it needs branch:<branch> in a comment). The action extracts the branch from the PR description and uses that branch.

See the GitHub API docs for PRs and the GitHub actions docs for the github context (for info with the PR) for reference.

Upvotes: 1

Related Questions