🦔
Typescriptで計算した結果をGithub Actionsに渡して使用する
Github Actions 上で Typescript を実行して、その計算内容を Github Actions 上で使用したい時とかありますよね🎅
TL;DR
- Github Actions で Typescript 実行
- @actions/core の setOutput を使用
例
毎朝 10 時に aws のコストを取得して、aws のコストはドルなので、円に直して slack に通知
※細かい取得方法はテーマではないので省略してます
const awsCost = 100; // ドル
const rate = 106.1 // ドル円
const awsJpyBill = awsCost * JPY
※ awsCost や rate は aws-sdk の CostExplorerなどで取得してください
上記の計算(ほぼしてないけど)を、Github Actions 上で使用したいときは@actions/core
を使用して渡します
方法
@actions/core
を使用
手順
- @actions/core を package.json に追加
- コード内から渡す
- Github Actions 上で使用
@actions/core を package.json に追加
npm i @actions/core -D
コード内から渡す
import { setOutput } from '@actions/core'; // 追加
const awsCost = 100; // ドル
const rate = 106.1 // ドル円
const awsJpyBill = awsCost * JPY
setOutput("awsCost", awsCost) // 追加
setOutput("rate", rate) // 追加
setOutput("awsJpyBill", awsJpyBill) // 追加
Github Actions 上で使用
name: Create AWS Bill
on:
schedule:
- cron: '0 1 * * *' # 毎日 am10時
workflow_dispatch:
branches: [master] # Githubの画面から手動でも実行できるように
env:
SLACK_ICON: https://raw.githubusercontent.com/quintessence/slack-icons/master/images/octocat-spock-slack-icon.png
jobs:
notify-aws-cost:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14'
- name: Install Dependency
run: npm ci
- name: Get AWS Cost
id: get-bill
run: npx ts-node get-aws-cost.ts
- name: Slack notification
uses: rtCamp/action-slack-notify@master
env:
SLACK_ICON: ${{ env.SLACK_ICON }}
SLACK_MESSAGE: AWSのコストは ${{ steps.get-bill.outputs.awsCost }}ドル で ${{ steps.get-bill.outputs.awsJpyBill }} レートは(${{steps.get-bill.outputs.rate}})円
ポイント
- ts の実行箇所に id(get-bill)を設定
-
${{ steps.<steps_id>.outputs.<setOutputで設定した名称>}}
で、ts 内で設定した値を使用することができます🎅
参照
Discussion