82

I'm trying to build a GitHub workflow that will be triggered when another repository creates a new release.

In the documentation, there is the paragraph: on.event_name.types where event_name will be release.

The question is: Is there any way to refer to the release event of another repository?

pierDipi
  • 1,388
  • 1
  • 11
  • 20

3 Answers3

74

Is there any way to refer to the release event of another repository?

Fairly sure this feature does not exist.

If you have have access to the repository creating the release then you could call a webhook event to trigger an on: repository_dispatch workflow to run in another repository. repository-dispatch action can help in this case.

If you don't have access to the repository creating the release (which I assume is the case here) then this would be my suggestion. First, create the following workflow that periodically checks the release version tag of the repository you want to track. If it differs from the release version you currently have saved in your repository then the new version will be committed.

Note that you must prepare the destination file first (e.g. release-versions/swagger-ui-latest.txt) for the the modified files check to work. Further, you must use a repo scoped token instead of the default GITHUB_TOKEN. For more detail about that see Push to origin from GitHub action

name: Get latest release version
on:
  schedule:
    - cron:  '0 10 * * *'
jobs:
  get-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.REPO_SCOPED_TOKEN }}
      - name: Fetch release version
        run: |
          curl -sL https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | \
          jq -r ".tag_name" > release-versions/swagger-ui-latest.txt
      - name: Check for modified files
        id: git-check
        run: echo ::set-output name=modified::$([ -z "`git status --porcelain`" ] && echo "false" || echo "true")
      - name: Commit latest release version
        if: steps.git-check.outputs.modified == 'true'
        run: |
          git config --global user.name 'Your Name'
          git config --global user.email 'your-email@users.noreply.github.com'
          git commit -am "New release version"
          git push

Then you can create a second workflow that only runs when it sees any changes to the directory release-versions.

on:
  push:
    paths:
      - 'release-versions/*'

In this workflow you can use the saved version to fetch the assets you need and do whatever processing you need to.

Here is a similar example that raises a pull request instead of committing immediately.

peterevans
  • 34,297
  • 7
  • 84
  • 83
  • 2
    Sadly you confirmed my hunch. Besides, thank you for sharing your approach. – pierDipi Oct 20 '19 at 11:18
  • 8
    I want to add my experience: instead of using this: `$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)` I think it would be better to use: ```$([ -z "`git status --porcelain`" ] && echo "false" || echo "true")``` why? `git diff-index` will return a value also in the case that only the modified timestamp of the file has changed. This than will cause the commit to fail because there are no changes to commit. Using `git status` will only return something when actual content has changed. – KirKone May 14 '20 at 14:02
  • 1
    I have access to the parent repo, could you write sample code for my case as well please? – Boris Verkhovskiy Oct 29 '21 at 21:09
  • @Boris, I found this tutorial implementing triggering another repository workflow: https://keithweaver.ca/lessons/trigger-another-repositorys-github-action-workflow-wait-for-result/?s=ytktc – WaterFox Jan 04 '22 at 22:51
  • any way to connect GH subscription with GH Action?, so subscribe on commit of other repository to run action. – Tilo Mar 29 '22 at 19:29
  • It is doable, here is a good example how to do this: https://blog.marcnuri.com/triggering-github-actions-across-different-repositories – tostao May 27 '22 at 07:45
8
    name: Trigger automation tests
steps:
- uses: actions/github-script@v6
  with:
    github-token: ${{ secrets.PERSONAL_TOKEN }}
    script: |
      await github.rest.actions.createWorkflowDispatch({
       owner: '*****',
       repo: '*****',
       workflow_id: '*****.yml',
       ref: 'main'
      })
      
  1. Create personal access token in target repository.
  2. Add this personal access token as secret in both repositories.
  3. Create a new workflow or edit one which is already exists (yml file).
  4. Add the above step to yml file.

owner = organization / personal gitHub. repo = target repository to run. workflow_id = yml file name of the target workflow. ref = which brench to run in target repository

Eyal Sooliman
  • 1,876
  • 23
  • 29
0

Similarly to @peterevans answer I would suggest to create two workflows - one that regularly checks for updates and one that will be triggered if a new release is detected.

The workflow that check for updates could look like this:

name: Check for new release of xyz

on:
  schedule:
    - cron: '0 0 * * *'
  workflow_dispatch:

jobs:
  check:
    runs-on: ubuntu-latest

    steps:

      - name: Download previous release info
        id: download-artifact
        uses: dawidd6/action-download-artifact@v2 # It's not possible to use actions/upload-artifact as of the time of writing
        with:
          name: xyz-release-info
          workflow_conclusion: success
          workflow: publish.yml
          if_no_artifact_found: warn

      - name: Get latest release
        id: get_release
        run: |
          # Fetch release information and extract the release tag
          RELEASE_TAG=$(curl -s https://api.github.com/repos/USER/xyz/releases/latest | jq -r '.tag_name')
          echo "release_tag=$RELEASE_TAG" >> $GITHUB_OUTPUT
          echo "latest release: $RELEASE_TAG"

      - name: Compare with previous release
        id: compare_release
        run: |
          # Read the release info from the downloaded artifact
          PREVIOUS_RELEASE=$(cat xyz-release-info 2> /dev/null || echo "NONE")
          echo "previous release: $PREVIOUS_RELEASE"

          # Compare the fetched release tag with the previous release tag
          if [ "${{ steps.get_release.outputs.RELEASE_TAG }}" != "$PREVIOUS_RELEASE" ]; then
            echo "release_changed=true" >> $GITHUB_OUTPUT
            echo "Release changed: true"
          else
            echo "release_changed=false" >> $GITHUB_OUTPUT
            echo "Release changed: false"
          fi

      - name: Call workflow to build code
        if: steps.compare_release.outputs.release_changed == 'true'
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: publish.yml

And the workflow that you want to trigger (publish.yml) like this:

name: Build

on:
  workflow_dispatch:

jobs:
  build:

    runs-on: ubuntu-latest
    
    ...

    steps:

      ...

      - name: Get latest release
        id: get_release
        run: |
          # Fetch release information and extract the release tag
          RELEASE_TAG=$(curl -s https://api.github.com/repos/USER/xyz/releases/latest | jq -r '.tag_name')
          echo "release_tag=$RELEASE_TAG" >> $GITHUB_OUTPUT
          echo "latest release: $RELEASE_TAG"
      
      - name: Store artifacts
        run: |
          # Store the newly fetched release version in a file
          echo "${{ steps.get_release.outputs.RELEASE_TAG }}" > xyz-release-info
          echo "Saved ${{ steps.get_release.outputs.RELEASE_TAG }} to xyz-release-info"

      - name: Upload new artifacts
        uses: actions/upload-artifact@v3
        with:
          name: xyz-release-info
          path: xyz-release-info
Trigus
  • 23
  • 3