6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub Actions in action

Posted at
1 / 12

はじめに

これは社内オンラインLTイベントの資料を編集したものです。


GitHub Actionsとは

参考: https://knowledge.sakura.ad.jp/23478/

  • GitHub が提供する、CI/CD向けの仕組み
    • GitHubのインフラ上でDockerコンテナを実行する
    • CI/CD以外にも使える
  • 実行のタイミング (on)
    • リポジトリへのpush/merge、ブランチの作成、削除
    • issueの登録、pull request, fork, star
    • 定期実行、などなど
  • 使えるOS (runs-on)
    • Linux(Ubuntu), Windows, macOS
  • 使えるコンテナ
    • GitHub上に用意されたAction/コンテナ
    • Dockerレジストリ上のDockerイメージを指定
    • 対象リポジトリ内にDockerfileを作成して、独自のコンテナをビルド

どんなふうに使えるか

例えば

  • ビルド
    • タイミング:メインのブランチに、ソースコードの変更が反映されたら
    • やること:最新ソースを取得→ビルド→テスト→実行環境にデプロイ
    • 対象:サーバー側のアプリや、ビルドが必要なWebのフロント、モバイルアプリも可
  • 監視、バッチ処理
    • タイミング:毎週、毎時、5分ごと
    • やること: 死活監視、ログの集約、...

Actionの作成

  • リポジトリの [Actions]-[set up a workflow yourself] から、yamlで指定
github_actions_new.png

例1:Webの死活監視

処理の流れは、yamlファイルで定義

name: http-monitor
on:
  schedule:
    - cron: '*/30 * * * *' # 30分ごと
jobs:
  watch-sfu:
    runs-on: ubuntu-latest # ubuntuで実行
    steps:
    - name: check sfu
      env:
        URL: ${{ secrets.SFU_URL }} # レポジトリの秘密情報から環境変数に設定
      run: curl $URL   # curl で httpアクセスできるかを確認
    - name: Slack Notification
      if: failure()  # 前のステップが失敗したら、slackに通知
      uses: rtCamp/action-slack-notify@v2.0.0
      env:
        SLACK_COLOR: '#c02020'
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        SLACK_MESSAGE: 'SFU maybe down. Plase check it.'

例2:React.jsアプリのビルド&デプロイ

name: Release GitHub Page
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
jobs:
  build:
    runs-on: ubuntu-latest
    strategy: #複数のバージョンを指定してテストすることも可能
      matrix:
        node-version: ['12.x']
    steps:
    - uses: actions/checkout@v2
    - name: install and build # パッケージのインストールとアプリのビルド
      run: |
        npm ci
        npm run build
      env:
        CI: true

    - name: deploy # gh-pagesを使って公開
      uses: peaceiris/actions-gh-pages@v2
      env:
        ACTIONS_DEPLOY_KEY: ${{ secrets.ACTION_KEY_DEPLOY_GHPAGES }} #秘密鍵を指定
        PUBLISH_BRANCH: gh-pages
        PUBLISH_DIR: ./build

実行中

github_actions_running.png

その他

  • artifact
    • ログや、処理結果のファイル、生成したモジュールなどを、ストレージに残せる
    • 90日後に自動的に期限切れになるので、それまでにダウンロードする
  • 稼働率
    • コンテナの起動、実行に失敗することもある
    • Web監視に半年利用中の経験
      • 2回、数時間連続で失敗
      • 1回、単発で失敗
      • 昨日(7/13)、2回失敗

デプロイ用の公開鍵

  • リポジトリの [Settings] - [Deploy Keys] で登録
  • [Allow write access]にチェック
github_actions_deploy_key.png

秘密鍵、秘密情報

  • リポジトリの [Settings] - [Secrets] で登録
github_action_secret_key.png

まとめ

  • GitHub Actionsを使うと、手軽にビルド、テスト、デプロイができる
    • ステージング、本番のような多段階のデプロイには他の仕組みとの連携が必要
  • 監視にも便利
    • ただし、シビアな利用ケースには他の監視サービスとの併用がお勧め
  • 公式: https://docs.github.com/ja/actions
6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?