简体   繁体   English

将文件添加或更新到 Github 存储库时调用外部 Rest api

[英]Call external Rest api when a file is added or updated to a Github repository

So, I have a Github repository where some files will be added or updated manually or through a merge from feature branch to main branch.所以,我有一个Github 存储库,其中一些文件将被手动添加或更新,或者通过从功能分支到主分支的合并。 I want to know how can I call another external microservice Rest api endpoint (.Net 6.0 endpoint), whenever files are added/updated to this Github repository or merged from feature branch to main branch?我想知道每当将文件添加/更新到此 Github 存储库或从功能分支合并到主分支时,如何调用另一个外部微服务 Rest api 端点(.Net 6.0 端点)?

These are custom files (consider.txt files) which also need to be sent to the external microservice api endpoint so that this endpoint can process received files and save the data to DB.这些是自定义文件(consider.txt 文件) ,它们也需要发送到外部微服务 api 端点,以便该端点可以处理接收到的文件并将数据保存到数据库。

Can someone pls help in solution to above question so that I can push the modified/added files from Github repo to call Rest api endpoint?有人可以帮助解决上述问题,以便我可以从 Github repo 推送修改/添加的文件来调用 Rest api 端点吗?

I looked into this link for solution but could not understand it?我查看了此链接以寻求解决方案,但无法理解? Can I use Github actions for solution to this problem (and how?) or is there any alternate solution?我可以使用 Github 操作来解决这个问题(以及如何?)或者是否有任何替代解决方案? Any link to solution will also help... Thanks..解决方案的任何链接也将有所帮助...谢谢..

A possible workflow in this case:这种情况下可能的工作流程:

  • trigger on files push (including paths and branches filter)触发文件推送(包括路径和分支过滤器)
  • prepare data about added and modified files using the file-changes-action使用file-changes-action准备有关添加和修改文件的数据
  • run a matrix based on the previous job output and call an external API endpoint using the http-request-action and passing values from the matrix.根据之前的作业输出运行一个矩阵,并使用http-request-action调用外部 API 端点并传递矩阵中的值。

Example:例子:

name: 'Call REST API when a file is pushed'

on:
  push:
    paths:
      - '*.txt' # match '*.txt' files (created or updated)
    branches: [ main ] # match specific branch

jobs:
  prepare-files:
    runs-on: ubuntu-latest
    outputs:
      matrix-added: ${{ steps.file-changes.outputs.files_added }}
      matrix-modified: ${{ steps.file-changes.outputs.files_modified }}
    steps:
      - id: file-changes
        uses: trilom/file-changes-action@v1.2.4

  post-files-added:
    needs: prepare-files
    runs-on: ubuntu-latest
    strategy:
      matrix:
        file: ${{ fromJSON(needs.prepare-files.outputs.matrix-added) }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Echo file
        run: |
          echo ${{ matrix.file }} # to test if the correct file was passed

      - name: Make an API call
        uses: fjogeleit/http-request-action@v1
        with:
          url: 'https://postman-echo.com/post'
          method: 'POST'
          file: "${{ github.workspace }}/${{ matrix.file }}"

    # post-files-modified:
    # ...

Additional references:其他参考资料:

Also, there is another possible approach - you can set up a GitHub webhook for your repo that will trigger an API endpoint on the push event.此外,还有另一种可能的方法——您可以为您的存储库设置一个 GitHub webhook,它将在推送事件上触发 API 端点。 For more details, see Automatically copy pushed files from one GitHub repository to another (this example is about copying files to another repository, but the same approach could be used in your case)有关详细信息,请参阅自动将推送的文件从一个 GitHub 存储库复制到另一个(此示例是关于将文件复制到另一个存储库,但您的情况可以使用相同的方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取上次从 Github 存储库更新文件的时间 - Get when the file was last updated from a Github repository 知道何时将一行代码添加到GitHub存储库 - Know when a line of code was added to a GitHub repository github存储库中的json文件需要时间更新吗? - Does the json file in a github repository require time to be updated? 如何使用 GitHub Rest API 在 ZA7F5F35426B9274187873Z 中自动从存储库中提取? - How to automate pulling from a repository with GitHub Rest API in Python? 无法使用 GitHub REST API 在存储库或组织中创建项目 - Unable to create project in repository or organisation using GitHub REST API 我们如何使用 GitHub Rest ZDB974238714801DE634A7CED1 使用模板在 GitHub 中创建一个存储库以及其他存储库内容 - How can we create a Repository in GitHub with other repository content with template using GitHub Rest API 使用 Azure devops REST API 获取 github 存储库 - Get github repository using Azure devops REST API 分叉在GitHub中更新的存储库 - Forking a repository that got updated in GitHub 如何使用 PHP 和 Github API 在 Github 存储库中创建和更新文件? - How to create and update a file in a Github repository with PHP and Github API? 创建存储库时GitHub覆盖了本地文件 - GitHub overwrote a local file when creating a repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM