2019-08-01 15:26:17 +00:00
# Creating a JavaScript Action
2019-08-09 19:06:50 +00:00
The [javascript-template ](https://github.com/actions/javascript-template ) repo contains everything you need to get started.
2019-08-01 15:26:17 +00:00
# Create a Repo from the Template
2019-08-09 19:06:50 +00:00
Navigate to https://github.com/actions/javascript-template
2019-08-01 15:26:17 +00:00
2019-09-04 21:30:45 +00:00
Click on `Use this template` to create the repo for your action. Provide a name such as `myaction` (used in rest of walk through).
2019-08-01 15:26:17 +00:00
![template ](assets/node12-template.png )
2019-09-04 21:30:45 +00:00
# Clone and Update
2019-08-01 15:26:17 +00:00
2019-09-04 21:30:45 +00:00
```bash
$ git clone < repolocation >
$ cd myaction
2019-08-01 15:26:17 +00:00
```
2019-09-04 21:30:45 +00:00
Update the `author` element in the package.json file.
2019-08-03 16:15:05 +00:00
# Dev Workflow
2019-08-01 15:26:17 +00:00
2019-08-03 16:15:05 +00:00
The workflow below describes one possible workflow with a branching strategy. Others exist.
2019-09-04 21:30:45 +00:00
> Key Point: the branch that users reference in their workflow files should reference an action from a distribution branch that **only** has the production dependencies.
2019-08-03 16:15:05 +00:00
2019-09-04 21:30:45 +00:00
The workflow below describes a strategy where you code in master (with node_modules ignored) with a distribution releases/v1 branch users reference via a tag. Actions are self contained referenced from the github graph of repos, downloaded by the runner and run intact at runtime.
2019-08-03 16:15:05 +00:00
## Install Dependencies
2019-09-04 21:30:45 +00:00
After creating a repo from the template and cloning it, you will be in master. The command below will install the toolkit, other dependencies and dev dependencies. node_modules are ignored in the coding master branch.
2019-08-01 15:26:17 +00:00
```bash
$ npm install
```
2019-08-03 16:15:05 +00:00
## Define Metadata
2019-08-01 15:26:17 +00:00
2019-09-04 21:30:45 +00:00
Your action has a name and a description. Update all fields .
2019-08-01 15:26:17 +00:00
```yaml
2019-09-04 21:30:45 +00:00
name: 'Hello'
description: 'Outputs Hello to a named input'
author: 'me'
2019-08-01 15:26:17 +00:00
inputs:
2019-09-04 21:30:45 +00:00
name:
description: 'the name to say hello to'
default: 'World'
2019-08-01 15:26:17 +00:00
runs:
using: 'node12'
main: 'lib/main.js'
```
2019-09-04 21:30:45 +00:00
The `name` input will be referenced by workflow authors using the `with:` keyword.
2019-08-01 15:26:17 +00:00
Note that the action will be run with node 12 (carried by the runner) and the entry point is specified with `main:`
2019-08-03 16:15:05 +00:00
## Change Code and Add Tests
2019-08-01 15:26:17 +00:00
The entry point is in main.ts
```typescript
import * as core from '@actions/core';
async function run() {
try {
2019-09-04 21:30:45 +00:00
const nameInput = core.getInput('name');
console.log(`Hello ${nameInput}!`);
2019-08-01 15:26:17 +00:00
} catch (error) {
core.setFailed(error.message);
}
}
run();
```
2019-09-04 21:30:45 +00:00
Note that tests are in `__tests__/main.test.ts` . The template uses [jest ](https://github.com/facebook/jest ) to get you started with unit testing.
2019-08-01 15:26:17 +00:00
2019-08-03 16:15:05 +00:00
## Build and Test
2019-08-01 15:26:17 +00:00
```bash
$ npm run build
2019-09-04 21:30:45 +00:00
> javascript-template-action@0.0.0 build /Users/user/Projects/myaction
2019-08-01 15:26:17 +00:00
> tsc
$ npm test
> jest
PASS __tests__ /main.test.ts
TODO - Add a test suite
✓ TODO - Add a test (1ms)
Test Suites: 1 passed, 1 total
...
```
2019-08-03 16:15:05 +00:00
## Commit and Push Changes
2019-08-01 15:26:17 +00:00
```bash
$ git add < whatever only files you added >
$ git commit -m "Message"
```
2019-09-04 21:30:45 +00:00
## Publish a releases/v1 Action
2019-08-01 21:08:30 +00:00
2019-09-04 21:30:45 +00:00
After changing some files, create a releases/v1 branch which we will release
2019-08-01 21:08:30 +00:00
2019-08-03 16:15:05 +00:00
```bash
2019-09-04 21:30:45 +00:00
$ git checkout -b releases/v1
2019-08-03 16:15:05 +00:00
```
2019-08-01 21:08:30 +00:00
2019-08-03 16:15:05 +00:00
> NOTE: We will provide tooling and an action to automate this soon.
2019-08-01 15:26:17 +00:00
2019-08-21 19:31:44 +00:00
Check in production dependencies:
2019-08-03 16:15:05 +00:00
1. **Do not ignore node_modules** : Add a `!` in front of the `node_modules` line.
2. **Delete node_modules** : rm -Rf node_modules
3. **Install production dependencies** : npm install --production
4. **Add** : git add node_modules
2019-08-01 15:26:17 +00:00
2019-08-03 16:15:05 +00:00
Simply commit and push your action to publish.
2019-08-01 15:26:17 +00:00
```bash
2019-08-03 16:17:54 +00:00
$ git commit -a -m "publishing v1 of action"
2019-08-01 15:26:17 +00:00
$ git push
```
2019-08-04 18:12:04 +00:00
> NOTE: Consider versioning your actions with tags. See [versioning](action-versioning.md)
2019-08-01 15:26:17 +00:00
2019-08-04 12:56:16 +00:00
## Test End To End
Once the action has a self contained version in the v1-release branch, you can test it by referencing the latest (and potentially unstable) version in the release branch. If you are fixing an issue that someone else is having with your action, you can have them try it before you officially releasing it as the 'v1' version.
```yaml
steps:
2019-09-04 21:30:45 +00:00
uses: myorg/myaction@releases/v1
with:
name: World!
2019-08-04 12:56:16 +00:00
```
## Release Current Changes as v1
Once you have tested end to end, push a tag of 'v1' to the commit in the release branch.
See [action versioning ](action-versioning.md ) for more details.
2019-08-03 16:15:05 +00:00
# Users Referencing
Users can now reference your action in their workflows with
```yaml
steps:
2019-09-04 21:30:45 +00:00
uses: myorg/myaction@v1
with:
name: World!
2019-08-03 16:15:05 +00:00
```
2019-08-01 15:26:17 +00:00