Typescript에서 수행한 내용을 Github Actions에 전달하여 사용

Github Actions에서 Typescript를 실행하고 계산 내용을 Github Actions에서 사용하고 싶을 때가 있습니다.

TL;DR


  • Github Actions에서 Typescript 실행
  • @actions/core 의 setOutput 사용



  • 매일 아침 10시에 aws의 비용을 취득하고, aws의 비용은 달러이므로, 원에 고쳐서 slack에 통지

    ※세세한 취득 방법은 테마가 아니기 때문에 생략하고 있습니다

    get-aws-cost.ts
    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
    

    코드 내에서 전달



    get-aws-cost.ts
    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에서 사용



    create-aws-bill.yml
    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에서 설정 한 값을 사용할 수 있습니다

  • 참조

    좋은 웹페이지 즐겨찾기