How to use GitHub Script in a GitHub Actions workflow

GitHub Actions is a workflow engine that automates the execution of actions. GitHub Script is one of the actions available for use in a workflow.

There are several ways in which you can use GitHub Script in a GitHub Actions workflow. This article will demystify the ways in which you can include your script code in your GitHub Actions workflow.

Inline Script

You can include your script directly within your workflow file using the script parameter of the GitHub Script action.

This is suitable for small scripts or when the script logic is simple and does not require external files.

- name: Run GitHub Script
  uses: actions/github-script@v5
  with:
    script: |
      // Your inline script code below
      console.log('Hello, AzureMentor!')

External File

If your script logic is more complex or you want to separate your script from your workflow file, you can store the script code in an external file instead of having all your scripts inline.

- name: Run GitHub Script
  uses: actions/github-script@v5
  with:
    // Reference your script file here
    script: |
      // Grab your script from an external file
      const path = require('path')
      const scriptPath = path.resolve('./myFolder/script.js')
      console.log(require(scriptPath)({context}))

Choosing inline script versus external file would depend on factors such as script complexity, code organization, reusability, and maintainability.

Feel free to choose the approach that best suits your requirements for organizing and reference your script code.

Happy scripting!

Leave a Comment