* Empty commit * updated beta note for GHAE * more GHAE update + resolve conflict * more GHAE updates + prepare for screenshots * Apply suggestions from code review Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com> * address remaining review comments * Revise "About GitHub AE" (#17679) * add screenshots to the Configuring article * reworked to have a separate GHAE section * list numbering * more work on screenshots and conditions * add GHAE screenshots in article * review screenshots in article * added more screenshots and updated more articles * screenshot madness * fix liquid versioning * refactor the ghae script * [GHAE CB/Feb 22]: Add article about data residency for GitHub AE (#17847) * add missing GHAE versioning to article * move screenshots to GHAE asset directory * forgot to change the path for these two images * replace CBB screenshot + add better screenshot * [GHAE CB/Feb 22]: Document upgrades for GitHub AE (#17848) * Version article for GitHub AE * Replace unused variable * Incorporate reviewer feedback * Update intro Co-authored-by: Ethan P <56270045+ethanpalm@users.noreply.github.com> * [GHAE] Enable IP allow list (#17691) * Notes for CC * Updat permission leves chart * Add updated article to further reading * Update gated feature callout with GitHub AE * Version "Managing allowed IP addresses for your organization" for AE * Update images * Update "Restricting network traffic to your enterprise" with new procedures * remove todo note * Update audited actions * Update info about Premium Runners * Use reusable for Premium Runners * Change "Premium Runners" to "AE hosted runners" * Incorporate reviewer feedback * Use correct reusable * Version reusable correctly * [Feb 22] GHAE: Code scanning beta (#17830) * Add "github-ae" to all the frontmatter * GHAE-ify the reusables * Add some more changes * Re-use some content * 🔪 Semmle links * Revert change re "--external-repository-token" in the CodeQL runner * Update CodeQL runner token scopes * Update two screenshots * Remove mention of GitHub.com from AE + other fixes * Apply suggestions from code review Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com> * Use `product_name` variable instead of `product_location` * Remove confusing phrase * [Feb 22] GHAE: Code scanning API and webhook docs (#17883) * Version API and webhook docs * Actually add versioning for GHAE * Fix anchor * [TEMPORARY] Preview for API endpoints * Revert API previews * Update procedure step Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com> * Update docs for AzureAD Group SCIM support in GHAE (#17892) * [GHAE CB] SMTP bootstrapping flow (#17888) * draft * update with AE conntent * update with tons of versioning * remove that lie * fill out the rest of these steps * update with correct versioning * more edits * add images * reversion most of ae article * fix versioning * format correctlly * words matter * last image * update with permmissions * update versioning * add link * apply feedback ❤️ * update with differrent spacing * update with feedback * more feedback * Temporary GHAE release notes for consumables beta launch (#17859) * Create release-notes.md * Add frontmatter * Add to index file * Update github-ae-release-notes.md * Add release notes from Google Doc * Update finalized docs links that have been reviewed * OAuth device flow link update * version for AE * few fixes * Update content/admin/overview/github-ae-release-notes.md * small edits * whoops * commit * update with different links * used wrong reusable * fix more brokenness * Update repository-references.js * Update repository-references.js Co-authored-by: Meg Bird <megbird@github.com> Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> * [GHAE] Audit public repos (#17917) * verifying what we mean by public * Apply suggestions from code review * Update content/developers/apps/installing-github-apps.md Co-authored-by: Laura Coursen <lecoursen@github.com> * fixing placememnt of liquid conditional Co-authored-by: Laura Coursen <lecoursen@github.com> * GHAE packages beta (#17786) Co-authored-by: jmarlena <6732600+jmarlena@users.noreply.github.com> Co-authored-by: Martin Lopes <martin389@github.com> * fix broken links * [GHAE CB/March 01]: GitHub Actions on GHAE (beta) (#17725) * Added initial layout for premium runners * Restructured content * Added placeholder for removing premium runner * Added versioning and warning note for self-hosted runners * Added versioning and beta notice for actions content * Rephrased beta note * Added versioning for API docs, fixes * Added versioning fixes * Split Github-hosted and premium topics into separate articles * Added edits * Restructured some topics * Revised "Using premium runners in a workflow" * Some small fixes * Fixed typo * Added fixes to reusable * Added edits * Made section titles consistent * Added billing, group mgmt, reusable steps * Cropped certain screenshots for future-proofing * Removed superfluous reusable * Added fixes * Revert "Cropped certain screenshots for future-proofing" This reverts commit c7f24f31fa30d4fe3de2b63fc3cd5feba44ef518. * Added new section for custom images * Added versioning for enterprise-admin operations * Added edits * Added edits * Update adding-premium-runners.md * Removed SHR screenshots. Intending to update them when UI is available. * Update using-labels-with-premium-runners.md * Added custom labels section * Added preview of API docs changes * Added versioning for ip allow list section * Removed removal article * Renamed premium runners to AE hosted runners * Re-added added API preview * Fixed links, updated software specs * Revised "Software specifications" based on feedback * Fixed typos * Small fixes * Added new article "Creating custom images" * Moved "Creating custom images" link * Apply suggestions from code review Co-authored-by: ahdbilal <55514721+ahdbilal@users.noreply.github.com> * Added update from review * Added updates from tech review * Apply suggestions from code review Co-authored-by: ahdbilal <55514721+ahdbilal@users.noreply.github.com> * Added updates from tech review * Added updates from tech review * Added updates from tech review * Added updates from tech review * Fixed reusable * Added fixes * Added update from tech review * Removed the dereferenced OpenAPI schema files * Added fixes * Fixed links * Fixed links * Apply suggestions from code review Co-authored-by: jmarlena <6732600+jmarlena@users.noreply.github.com> * Added updates from peer review * Removed sections that are not in beta * Update viewing-your-github-actions-usage.md * Update viewing-job-execution-time.md * Update index.md * Update about-github-hosted-runners.md * Restored versioning to match GHES approach * Fixed link * Restored self-hosted runner reference to UI steps. * Updated screenshots * Updated screenshots and procedures * Small edits to screenshots * Added AE url info for SHR * Removed superfluous versioning * Update security-hardening-for-github-actions.md * Update actions-shared.md * Small edits * Update usage-limits-billing-and-administration.md * Update managing-complex-workflows.md * Additional versioning * Additional versioning * version environments api and checkrun deployments for ghae (#17991) Co-authored-by: Martin Lopes <martin389@github.com> * Update reviewing-the-audit-log-for-your-organization.md * Added versioning for enterprise policy settings * version configuring artifact retention for AE * remove AE versioning for connecting to Marketplace * Apply suggestions from code review Co-authored-by: Joe Bourne <thejoebourneidentity@github.com> * Update content/admin/github-actions/getting-started-with-github-actions-for-github-ae.md Co-authored-by: Joe Bourne <thejoebourneidentity@github.com> * rewording not public to private * fixing liquid * Fixed elseif entries * Added expectations note * Revised label management article for AE hosted runners * Added enterprise-admin note for adding AE hosted runners * Update enterprise-admin.md * Update self-hosted-runner-security.md * Versioned reusable for AE * Empty commit for CI Co-authored-by: ahdbilal <55514721+ahdbilal@users.noreply.github.com> Co-authored-by: jmarlena <6732600+jmarlena@users.noreply.github.com> Co-authored-by: skedwards88 <skedwards88@github.com> Co-authored-by: Leona B. Campbell <3880403+runleonarun@users.noreply.github.com> Co-authored-by: Joe Bourne <thejoebourneidentity@github.com> Co-authored-by: runleonarun <runleonarun@github.com> * Update OpenAPI Descriptions for GHAE * Update content/admin/overview/github-ae-release-notes.md Co-authored-by: Ethan Palm <56270045+ethanpalm@users.noreply.github.com> Co-authored-by: mchammer01 <42146119+mchammer01@users.noreply.github.com> Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com> Co-authored-by: shati-patel <shati-patel@github.com> Co-authored-by: Sarah Schneider <sarahs@github.com> Co-authored-by: skedwards88 <skedwards88@github.com> Co-authored-by: Sarah Schneider <sarahs@users.noreply.github.com> Co-authored-by: Melanie Yarbrough <11952755+myarb@users.noreply.github.com> Co-authored-by: Felicity Chapman <felicitymay@github.com> Co-authored-by: Meg Bird <megbird@github.com> Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> Co-authored-by: Leona B. Campbell <3880403+runleonarun@users.noreply.github.com> Co-authored-by: Laura Coursen <lecoursen@github.com> Co-authored-by: jmarlena <6732600+jmarlena@users.noreply.github.com> Co-authored-by: Martin Lopes <martin389@github.com> Co-authored-by: ahdbilal <55514721+ahdbilal@users.noreply.github.com> Co-authored-by: Joe Bourne <thejoebourneidentity@github.com> Co-authored-by: runleonarun <runleonarun@github.com> Co-authored-by: github-openapi-bot <69533958+github-openapi-bot@users.noreply.github.com>
279 lines
12 KiB
Markdown
279 lines
12 KiB
Markdown
---
|
|
title: Creating a JavaScript action
|
|
intro: 'In this guide, you''ll learn how to build a JavaScript action using the actions toolkit.'
|
|
product: '{% data reusables.gated-features.actions %}'
|
|
redirect_from:
|
|
- /articles/creating-a-javascript-action
|
|
- /github/automating-your-workflow-with-github-actions/creating-a-javascript-action
|
|
- /actions/automating-your-workflow-with-github-actions/creating-a-javascript-action
|
|
- /actions/building-actions/creating-a-javascript-action
|
|
versions:
|
|
free-pro-team: '*'
|
|
enterprise-server: '>=2.22'
|
|
github-ae: '*'
|
|
type: 'tutorial'
|
|
topics:
|
|
- 'Action development'
|
|
- 'JavaScript'
|
|
---
|
|
|
|
{% data reusables.actions.enterprise-beta %}
|
|
{% data reusables.actions.enterprise-github-hosted-runners %}
|
|
{% data reusables.actions.ae-beta %}
|
|
|
|
### Introduction
|
|
|
|
In this guide, you'll learn about the basic components needed to create and use a packaged JavaScript action. To focus this guide on the components needed to package the action, the functionality of the action's code is minimal. The action prints "Hello World" in the logs or "Hello [who-to-greet]" if you provide a custom name.
|
|
|
|
This guide uses the {% data variables.product.prodname_actions %} Toolkit Node.js module to speed up development. For more information, see the [actions/toolkit](https://github.com/actions/toolkit) repository.
|
|
|
|
Once you complete this project, you should understand how to build your own JavaScript action and test it in a workflow.
|
|
|
|
{% data reusables.github-actions.pure-javascript %}
|
|
|
|
### Prerequisites
|
|
|
|
Before you begin, you'll need to download Node.js and create a GitHub repository.
|
|
|
|
1. Download and install Node.js 12.x, which includes npm.
|
|
|
|
https://nodejs.org/en/download/current/
|
|
|
|
1. Create a new repository on {% data variables.product.product_location %}. You can choose any repository name or use "hello-world-javascript-action" like this example. You can add these files after your project has been pushed to {% data variables.product.product_name %}. For more information, see "[Create a new repository](/articles/creating-a-new-repository)."
|
|
|
|
1. Clone your repository to your computer. For more information, see "[Cloning a repository](/articles/cloning-a-repository)."
|
|
|
|
1. From your terminal, change directories into your new repository.
|
|
|
|
```shell
|
|
cd hello-world-javascript-action
|
|
```
|
|
|
|
1. From your terminal, initialize the directory with a `package.json` file.
|
|
|
|
```shell
|
|
npm init -y
|
|
```
|
|
|
|
### Creating an action metadata file
|
|
|
|
Create a new file `action.yml` in the `hello-world-javascript-action` directory with the following example code. For more information, see "[Metadata syntax for {% data variables.product.prodname_actions %}](/actions/creating-actions/metadata-syntax-for-github-actions)."
|
|
|
|
|
|
**action.yml**
|
|
```yaml
|
|
name: 'Hello World'
|
|
description: 'Greet someone and record the time'
|
|
inputs:
|
|
who-to-greet: # id of input
|
|
description: 'Who to greet'
|
|
required: true
|
|
default: 'World'
|
|
outputs:
|
|
time: # id of output
|
|
description: 'The time we greeted you'
|
|
runs:
|
|
using: 'node12'
|
|
main: 'index.js'
|
|
```
|
|
|
|
This file defines the `who-to-greet` input and `time` output. It also tells the action runner how to start running this JavaScript action.
|
|
|
|
### Adding actions toolkit packages
|
|
|
|
The actions toolkit is a collection of Node.js packages that allow you to quickly build JavaScript actions with more consistency.
|
|
|
|
The toolkit [`@actions/core`](https://github.com/actions/toolkit/tree/main/packages/core) package provides an interface to the workflow commands, input and output variables, exit statuses, and debug messages.
|
|
|
|
The toolkit also offers a [`@actions/github`](https://github.com/actions/toolkit/tree/main/packages/github) package that returns an authenticated Octokit REST client and access to GitHub Actions contexts.
|
|
|
|
The toolkit offers more than the `core` and `github` packages. For more information, see the [actions/toolkit](https://github.com/actions/toolkit) repository.
|
|
|
|
At your terminal, install the actions toolkit `core` and `github` packages.
|
|
|
|
```shell
|
|
npm install @actions/core
|
|
npm install @actions/github
|
|
```
|
|
|
|
Now you should see a `node_modules` directory with the modules you just installed and a `package-lock.json` file with the installed module dependencies and the versions of each installed module.
|
|
|
|
### Writing the action code
|
|
|
|
This action uses the toolkit to get the `who-to-greet` input variable required in the action's metadata file and prints "Hello [who-to-greet]" in a debug message in the log. Next, the script gets the current time and sets it as an output variable that actions running later in a job can use.
|
|
|
|
GitHub Actions provide context information about the webhook event, Git refs, workflow, action, and the person who triggered the workflow. To access the context information, you can use the `github` package. The action you'll write will print the webhook event payload to the log.
|
|
|
|
Add a new file called `index.js`, with the following code.
|
|
|
|
**index.js**
|
|
```javascript
|
|
const core = require('@actions/core');
|
|
const github = require('@actions/github');
|
|
|
|
try {
|
|
// `who-to-greet` input defined in action metadata file
|
|
const nameToGreet = core.getInput('who-to-greet');
|
|
console.log(`Hello ${nameToGreet}!`);
|
|
const time = (new Date()).toTimeString();
|
|
core.setOutput("time", time);
|
|
// Get the JSON webhook payload for the event that triggered the workflow
|
|
const payload = JSON.stringify(github.context.payload, undefined, 2)
|
|
console.log(`The event payload: ${payload}`);
|
|
} catch (error) {
|
|
core.setFailed(error.message);
|
|
}
|
|
```
|
|
|
|
If an error is thrown in the above `index.js` example, `core.setFailed(error.message);` uses the actions toolkit [`@actions/core`](https://github.com/actions/toolkit/tree/main/packages/core) package to log a message and set a failing exit code. For more information, see "[Setting exit codes for actions](/actions/creating-actions/setting-exit-codes-for-actions)."
|
|
|
|
|
|
### Creating a README
|
|
|
|
To let people know how to use your action, you can create a README file. A README is most helpful when you plan to share your action publicly, but is also a great way to remind you or your team how to use the action.
|
|
|
|
In your `hello-world-javascript-action` directory, create a `README.md` file that specifies the following information:
|
|
|
|
- A detailed description of what the action does.
|
|
- Required input and output arguments.
|
|
- Optional input and output arguments.
|
|
- Secrets the action uses.
|
|
- Environment variables the action uses.
|
|
- An example of how to use your action in a workflow.
|
|
|
|
**README.md**
|
|
```markdown
|
|
# Hello world javascript action
|
|
|
|
This action prints "Hello World" or "Hello" + the name of a person to greet to the log.
|
|
|
|
## Inputs
|
|
|
|
### `who-to-greet`
|
|
|
|
**Required** The name of the person to greet. Default `"World"`.
|
|
|
|
## Outputs
|
|
|
|
### `time`
|
|
|
|
The time we greeted you.
|
|
|
|
## Example usage
|
|
|
|
uses: actions/hello-world-javascript-action@v1.1
|
|
with:
|
|
who-to-greet: 'Mona the Octocat'
|
|
```
|
|
|
|
### Commit, tag, and push your action to GitHub
|
|
|
|
{% data variables.product.product_name %} downloads each action run in a workflow during runtime and executes it as a complete package of code before you can use workflow commands like `run` to interact with the runner machine. This means you must include any package dependencies required to run the JavaScript code. You'll need to check in the toolkit `core` and `github` packages to your action's repository.
|
|
|
|
From your terminal, commit your `action.yml`, `index.js`, `node_modules`, `package.json`, `package-lock.json`, and `README.md` files. If you added a `.gitignore` file that lists `node_modules`, you'll need to remove that line to commit the `node_modules` directory.
|
|
|
|
It's best practice to also add a version tag for releases of your action. For more information on versioning your action, see "[About actions](/actions/automating-your-workflow-with-github-actions/about-actions#using-release-management-for-actions)."
|
|
|
|
|
|
```shell
|
|
git add action.yml index.js node_modules/* package.json package-lock.json README.md
|
|
git commit -m "My first action is ready"
|
|
git tag -a -m "My first action release" v1
|
|
git push --follow-tags
|
|
```
|
|
|
|
Checking in your `node_modules` directory can cause problems. As an alternative, you can use a tool called [`@vercel/ncc`](https://github.com/vercel/ncc) to compile your code and modules into one file used for distribution.
|
|
|
|
1. Install `vercel/ncc` by running this command in your terminal.
|
|
`npm i -g @vercel/ncc`
|
|
|
|
1. Compile your `index.js` file.
|
|
`ncc build index.js --license licenses.txt`
|
|
|
|
You'll see a new `dist/index.js` file with your code and the compiled modules.
|
|
You will also see an accompanying `dist/licenses.txt` file containing all the licenses of the `node_modules` you are using.
|
|
|
|
1. Change the `main` keyword in your `action.yml` file to use the new `dist/index.js` file.
|
|
`main: 'dist/index.js'`
|
|
|
|
1. If you already checked in your `node_modules` directory, remove it.
|
|
`rm -rf node_modules/*`
|
|
|
|
1. From your terminal, commit the updates to your `action.yml`, `dist/index.js`, and `node_modules` files.
|
|
```shell
|
|
git add action.yml dist/index.js node_modules/*
|
|
git commit -m "Use vercel/ncc"
|
|
git tag -a -m "My first action release" v1
|
|
git push --follow-tags
|
|
```
|
|
|
|
### Testing out your action in a workflow
|
|
|
|
Now you're ready to test your action out in a workflow. When an action is in a private repository, the action can only be used in workflows in the same repository. Public actions can be used by workflows in any repository.
|
|
|
|
{% data reusables.actions.enterprise-marketplace-actions %}
|
|
|
|
#### Example using a public action
|
|
|
|
The following workflow code uses the completed hello world action in the `actions/hello-world-javascript-action` repository. Copy the workflow code into a `.github/workflows/main.yml` file, but replace the `actions/hello-world-javascript-action` repository with the repository you created. You can also replace the `who-to-greet` input with your name.
|
|
|
|
{% raw %}
|
|
**.github/workflows/main.yml**
|
|
```yaml
|
|
on: [push]
|
|
|
|
jobs:
|
|
hello_world_job:
|
|
runs-on: ubuntu-latest
|
|
name: A job to say hello
|
|
steps:
|
|
- name: Hello world action step
|
|
id: hello
|
|
uses: actions/hello-world-javascript-action@v1.1
|
|
with:
|
|
who-to-greet: 'Mona the Octocat'
|
|
# Use the output from the `hello` step
|
|
- name: Get the output time
|
|
run: echo "The time was ${{ steps.hello.outputs.time }}"
|
|
```
|
|
{% endraw %}
|
|
|
|
#### Example using a private action
|
|
|
|
Copy the workflow code into a `.github/workflows/main.yml` file in your action's repository. You can also replace the `who-to-greet` input with your name.
|
|
|
|
{% raw %}
|
|
**.github/workflows/main.yml**
|
|
```yaml
|
|
on: [push]
|
|
|
|
jobs:
|
|
hello_world_job:
|
|
runs-on: ubuntu-latest
|
|
name: A job to say hello
|
|
steps:
|
|
# To use this repository's private action,
|
|
# you must check out the repository
|
|
- name: Checkout
|
|
uses: actions/checkout@v2
|
|
- name: Hello world action step
|
|
uses: ./ # Uses an action in the root directory
|
|
id: hello
|
|
with:
|
|
who-to-greet: 'Mona the Octocat'
|
|
# Use the output from the `hello` step
|
|
- name: Get the output time
|
|
run: echo "The time was ${{ steps.hello.outputs.time }}"
|
|
```
|
|
{% endraw %}
|
|
|
|
From your repository, click the **Actions** tab, and select the latest workflow run. {% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@3.0" or currentVersion == "github-ae@latest" %}Under **Jobs** or in the visualization graph, click **A job to say hello**. {% endif %}You should see "Hello Mona the Octocat" or the name you used for the `who-to-greet` input and the timestamp printed in the log.
|
|
|
|
{% if currentVersion == "free-pro-team@latest" or currentVersion ver_gt "enterprise-server@3.0" or currentVersion == "github-ae@latest" %}
|
|

|
|
{% elsif currentVersion ver_gt "enterprise-server@2.22" %}
|
|

|
|
{% else %}
|
|

|
|
{% endif %}
|