Triggering by other repository

Is it possible to run a github action on a push to a 2nd repository?

For example, I have 2 repos:

  • Application
  • Library

As soon as I push to Library, I would like the Library CI to run, but also run the CI for the Application, to see if the application still works with the changes in the Library.

3 Likes

Triggers are per-repository, _however _you could send a “repository dispatch” event to your Application repository.  This is a custom event type that has no explicit meaning within GitHub, that you can use for your own purposes.

So in the application’s workflow, you could listen for a repository dispatch event:

on: repository_dispatch

And then in the library’s workflow, you could send a repository dispatch event.  You’ll need to set up a personal access token as a secret in the library’s repository:

run: |
curl -XPOST -u "${{ secrets.PAT_USERNAME}}:${{secrets.PAT_TOKEN}}" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/YOURNAME/APPLICATION_NAME/dispatches --data '{"event_type": "build_application"}'

Replace YOURNAME/APPLICATION_NAME with the name of your github repository for your Application.

Set up a secret named PAT_USERNAME with your username, and PAT_TOKEN with the personal access token that you created (it needs repo level access).

32 Likes

Thanks. I’ve accepted it as a sollution.

Even if it doesn’t really work for my specific usecase, it does seem like a valid way to solve my question.

I have 1 library that is used by about 10 different repositories. And I don’t want to add knowledge about those external repositories to my main library repository. My home grown CI solution currently allows this “watch link”, but I understand that it’s a bit of an odd usecase.

1 Like

I don’t agree that this is a “odd” usecase.

Actually it is very common that builds in upstream projects (e.g. libraries) should also trigger a build. Especially true in the CD world.

24 Likes

Is it possible to go the other way.

If my repository depends on 4 other repos can an action be triggered if one of them has a commit action (to the master for example.)?

3 Likes

This POST command seems to be out of date - see https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event. I got it working instead with this in the application workflow:

on: workflow_dispatch

and this curl command in the library (no more “event_type”):

curl -XPOST -u "${{ secrets.PAT_USERNAME}}:${{secrets.PAT_TOKEN}}" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/YOURNAME/APPLICATION_NAME/actions/workflows/build.yaml/dispatches --data '{"ref": "master"}'

I also found that the only permission required for the token was “public_repo Access public repositories”. Lot of trial and error so hopefully this will save someone else the time.

18 Likes

This is pretty important for our CI across an SDK which is split into various modules across different repos. It’s something that’s supported in Jenkins. Hopefully this will be functionality that arrives at some point.

We also don’t want to hardcode details about consumer modules in our library modules. One solution could be to use a webhook, then from the webhook invoke the workflows using repo dispatch. Feels like quite a bit of effort to set this up though and dependency context would need sorted somewhere else too.

Another option could be to have an intermediate repo which holds workflows which hold teh dependency contexts, so the lib workflow simply uses repository dispatch on a workflow in the intermediate repo. That workflow then does a repo dispatch on the required workflows in other repos. If dependencies change, then they can be updated in one place.

1 Like

How can I use it for two repositories on an organization?

1 Like

You can do something like this nowadays:

    - name: Deploy Stage
      uses: actions/github-script@v5
      with:
        script: |
          await github.rest.actions.createWorkflowDispatch({
            'Mause',
            'fdroidrepo',
            'workflows/update.yaml',
            'main',
          });
3 Likes

As far as I understand, this still uses the default repo-scoped GITHUB_TOKEN so you need to pass your own token if you want to access a different repository: GitHub - actions/github-script: Write workflows scripting the GitHub API in JavaScript

Also worth noting that the documentation link to the right area is https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#repository_dispatch

A complete use case

on: [push]
jobs:
  dispatch:
    runs-on: ubuntu-latest
    steps:
    # - run: |
    #     curl -u "${{ secrets.PAT_USERNAME}}:${{secrets.PAT_TOKEN}}" \
    #       -H "Accept: application/vnd.github.everest-preview+json" \
    #       -H "Content-Type: application/json" \
    #       https://api.github.com/repos/docker-sstc/docker-notify/actions/workflows/main.yml/dispatches \
    #       --data '{"ref": "master"}'
    - uses: actions/github-script@v6
      with:
        github-token: ${{ secrets.PAT_TOKEN }}
        script: |
          await github.rest.actions.createWorkflowDispatch({
            owner: 'docker-sstc',
            repo: 'docker-notify',
            workflow_id: 'main.yml',
            ref: 'master'
          })

see

3 Likes

Hello

have you found a simple Solution?

The Problem is simple and a default situation nearly every developer is in.

Java + maven Example
1 Repo with a lib => Version 0.1.0-SNAPSHOT
1 Repo with the app =>depends on repo lib => 0.1.0-SNAPSHOT
both repos are own be the same developer/company

1 Developer commits something to the lib => i want to rebuild the app fully automated. without knowledge in the lib which app uses this lib.

any idea? i havent found some solution for this use case