LoginSignup
2
0

More than 1 year has passed since last update.

GitHub GitHub Actionsサンプルコード

Last updated at Posted at 2021-08-10

はじめに

サンプルコードと軽い解説をまとめました。

サンプルコード

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on: [push, pull_request, workflow_dispatch]

# on:
#   # Triggers the workflow on push or pull request events but only for the master branch
#   push:
#     branches: [ master ]
#   pull_request:
#     branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  # workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: macos-10.15

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Install Norminette.
        run: |
          python3 -m pip install --upgrade pip setuptools
          python3 -m pip install norminette

      # Runs a single command using the runners shell
      # アクションの終了コードの設定 - GitHub Docs => 終了ステータス0以外ならfailureが出ます。
      # https://docs.github.com/ja/actions/creating-actions/setting-exit-codes-for-actions
      - name: Run Norminette.
        run: make norm

      - name: Run Test.
        run: bash ./all-test.sh tests

      # # Runs a set of commands using the runners shell
      # - name: Run a multi-line script
      #   run: |
      #     echo Add other actions to build,
      #     echo test, and deploy your project.

どのタイミングで実行するか

# Controls when the action will run.
on: [push, pull_request, workflow_dispatch]

pushするタイミング、pull_requestを送るタイミングで実施されるように設定しています。

workflow_dispatch

workflow_dispatchを設定しておくと、githubから手動でテストを実行できるようになります。

スクリーンショット 2021-08-10 12.14.14.png

どの環境で実施するか

    # The type of runner that the job will run on
    runs-on: macos-10.15

macos-10.15で実行されるようにしています。

About GitHub-hosted runners - GitHub Docs

macで実施すると、通常より実行に対して支払うコストが高いようなので注意してください。

GitHubがホストするWindows及びmacOSのランナー上で実行されるジョブは、Linuxのランナー上のジョブの消費に対して2倍及び10倍の分を消費します。 たとえば、Windowsでの1,000分はアカウントに含まれる分のうちの2,000分を消費します。 1,000 macOS分を使用すると、アカウントに含まれる10,000分を消費します。

オペレーティングシステム 分の倍率
Linux 1
macOS 10
Windows 2

GitHub Actionsの支払いについて - GitHub Docs

実行させるコード

      # Runs a single command using the runners shell
      # https://docs.github.com/ja/actions/creating-actions/setting-exit-codes-for-actions
      - name: Run Norminette.
        run: make norm

終了ステータス0以外ならfailureが出ます。

failureが出るとCI上でエラーとして表示され、マージを拒否したり、いろいろな設定ができます。

github actionを使用したチーム開発

チーム開発をしているレポジトリで設定をします。

Setting -> Branches -> Branch protection rules -> Add ruleを押す

以下のように設定します。

スクリーンショット 2021-08-10 18.33.46.png

status checks that are required.の欄で実行したgithub actionを入力します。

これを設定することで、github actionで通過しなかった場合にmainブランチにマージすることができなくなります。

  • ここにはjobs以下で記述したActionが設定できるようになります。実行は並列で行われるため、分割することで早く処理ができる場合もあります。

参考

GitHub Actionsのドキュメント - GitHub Docs

2
0
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
2
0