Open8

GitHub ActionsのactionをJavaScriptで作るチュートリアルのメモ

kentamakentama

gh repo create でリポジトリを作成する。
設定内容は適当に。

kentamakentama

npmの初期化とか

npm init -y
npm install @actions/core
npm install @actions/github
kentamakentama

action.ymlを作る

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'
kentamakentama

index.jsを作る

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);
}
kentamakentama

tagをつけてpushする。
node_modulesもコミットする。

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.1
git push --follow-tags
kentamakentama

https://github.com/vercel/ncc

node_modulesをコミットしない代替案。
npm i -g @vercel/ncc しろと書いてあるがglobalに入れたくないのでnpxで。

npx @vercel/ncc build index.js --license licenses.txt

action.ymlを書き換える。

 runs:
   using: 'node12'
-  main: 'index.js'
+  main: 'dist/index.js'
kentamakentama

こんな感じでactionを作ると呼び出せる

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: xxx/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 }}"