0

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. 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?

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.

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?

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? Any link to solution will also help... Thanks..

S2S2
  • 8,322
  • 5
  • 37
  • 65

1 Answers1

1

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
  • 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.

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. 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)

Andrii Bodnar
  • 1,672
  • 2
  • 17
  • 24