2019-08-01 15:26:17 +00:00
# Creating a JavaScript Action
The [node12-template ](https://github.com/actions/node12-template ) repo contains everything you need to get started.
# Create a Repo from the Template
Navigate to https://github.com/actions/node12-template
Click on `Use this template` to create the repo for your action.
![template ](assets/node12-template.png )
Complete creating your repo and clone the repo.
> NOTE: The location of the repo will be how users will reference your action in their workflow file with the using keyword.
e.g. To use https://github.com/actions/setup-node, user's will author:
```yaml
steps:
using: actions/setup-node@master
```
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.
> Key Point: the branch that users reference in their workflow files should reference an action that has **only** the production dependencies.
2019-08-03 16:17:54 +00:00
The workflow below describes a strategy where you code in master (with node_modules ignored) with a v1 branch users reference and contains the product references. Actions are self contained referenced on the github graph of repos.
2019-08-03 16:15:05 +00:00
## Install Dependencies
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
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
Your action has a name and a description. Update the author.
Create inputs that your unit of work will need. These will be what workflow authors set with the `with:` keyword.
```yaml
name: 'My new action'
description: 'A test action'
author: 'GitHub'
inputs:
myInput:
description: 'Input to use'
default: 'world'
runs:
using: 'node12'
main: 'lib/main.js'
```
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 {
const myInput = core.getInput('myInput');
core.debug(`Hello ${myInput}!`);
} catch (error) {
core.setFailed(error.message);
}
}
run();
```
Modify tests in `__tests__\main.test.ts` . The template uses [jest ](https://github.com/facebook/jest ).
2019-08-03 16:15:05 +00:00
## Format the Code
2019-08-01 15:26:17 +00:00
```bash
$ npm run format
```
2019-08-03 16:15:05 +00:00
## Build and Test
2019-08-01 15:26:17 +00:00
```bash
$ npm run build
> node12-template-action@0.0.0 build /Users/user/Projects/testnode12
> 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-08-03 16:17:54 +00:00
## Publish a v1 Action
2019-08-01 21:08:30 +00:00
2019-08-03 16:17:54 +00:00
After changing some files, create a v1 branch which we will release
2019-08-01 21:08:30 +00:00
2019-08-03 16:15:05 +00:00
```bash
2019-08-03 16:17:54 +00:00
$ git checkout -b 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-03 16:15:05 +00:00
Checkin production dependencies:
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
```
> NOTE: Consider versioning your actions with tags. See [versioning](docs/action-versioning.md)
2019-08-03 16:15:05 +00:00
# Users Referencing
Users can now reference your action in their workflows with
```yaml
steps:
2019-08-03 16:17:54 +00:00
using: {org}/{reponame}@v1
2019-08-03 16:15:05 +00:00
```
2019-08-01 15:26:17 +00:00