bretauv
bretauv

Reputation: 8506

Trigger GitHub Actions if push in another repo

In one of my repos (repo A), there is a script that gets the content of a file on another repo (repo B, not one of mine). Therefore, I would like to trigger a workflow on repo A each time there's a push on repo B. I didn't see this case on GitHub Actions trigger documentation, is this possible?

Upvotes: 13

Views: 6227

Answers (2)

Karima Rafes
Karima Rafes

Reputation: 1108

You can execute each day a script with cron in order to start or not your workflow. A script can read easily the date of last commit of another repository.

Detect when the last commit is less than a day old

With this script, the exit code is 0, only if the last commit is less than a day old.

#!/bin/bash
exit $(curl -s https://api.github.com/repos/wikimedia/mediawiki/commits/REL1_36 | jq -r "((now - (.commit.author.date | fromdateiso8601) )  / (60*60*24)  | trunc)")

Execute the script with cron each day

name: trigger

on:
  schedule:
    - cron:  '30 5 * * *'

  workflow_dispatch:

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - run: ./isUpdated.sh

Start your workflow when a new commit exists since yesterday

name: Your workflow
on:
  workflow_run:
    workflows: ["trigger"]
    types: [completed]

  workflow_dispatch:

jobs:
  check:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - run: echo ok

Upvotes: 6

jidicula
jidicula

Reputation: 3929

edit: this won't work since OP doesn't have push access to repo B

I think that you might be able to get what you want by using the repository_dispatch event. This essentially triggers the workflow when an endpoint is pinged with the correct payload.

From the documentation:

To trigger the custom repository_dispatch webhook event, you must send a POST request to a GitHub API endpoint and provide an event_type name to describe the activity type. To trigger a workflow run, you must also configure your workflow to use the repository_dispatch event.

My best guess is that your desired solution can be achieved by creating 2 workflows: 1 in Repo A that runs on: repository_dispatch, and 1 in Repo B that runs on: push and contains a step that hits Repo A's API (e.g. using curl). This would give you the following events:

  1. You push to Repo B.
  2. Repo B's on: push workflow is triggered, and the step that hits Repo A's API endpoint (e.g. using curl) is run.
  3. Repo A's API endpoint receives the payload and the on: repository_dispatch workflow is triggered.

Upvotes: 5

Related Questions