Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting file contents to Issue with Github scripts

According to Github scripts docs, I can post to a Github issue like,

jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '👋 Thanks for reporting!'
            })

I want to post a body that reads data from a file. I tried,

body: fs.readFileSync('/my/cool/file')

but it complains that fs does not exist.

How do I do this?


1 Answers

Install the fs module and use require('fs') before using the fs module.

require A proxy wrapper around the normal Node.js require to enable requiring relative paths (relative to the current working directory) and requiring npm packages installed in the current working directory

source

jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
    - run: npm install fs
    - uses: actions/github-script@v3
      with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const fs = require('fs')
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: fs.readFileSync('/my/cool/file', 'utf8')
            })

like image 155
riQQ Avatar answered Sep 13 '25 18:09

riQQ