{"id":13433045,"url":"https://github.com/actions/github-script","last_synced_at":"2025-05-12T03:38:12.653Z","repository":{"id":37391059,"uuid":"205262760","full_name":"actions/github-script","owner":"actions","description":"Write workflows scripting the GitHub API in JavaScript","archived":false,"fork":false,"pushed_at":"2025-05-05T18:42:59.000Z","size":2049,"stargazers_count":4494,"open_issues_count":59,"forks_count":456,"subscribers_count":38,"default_branch":"main","last_synced_at":"2025-05-09T20:07:17.004Z","etag":null,"topics":["actions","github-api","javascript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/actions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-08-29T22:46:51.000Z","updated_at":"2025-05-09T10:49:15.000Z","dependencies_parsed_at":"2024-08-25T21:06:38.414Z","dependency_job_id":"9f2200a1-4e74-4394-88e9-30745cf9263b","html_url":"https://github.com/actions/github-script","commit_stats":{"total_commits":309,"total_committers":58,"mean_commits":5.327586206896552,"dds":0.6537216828478964,"last_synced_commit":"4020e461acd7a80762cdfff123a1fde368246fa4"},"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actions%2Fgithub-script","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actions%2Fgithub-script/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actions%2Fgithub-script/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actions%2Fgithub-script/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/actions","download_url":"https://codeload.github.com/actions/github-script/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253377130,"owners_count":21898934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actions","github-api","javascript"],"created_at":"2024-07-31T02:01:20.237Z","updated_at":"2025-05-12T03:38:12.629Z","avatar_url":"https://github.com/actions.png","language":"TypeScript","readme":"# actions/github-script\n\n[![Integration](https://github.com/actions/github-script/actions/workflows/integration.yml/badge.svg?branch=main\u0026event=push)](https://github.com/actions/github-script/actions/workflows/integration.yml)\n[![CI](https://github.com/actions/github-script/actions/workflows/ci.yml/badge.svg?branch=main\u0026event=push)](https://github.com/actions/github-script/actions/workflows/ci.yml)\n[![Licensed](https://github.com/actions/github-script/actions/workflows/licensed.yml/badge.svg?branch=main\u0026event=push)](https://github.com/actions/github-script/actions/workflows/licensed.yml)\n\nThis action makes it easy to quickly write a script in your workflow that\nuses the GitHub API and the workflow run context.\n\nTo use this action, provide an input named `script` that contains the body of an asynchronous JavaScript function call.\nThe following arguments will be provided:\n\n- `github` A pre-authenticated\n  [octokit/rest.js](https://octokit.github.io/rest.js) client with pagination plugins\n- `context` An object containing the [context of the workflow\n  run](https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts)\n- `core` A reference to the [@actions/core](https://github.com/actions/toolkit/tree/main/packages/core) package\n- `glob` A reference to the [@actions/glob](https://github.com/actions/toolkit/tree/main/packages/glob) package\n- `io` A reference to the [@actions/io](https://github.com/actions/toolkit/tree/main/packages/io) package\n- `exec` A reference to the [@actions/exec](https://github.com/actions/toolkit/tree/main/packages/exec) package\n- `require` A proxy wrapper around the normal Node.js `require` to enable\n  requiring relative paths (relative to the current working directory) and\n  requiring npm packages installed in the current working directory. If for\n  some reason you need the non-wrapped `require`, there is an escape hatch\n  available: `__original_require__` is the original value of `require` without\n  our wrapping applied.\n\nSince the `script` is just a function body, these values will already be\ndefined, so you don't have to import them (see examples below).\n\nSee [octokit/rest.js](https://octokit.github.io/rest.js/) for the API client\ndocumentation.\n\n## Breaking Changes\n\n### V7\n\nVersion 7 of this action updated the runtime to Node 20 - https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-javascript-actions\n\nAll scripts are now run with Node 20 instead of Node 16 and are affected by any breaking changes between Node 16 and 20\n\nThe `previews` input now only applies to GraphQL API calls as REST API previews are no longer necessary - https://github.blog/changelog/2021-10-14-rest-api-preview-promotions/.\n\n### V6\n\nVersion 6 of this action updated the runtime to Node 16 - https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-javascript-actions\n\nAll scripts are now run with Node 16 instead of Node 12 and are affected by any breaking changes between Node 12 and 16.\n\n### V5\n\nVersion 5 of this action includes the version 5 of `@actions/github` and `@octokit/plugin-rest-endpoint-methods`. As part of this update, the Octokit context available via `github` no longer has REST methods directly. These methods are available via `github.rest.*` - https://github.com/octokit/plugin-rest-endpoint-methods.js/releases/tag/v5.0.0\n\nFor example, `github.issues.createComment` in V4 becomes `github.rest.issues.createComment` in V5\n\n`github.request`, `github.paginate`, and `github.graphql` are unchanged.\n\n## Development\n\nSee [development.md](/docs/development.md).\n\n## Reading step results\n\nThe return value of the script will be in the step's outputs under the\n\"result\" key.\n\n```yaml\n- uses: actions/github-script@v7\n  id: set-result\n  with:\n    script: return \"Hello!\"\n    result-encoding: string\n- name: Get result\n  run: echo \"${{steps.set-result.outputs.result}}\"\n```\n\nSee [\"Result encoding\"](#result-encoding) for details on how the encoding of\nthese outputs can be changed.\n\n## Result encoding\n\nBy default, the JSON-encoded return value of the function is set as the \"result\" in the\noutput of a github-script step. For some workflows, string encoding is preferred. This option can be set using the\n`result-encoding` input:\n\n```yaml\n- uses: actions/github-script@v7\n  id: my-script\n  with:\n    result-encoding: string\n    script: return \"I will be string (not JSON) encoded!\"\n```\n\n## Retries\n\nBy default, requests made with the `github` instance will not be retried. You can configure this with the `retries` option:\n\n```yaml\n- uses: actions/github-script@v7\n  id: my-script\n  with:\n    result-encoding: string\n    retries: 3\n    script: |\n      github.rest.issues.get({\n        issue_number: context.issue.number,\n        owner: context.repo.owner,\n        repo: context.repo.repo,\n      })\n```\n\nIn this example, request failures from `github.rest.issues.get()` will be retried up to 3 times.\n\nYou can also configure which status codes should be exempt from retries via the `retry-exempt-status-codes` option:\n\n```yaml\n- uses: actions/github-script@v7\n  id: my-script\n  with:\n    result-encoding: string\n    retries: 3\n    retry-exempt-status-codes: 400,401\n    script: |\n      github.rest.issues.get({\n        issue_number: context.issue.number,\n        owner: context.repo.owner,\n        repo: context.repo.repo,\n      })\n```\n\nBy default, the following status codes will not be retried: `400, 401, 403, 404, 422` [(source)](https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14).\n\nThese retries are implemented using the [octokit/plugin-retry.js](https://github.com/octokit/plugin-retry.js) plugin. The retries use [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) to space out retries. ([source](https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/error-request.ts#L13))\n\n## Examples\n\nNote that `github-token` is optional in this action, and the input is there\nin case you need to use a non-default token.\n\nBy default, github-script will use the token provided to your workflow.\n\n### Print the available attributes of context\n\n```yaml\n- name: View context attributes\n  uses: actions/github-script@v7\n  with:\n    script: console.log(context)\n```\n\n### Comment on an issue\n\n```yaml\non:\n  issues:\n    types: [opened]\n\njobs:\n  comment:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            github.rest.issues.createComment({\n              issue_number: context.issue.number,\n              owner: context.repo.owner,\n              repo: context.repo.repo,\n              body: '👋 Thanks for reporting!'\n            })\n```\n\n### Apply a label to an issue\n\n```yaml\non:\n  issues:\n    types: [opened]\n\njobs:\n  apply-label:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            github.rest.issues.addLabels({\n              issue_number: context.issue.number,\n              owner: context.repo.owner,\n              repo: context.repo.repo,\n              labels: ['Triage']\n            })\n```\n\n### Welcome a first-time contributor\n\nYou can format text in comments using the same [Markdown syntax](https://docs.github.com/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax) as the GitHub web interface:\n\n```yaml\non: pull_request_target\n\njobs:\n  welcome:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            // Get a list of all issues created by the PR opener\n            // See: https://octokit.github.io/rest.js/#pagination\n            const creator = context.payload.sender.login\n            const opts = github.rest.issues.listForRepo.endpoint.merge({\n              ...context.issue,\n              creator,\n              state: 'all'\n            })\n            const issues = await github.paginate(opts)\n\n            for (const issue of issues) {\n              if (issue.number === context.issue.number) {\n                continue\n              }\n\n              if (issue.pull_request) {\n                return // Creator is already a contributor.\n              }\n            }\n\n            await github.rest.issues.createComment({\n              issue_number: context.issue.number,\n              owner: context.repo.owner,\n              repo: context.repo.repo,\n              body: `**Welcome**, new contributor!\n\n                Please make sure you've read our [contributing guide](CONTRIBUTING.md) and we look forward to reviewing your Pull request shortly ✨`\n            })\n```\n\n### Download data from a URL\n\nYou can use the `github` object to access the Octokit API. For\ninstance, `github.request`\n\n```yaml\non: pull_request\n\njobs:\n  diff:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const diff_url = context.payload.pull_request.diff_url\n            const result = await github.request(diff_url)\n            console.log(result)\n```\n\n_(Note that this particular example only works for a public URL, where the\ndiff URL is publicly accessible. Getting the diff for a private URL requires\nusing the API.)_\n\nThis will print the full diff object in the screen; `result.data` will\ncontain the actual diff text.\n\n### Run custom GraphQL queries\n\nYou can use the `github.graphql` object to run custom GraphQL queries against the GitHub API.\n\n```yaml\njobs:\n  list-issues:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const query = `query($owner:String!, $name:String!, $label:String!) {\n              repository(owner:$owner, name:$name){\n                issues(first:100, labels: [$label]) {\n                  nodes {\n                    id\n                  }\n                }\n              }\n            }`;\n            const variables = {\n              owner: context.repo.owner,\n              name: context.repo.repo,\n              label: 'wontfix'\n            }\n            const result = await github.graphql(query, variables)\n            console.log(result)\n```\n\n### Run a separate file\n\nIf you don't want to inline your entire script that you want to run, you can\nuse a separate JavaScript module in your repository like so:\n\n```yaml\non: push\n\njobs:\n  echo-input:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const script = require('./path/to/script.js')\n            console.log(script({github, context}))\n```\n\nAnd then export a function from your module:\n\n```javascript\nmodule.exports = ({github, context}) =\u003e {\n  return context.payload.client_payload.value\n}\n```\n\nNote that because you can't `require` things like the GitHub context or\nActions Toolkit libraries, you'll want to pass them as arguments to your\nexternal function.\n\nAdditionally, you'll want to use the [checkout\naction](https://github.com/actions/checkout) to make sure your script file is\navailable.\n\n### Run a separate file with an async function\n\nYou can also use async functions in this manner, as long as you `await` it in\nthe inline script.\n\nIn your workflow:\n\n```yaml\non: push\n\njobs:\n  echo-input:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/github-script@v7\n        env:\n          SHA: '${{env.parentSHA}}'\n        with:\n          script: |\n            const script = require('./path/to/script.js')\n            await script({github, context, core})\n```\n\nAnd then export an async function from your module:\n\n```javascript\nmodule.exports = async ({github, context, core}) =\u003e {\n  const {SHA} = process.env\n  const commit = await github.rest.repos.getCommit({\n    owner: context.repo.owner,\n    repo: context.repo.repo,\n    ref: `${SHA}`\n  })\n  core.exportVariable('author', commit.data.commit.author.email)\n}\n```\n\n### Use npm packages\n\nLike importing your own files above, you can also use installed modules.\nNote that this is achieved with a wrapper on top `require`, so if you're\ntrying to require a module inside your own file, you might need to import\nit externally or pass the `require` wrapper to your file:\n\n```yaml\non: push\n\njobs:\n  echo-input:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/setup-node@v4\n        with:\n          node-version: '20.x'\n      - run: npm ci\n      # or one-off:\n      - run: npm install execa\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const execa = require('execa')\n\n            const { stdout } = await execa('echo', ['hello', 'world'])\n\n            console.log(stdout)\n```\n\n### Use ESM `import`\n\nTo import an ESM file, you'll need to reference your script by an absolute path and ensure you have a `package.json` file with `\"type\": \"module\"` specified.\n\nFor a script in your repository `src/print-stuff.js`:\n\n```js\nexport default function printStuff() {\n  console.log('stuff')\n}\n```\n\n```yaml\non: push\n\njobs:\n  print-stuff:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const { default: printStuff } = await import('${{ github.workspace }}/src/print-stuff.js')\n\n            await printStuff()\n```\n\n### Use scripts with jsDoc support\n\nIf you want type support for your scripts, you could use the command below to install the\n`@actions/github-script` type declaration.\n```sh\n$ npm i -D @actions/github-script@github:actions/github-script\n```\n\nAnd then add the `jsDoc` declaration to your script like this:\n```js\n// @ts-check\n/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */\nexport default async ({ core, context }) =\u003e {\n  core.debug(\"Running something at the moment\");\n  return context.actor;\n};\n```\n\n### Use env as input\n\nYou can set env vars to use them in your script:\n\n```yaml\non: push\n\njobs:\n  echo-input:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        env:\n          FIRST_NAME: Mona\n          LAST_NAME: Octocat\n        with:\n          script: |\n            const { FIRST_NAME, LAST_NAME } = process.env\n\n            console.log(`Hello ${FIRST_NAME} ${LAST_NAME}`)\n```\n\n### Using a separate GitHub token\n\nThe `GITHUB_TOKEN` used by default is scoped to the current repository, see [Authentication in a workflow](https://docs.github.com/actions/reference/authentication-in-a-workflow).\n\nIf you need access to a different repository or an API that the `GITHUB_TOKEN` doesn't have permissions to, you can provide your own [PAT](https://help.github.com/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) as a secret using the `github-token` input.\n\n[Learn more about creating and using encrypted secrets](https://docs.github.com/actions/reference/encrypted-secrets)\n\n```yaml\non:\n  issues:\n    types: [opened]\n\njobs:\n  apply-label:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/github-script@v7\n        with:\n          github-token: ${{ secrets.MY_PAT }}\n          script: |\n            github.rest.issues.addLabels({\n              issue_number: context.issue.number,\n              owner: context.repo.owner,\n              repo: context.repo.repo,\n              labels: ['Triage']\n            })\n```\n\n### Using exec package\n\nThe provided [@actions/exec](https://github.com/actions/toolkit/tree/main/packages/exec) package allows to execute command or tools in a cross platform way:\n\n```yaml\non: push\n\njobs:\n  use-exec:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const exitCode = await exec.exec('echo', ['hello'])\n\n            console.log(exitCode)\n```\n\n`exec` packages provides `getExecOutput` function to retrieve stdout and stderr from executed command:\n\n```yaml\non: push\n\njobs:\n  use-get-exec-output:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n      - uses: actions/github-script@v7\n        with:\n          script: |\n            const {\n              exitCode,\n              stdout,\n              stderr\n            } = await exec.getExecOutput('echo', ['hello']);\n\n            console.log(exitCode, stdout, stderr)\n```      \n","funding_links":[],"categories":["TypeScript","Official Resources","Related","github-api","javascript","Building","二、核心官方Action（工作流必备）"],"sub_categories":["Official Actions","Workflows","1. 工作流基础工具"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factions%2Fgithub-script","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Factions%2Fgithub-script","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factions%2Fgithub-script/lists"}