在外部存储库更新(推送)上触发 GitHub 操作

时间:2021-01-07 20:28:30

标签: github github-actions git-push mirroring

我正在尝试镜像一个我不拥有的公共存储库,更重要的是镜像他们的推送(以触发另一个操作)。

现在,我看到的同步操作似乎将一个存储库复制粘贴到我拥有的存储库中,但该存储库的推送不会触发操作。 有没有办法做到这一点?

我不知道外国回购的所有者。我知道所有者可以发送调度事件,但我想要一个不依赖于某人善意的解决方案。

基本上,我希望这发生: 我的仓库每小时与一个外部仓库同步,​​如果过去一小时有更新,则会触发另一个操作。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

对于有同样问题的人,我找到了一种通过 cron 计划的方法:

  1. 在您的存储库中创建一个空文本文件
  2. 检查原始 repo 的提交 ID
  3. 如果该 ID 与文本文件中的 ID 不同,则触发同步操作(以及可选的任何其他操作),然后将提交 ID 添加到一个小文本文件中
  4. 否则,什么都不做
  5. 按照 cron 计划从 2 开始重复

外部存储库的更新将在 cron 计划中触发。

这不是我想要的,但已经足够接近了。

编辑: 这是来自 my repo 的完整 .yml 文件示例,已将其注释掉以使其更短

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