on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 Thanks for reporting!'
})
github-script
Write workflows scripting the GitHub API in JavaScript
What is GitHub Script Action?
This action simplifies the execution of scripts that interact with the GitHub API and the context of our workflow runs.
Key Features of the github-script Action:
Simplicity: You only need to provide a script, and it automatically handles the execution environment with a pre-authenticated GitHub client and other utilities.
Access to Utilities: The action provides pre-configured access to several GitHub tools and libraries, such as Octokit (GitHub's official client library), which helps in handling API pagination and other tasks.
Why do we use GitHub Script Action?
- Automation: Automate repetitive tasks like tagging issues, commenting, or updating labels.
- Flexibility: Write custom scripts to interact with almost any part of GitHub's extensive API.
- Efficiency: Reduce manual intervention and streamline our development processes.
By integrating this action into our workflows, we can significantly improve the efficiency of our project management and CI/CD pipelines
Certainly! Here's the detailed explanation including the code snippets for each action:
How to Print the Available Attributes of Context?
This action logs all the available attributes of the current GitHub workflow context. It's useful for debugging and understanding the environment your scripts are running in.
The action logs the complete context to the console.
- name: View context attributes
uses: actions/github-script@v7
with:
script: console.log(context)
How to Comment on an Issue?
Automatically comments on newly opened issues to acknowledge the reporter. It helps in engaging with contributors promptly.
On the trigger of a new issue being opened, this script comments '👋 Thanks for reporting!' on the issue.
on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 Thanks for reporting!'
})
How to Apply a Label to an Issue with GitHub Action?
Automatically adds a label to newly opened issues. This can be used for initial triage of incoming issues.
When a new issue is opened, this script automatically adds the 'Triage' label to it.
on:
issues:
types: [opened]
jobs:
apply-label:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['Triage']
})
How to Welcome a First-Time Contributor?
Sends a welcoming comment to first-time contributors when they open their first pull request. This enhances the community experience.
This script checks if the pull request opener has opened issues before. If not, it posts a welcoming message, encouraging them to read the contributing guide.
on: pull_request_target
jobs:
welcome:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
// Get a list of all issues created by the PR opener
const creator = context.payload.sender.login
const opts = github.rest.issues.listForRepo.endpoint.merge({
...context.issue,
creator,
state: 'all'
})
const issues = await github.paginate(opts)
for (const issue of issues) {
if (issue.number === context.issue.number) {
continue
}
if (issue.pull_request) {
return // Creator is already a contributor.
}
}
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `**Welcome**, new contributor! Please make sure you've read our [contributing guide](CONTRIBUTING.md) and we look forward to reviewing your Pull request shortly ✨`
})
How to Download Data from a URL using GitHub Action?
Fetches data from a specified URL using a GitHub action. This is useful for integrating external data directly into your workflow. For pull requests, this script fetches the diff URL provided in the payload and logs it. Note: Works only if the diff URL is publicly accessible.
on: pull_request
jobs:
diff:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const diff_url = context.payload.pull_request.diff_url
const result = await github.request(diff_url)
console.log(result)
These examples show the power and flexibility of using GitHub Actions with scripts to automate various parts of your GitHub workflow. Each section now includes the appropriate YAML configuration snippet alongside the plain English explanation.
Certainly, here's the continuation with more examples:
How to Run Custom GraphQL Queries in GitHub Actions?
Executes a GraphQL query to fetch data from GitHub, allowing for complex data retrieval tailored to your needs.
This script runs a GraphQL query to fetch 100 issues with the label 'wontfix' from the repository, and logs the results.
jobs:
list-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const query = `query($owner:String!, $name:String!, $label:String!) {
repository(owner:$owner, name:$name){
issues(first:100, labels: [$label]) {
nodes {
id
}
}
}
}`;
const variables = {
owner: context.repo.owner,
name: context.repo.repo,
label: 'wontfix'
}
const result = await github.graphql(query, variables)
console.log(result)
How to Run a Separate JavaScript File in GitHub Actions?
Enables the execution of a separate JavaScript file within the GitHub Action, making your workflows more modular.
This script imports a separate JavaScript file and logs the output of a function defined in that file.
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/github-script@v7
with:
script: |
const script = require('./path/to/script.js')
console.log(script({github, context}))
How to Run a Separate File with an Async Function in GitHub Actions?
Demonstrates running an external JavaScript file containing an asynchronous function within a GitHub Action.
The script requires an external module that performs an asynchronous operation and awaits its completion.
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/github-script@v7
env:
SHA: '${{env.parentSHA}}'
with:
script: |
const script = require('./path/to/script.js')
await script({github, context, core})
How to Use npm Packages in GitHub Actions?
Shows how to use npm packages in your GitHub Actions to leverage a vast range of functionalities available through Node.js packages.
This script uses the npm package 'execa' to run a command line operation that echoes 'hello world' and logs the output.
on: push
jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '20.x'
- run: npm ci
# or one-off:
- run: npm install execa
- uses: actions/github-script@v7
with:
script: |
const execa = require('execa')
const { stdout } = await execa('echo', ['hello', 'world'])
console.log(stdout)
How to Use ESM Import in GitHub Actions
Illustrates how to use ECMAScript modules in your GitHub Actions scripts, allowing for modern JavaScript syntax and imports.
This script uses ESM import to include and run a function from a local module that prints 'stuff'.
on: push
jobs:
print-stuff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/github-script@v7
with:
script: |
const { default: printStuff } = await import('${{ github.workspace }}/src/print-stuff.js')
await printStuff()
These examples continue to demonstrate the flexibility and power of using GitHub Actions with custom scripts to automate tasks, interact with the GitHub API, and integrate external functionalities into your GitHub workflows. Each example includes both a YAML snippet that provides the configuration and a plain English description to explain what each script does.