LoginSignup
1
1

Github Actionsをローカルで動かすことができるactの紹介

Posted at

昨日、Rustで作成したCLIアプリケーションのビルドを目的に、Github Actionsをはじめて触ってみました。
他の方が用意してくださっていたワークフローを少し弄って利用してみたのですが、書き方が良く分からなかったり上手く動作しなかったりなどが原因で、何度もコミットと実行を繰り替えすことになりました。

ワークフローのデバッグがローカルで実行できるツールはないだろうかと探してみたところ、actというツールを発見しました。この記事ではactについて簡単にまとめたいと思います。

はじめに

Github Actionsは、ソフトウェアの開発プロセスを自動化することができるCI/CDプラットフォームです。例えばプルリクエスト時にテストを実行したり、mainにマージされたら運用環境にデプロイしたりすることができるようになります。

しかし、今回のわたしのように、適切なワークフローを作成できない場合は何度もコミットと実行を繰り返さなければならず、手間がかかります。

そこでactというツールを使うことで、Github Actionsをローカルで実行できるようになり、ワークフローのデバッグを行うことができます。

動作環境

  • Ubuntu 22.04
  • act 0.2.46

actとは

actは、Github Actionsのワークフローをローカル環境で実行するためのツールです。このツールを使用することで、ワークフローに加えた変更をテストするために毎回コミットやプッシュをする必要がなくなり、ローカルでアクションを実行することができるようになります。

actのインストール

前提条件として、dockerをインストールする必要があります。actはワークフローをdockerで実行するためです。dockerのインストールにつきましては今回の記事では省略させていただきます。

その他のコンテナバックエンドでも動作する可能性はありますが、公式ではサポートしておらず、保証されていません。

$ curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
$ act --version
act version 0.2.46

curlコマンドでインストールした場合はPATHを通す必要がありました。brewwingetなどのパッケージマネジャーでもインストールすることができるため、お使いの環境に応じた方法でインストールしてください。

シンプルなワークフローの実行

では早速ワークフローファイルを作成してactを実行してみたいと思います。
今回使用するワークフローは、Githubが用意してくれている、Simple workflowを利用します。

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

name: CI

# Controls when the workflow will run
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: ubuntu-latest

    # 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@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # 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.

ワークフローファイルの用意が完了したので、actを実行し、セットアップを行います。

$ act
? Please choose the default image you want to use with act:

  - Large size image: +20GB Docker image, includes almost all tools used on GitHub Actions (IMPORTANT: currently only ubuntu-18.04 platform is available)
  - Medium size image: ~500MB, includes only necessary tools to bootstrap actions and aims to be compatible with all actions
  - Micro size image: <200MB, contains only NodeJS required to bootstrap actions, doesn't work with all actions

Default image and other options can be changed manually in ~/.actrc (please refer to https://github.com/nektos/act#configuration for additional information about file structure)  [Use arrows to move, type to filter, ? for more help]
  Large
> Medium
  Micro

actの初回実行時には、デフォルトで使用するイメージを選択するよう求められます。わたしの場合は今後長期的に使用することを考えて、Mediumサイズを選択しました。
現状、actではubuntuランナーのみ使用可能であり、WindowsおよびmacOSプラットフォームはサポートされていません。

Dockerイメージのプルがすべて完了すればactの準備は完了です。

$ act -l
Stage  Job ID  Job name  Workflow name  Workflow file  Events                             
0      build   build     CI             blank.yml      push,pull_request,workflow_dispatch
$ act 
# pushイベントの実行
$ act pull_request
# pull_requestイベントの実行
$ act -j build
# buildジョブの実行

act <イベント名>act -j <ジョブ名>でGithub Actionsのワークフローをローカル環境でも実行できるようになりました。

まとめ

今回の記事では、actを使ってGithub Actionsをローカル環境で実行する方法について紹介しました。actを利用することで、ワークフローのデバッグをローカルで行うことができるので、昨日のわたしのようにコミット&実行地獄に陥る可能性は低くなると思います。

今回は紹介できませんでしたが、actでは環境変数を設定したりすることもできます。より詳細な情報につきましては、actのGithubリポジトリを参照してください

1
1
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
1
1