-
Is it possible to run a github action on a push to a 2nd repository? For example, I have 2 repos:
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. |
Beta Was this translation helpful? Give feedback.
Replies: 23 comments 24 replies
-
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:
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:
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). |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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.)? |
Beta Was this translation helpful? Give feedback.
-
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:
and this curl command in the library (no more “event_type”):
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
How can I use it for two repositories on an organization? |
Beta Was this translation helpful? Give feedback.
-
You can do something like this nowadays:
|
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
A complete use case
see |
Beta Was this translation helpful? Give feedback.
-
Hello have you found a simple Solution? The Problem is simple and a default situation nearly every developer is in. Java + maven Example 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 |
Beta Was this translation helpful? Give feedback.
-
Appreciate you taking the time to inform us. Definitely takes a lot of trial and error. |
Beta Was this translation helpful? Give feedback.
-
I had a lot of back end forth and this is what worked for me: We have a backend repo with a GitHub Action to pull the frontend, build node, authenticate to gcloud, and deploy to google app engine. I wanted to create an Action in my frontend that when someone merges/pushes to After a lot of back and forth this is what worked for me: name: Trigger Backend Action to Deploy
on:
push:
branches:
- deployment
jobs:
send-request-to-backend:
runs-on: ubuntu-latest
steps:
- name: Deploy
run: |
curl -X POST 'https://api.github.com/repos/{COMPANY}/{BACKEND_REPO}/actions/workflows/ci-development.yml/dispatches' \
-H 'Accept: application/vnd.github.everest-preview+json' \
-H 'Authorization: Bearer ${{ secrets.RUN_BACKEND_ACTION_TO_DEPLOY }}' \
-d '{"ref":"development"}' Notes:
|
Beta Was this translation helpful? Give feedback.
-
This is all a very interesting discussion. I am pleased that I am able to start a workflow in another repository in our org successfully with: - name: Test
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GHA_WORKFLOW_TRIGGER }}
script: |
const result = await github.rest.actions.createWorkflowDispatch({
owner: 'my_org',
repo: 'other_repo',
workflow_id: 'test.yml',
ref: 'my_prs/branch'
})
console.log(result) But per the OP, surely I want to be able to wait for the triggered workflow to complete and then use it's result as the result of the workflow that triggered it (above). I'll go start searching on how to do that, but if anyone has an example or knows, I'd surely appreciate pointers. Update: trigger-workflow-and-wait seems to be working well enough. |
Beta Was this translation helpful? Give feedback.
-
My org has created https://github.com/marketplace/webhook-actions to allow for dispatching across repositories without the use of PATs. Currently we can only offer a free plan until we reach 100 installations. |
Beta Was this translation helpful? Give feedback.
-
so i came up with a horrible way to do this. set up a webhook in discord to post to a private channel when the repo has the desired trigger. then have a bot watch the channel for messages and when there's a message it will trigger your repo. yes i know this is horrible |
Beta Was this translation helpful? Give feedback.
-
It's also possible to use the GitHub CLI to send a steps:
- run: |
gh workflow run workflow.yml -R org/repo
sleep 5
gh run watch -R org/repo $(gh run list -R org/repo -w workflow.yaml -L1 --json databaseId --jq .[0].databaseId)
env:
GH_TOKEN: ${{ secrets.MY_TOKEN }} Note that you must set the References: |
Beta Was this translation helpful? Give feedback.
-
This is how I got the repository_dispatch (not workflow_dispatch) approach mentioned in the answer to work. # file: target.yml
name: Gets called
on:
repository_dispatch:
types: [bcd_release]
jobs:
dostuff:
runs-on: ubuntu-latest
steps:
- name: "do stuff"
env:
VERSION: ${{ github.event.client_payload.version }}
run: |
echo BCD version $VERSION released! Create a PAT in your user account settings. Use "Fine-grained tokens (beta)", select the Workflow in #file: source.yml
name: Release BCD
# I am triggering this manually so using 'workflow_dispatch'
on:
workflow_dispatch
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
github-token: ${{ secrets.DISPATCH_PAT }}
script: |
const result = await github.rest.repos.createDispatchEvent({
owner: 'my-org',
repo: 'target-repo',
event_type: 'bcd_release',
client_payload: {"version": "v5.3.10"}
})
console.log(result); Here is our working live implementation:
|
Beta Was this translation helpful? Give feedback.
-
How can I apply this to a pair of repositories within an organization? |
Beta Was this translation helpful? Give feedback.
-
I'm not too hopeful, but does anyone know if there's any update on a standard way for a consumer repo to monitor for a version update on a producer repo, without the producer taking some action (creating a dispatch event)? My main issues with this are:
The closest I've found is a cron triggered workflow running nightly to poll that producer for a new version; if I want versions faster, it seems like this requires some custom infrastructure. |
Beta Was this translation helpful? Give feedback.
-
Along with the trigger is it possible to pass a value from repo to another?? Eg: Pass docker image tag to be passed from one to another repo |
Beta Was this translation helpful? Give feedback.
-
Not exactly a solution to the requested, but there is always the option to schedule the running of a workflow in your Application repo to check for changes in the Library repo, and when it finds changes, perform a build! I use this approach on a couple of repositories of mine, specifically:
In both cases, I check the latest commit to the referenced repo, calculate a hash of it, and check if I have a docker container with that hash; if I do, the workflow ends, but if such image does not exist, it will perform the build action to the end! |
Beta Was this translation helpful? Give feedback.
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:
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: