28

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?

1
  • Github actions run on a remote box in batch mode, so I don't see how they can be interactive. That having been said, it should be easy to control what the action does just by modifying it and checking it in.
    – ccleve
    Dec 20, 2019 at 18:15

3 Answers 3

35

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.

1
  • 1
    Additional bit of info possibly useful to know in this context: the gh command-line tool lets you invoke workflows and pass the input values on the command line. C.f. cli.github.com/manual/gh_workflow_run
    – mhucka
    Dec 12, 2022 at 21:15
3

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.