LoginSignup
8
5

More than 3 years have passed since last update.

【GitHub Actions】自作Actionsのリリースを自動化する

Last updated at Posted at 2019-09-15

TypeScriptでGitHub Actions用のActionsを自作する際に、色々と面倒臭い部分をGitHub Actions上で自動化したので共有します。
開発・メンテナンスしにくかった理由は、開発用のブランチでnode_modulesをトレース対象にし、ローカルでブランチ変えてnpm install --productionしたりなど、普段の開発ではありえないことをする必要があったからです。
特に複数人で開発する際には列挙したことはかなりネックになると思います。
それでは、これらの解消方法を説明していきます!

今回の成果物はrelease.ymlにあります。これは実際にhomoluctus/slatifyで動いているので実質的に動作が保証されています。

自動化したこと

  1. TypeScriptのビルド
  2. 本番用のnode_modulesを作成(npm install --production)
  3. 変更箇所のコミット
  4. リリース用ブランチにプッシュ

Workflow

release.yml
name: Prepare for release

# イベントトリガーをpushに設定して、対象ブランチをreleases/*、ファイルパスをsrc/*に制限する
on:
  push:
    branches:
      - releases/*

    paths:
      - src/*

jobs:
  release:
    name: Build production
    runs-on: ubuntu-18.04
    steps:
      # remotes/origin/releases/v1にチェックアウト
      - uses: actions/checkout@v1
        with:
          ref: remotes/origin/releases/v1

      - name: Git checkout
        run: git checkout -b releases/v1

      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: '10.x'

      - name: Install dependencies
        run: npm install

      # typescriptをコンパイル
      - name: NPM build
        run: npm run build

      # リリース用に.gitignoreからnode_modulesを削除
      - name: Not ignore node_modules
        run: sed -i '/node_modules*/d' .gitignore

      # リリース用にプロダクション用のモジュールのみインストール
      - name: Install dependencies for production
        run: |
          rm -rf node_modules
          npm install --production

      # gitのコンフィグ回りを設定
      - name: Setup git
        env:
          # GitHubのPersonal access tokens
          ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
        run: |
          git config --local user.name homoluctus
          git config --local user.email xxxxxxxx@xxxxxx
          git remote set-url origin https://homoluctus:${ACCESS_TOKEN}@github.com/homoluctus/slatify.git

      - name: Git commit
        run: |
          git add -A

          DIFF=`git diff --cached --numstat | wc -l`
          if [ $DIFF -eq 0 ]; then
          # ファイルの変更がなければjobを正常終了する
            exit 0
          fi

          git commit -am '[UPDATE] prepare for release'

      - name: Git push
        run: git push origin HEAD

      - name: Slack Notification
        uses: homoluctus/slatify@master
        if: always()
        with:
          type: ${{ job.status }}
          job_name: ':truck: *Prepare for release*'
          channel: '#develop'
          url: ${{ secrets.SLACK_WEBHOOK }}

  # releases/v1にプッシュしたコードが正常に動作するか確認
  check:
    name: Check action
    runs-on: ubuntu-18.04
    needs: release
    steps:
      - uses: actions/checkout@master

      - name: Slack Notification
        uses: homoluctus/slatify@releases/v1
        if: always()
        with:
          type: ${{ job.status }}
          job_name: ':robot_face: *Check action*'
          channel: '#develop'
          url: ${{ secrets.SLACK_WEBHOOK }}

おわりに

今回説明したのを応用すると、Actionsの開発に限らず様々な場面でCIでコードを編集してコミット→プッシュすることができます。
煩雑な作業は自動化してスピーディーな開発を目指していきましょう!

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5