Search code examples
githubgithub-actionsgit-pushmirroring

Trigger a GitHub Action on a foreign repo update (push)


I'm trying to mirror a public repo that I don't own, and more importantly mirror their pushes (to trigger another action).

Right now, the sync actions I have seen seem to copy paste a repo into a repo I own, but that repo's pushes don't trigger actions. Is there a way to do this ?

I don't know the foreign repo's owner. I'm aware that the owner could send a dispatch event, but I want a solution that doesn't rely on the goodwill of someone.

Basically, I want this to happen: My repo synchronizes with a foreign one every hour, and if there was an update in the last hour then another action gets triggered.

Is there a way to do this ?


Solution

  • For people with the same problem, I found a way through a cron schedule:

    1. Create an empty text file in your repo
    2. Check the original repo's commit id
    3. If that id is different from the one in the text file, then trigger a sync Action (and optionally any other Action), and then add the commit id in a small text file
    4. Otherwise, do nothing
    5. Repeat from 2 on a cron schedule

    Updates on the foreign repo will trigger on the cron schedule.

    It's not exactly what I wanted, but close enough.

    Edit: Here is a complete .yml file example from my repo, decommented to make it shorter

    name: Sync+build+push TTRSS
    
    on:
      schedule:
        - cron:  '0 0 * * *'
      workflow_dispatch:
    
    jobs:
      get_commits:
        runs-on: ubuntu-latest
        outputs:
          LOCAL: ${{ steps.commits.outputs.SETLOCAL }}
          REMOTE: ${{ steps.commits.outputs.SETREMOTE }} 
        steps:
          - name: Checkout
            uses: actions/checkout@v2
            with:
              ref: 'TTRSS-docker'
          - name: set local and remote latest commit as environment variables
            id: commits
            run: |
              echo "::set-output name=SETREMOTE::$(git ls-remote https://git.tt-rss.org/fox/ttrss-docker-compose.git HEAD | awk '{ print $1 }')"
              echo "::set-output name=SETLOCAL::$(cat last_sync_with_original_repo_commit_id)"
      
      repo_sync:
        needs: [get_commits]
        runs-on: ubuntu-latest
        if: needs.get_commits.outputs.LOCAL != needs.get_commits.outputs.REMOTE
        steps:
          - name: repo-sync
            uses: wei/git-sync@v3
            with:
              source_repo: "https://git.tt-rss.org/fox/ttrss-docker-compose.git"
              source_branch: "master"
              destination_repo: "git@github.com:schklom/Mirror-workflows.git"
              destination_branch: "TTRSS-docker"
              ssh_private_key: ${{ secrets.GITSYNCACTION }}
          - name: Checkout
            uses: actions/checkout@v2
            with:
              ref: 'TTRSS-docker'
          - name: get most recent commit id on original repo, for next comparison on sync
            run: git ls-remote https://git.tt-rss.org/fox/ttrss-docker-compose.git HEAD | awk '{ print $1 }' > last_sync_with_original_repo_commit_id
          - name: Commit and push the change
            uses: stefanzweifel/git-auto-commit-action@v4
            with:
              commit_message: Add last_sync_with_original_repo_commit_id
    
      build_push:
        needs: [repo_sync]
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v2
            with:
              ref: 'TTRSS-docker'
          - name: Set up QEMU
            uses: docker/setup-qemu-action@v1
          - name: Set up Docker Buildx
            id: buildx
            uses: docker/setup-buildx-action@v1
          - name: Login to Docker Hub
            uses: docker/login-action@v1
            with:
              username: ${{ secrets.DOCKERHUB_USERNAME }}
              password: ${{ secrets.DOCKERHUB_TOKEN }}
          - name: Build and push TTRSS app
            uses: docker/build-push-action@v2
            with:
              context: ./app
              file: ./app/Dockerfile
              platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
              pull: true
              push: true
              tags: |
                schklom/ttrss-app:latest
          - name: Build and push TTRSS web-nginx
            uses: docker/build-push-action@v2
            with:
              context: ./web-nginx
              file: ./web-nginx/Dockerfile
              platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
              pull: true
              push: true
              tags: |
                schklom/ttrss-web-nginx:latest
    
      build_push_filelogging:
        needs: [build_push]
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v2
            with:
              ref: 'TTRSS-docker-with-filelogging'
          - name: Set up QEMU
            uses: docker/setup-qemu-action@v1
          - name: Set up Docker Buildx
            id: buildx
            uses: docker/setup-buildx-action@v1
          - name: Login to Docker Hub
            uses: docker/login-action@v1
            with:
              username: ${{ secrets.DOCKERHUB_USERNAME }}
              password: ${{ secrets.DOCKERHUB_TOKEN }}
          - name: Build and push TTRSS app (with file logging)
            uses: docker/build-push-action@v2
            with:
              context: ./
              file: ./Dockerfile
              platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
              pull: true
              push: true
              tags: |
                schklom/ttrss-app:with-filelogging-latest