kaikai221のブログ

kaikaiについてのブログです

GitHub Actions

GitHub Actionsの紹介

 

GitHub Actionsのコアな概念と様々な機能について学び、あなたのリポジトリにautomation機能を追加する方法を例を用いて示します。

 

概要

 

 

GitHub Actionsはあなたのソフトウェア開発のライフサイクル内のタスクを自動かするのに役立ちます。GitHub Actionsはイベントドリブンでそしてそれは指定したイベントが生じた後に一連のコマンドを叩くことができます。例えば、誰かがプルリクエストをリポジトリに出すたびに、ソフトウェアテストスクリプトで実行するコマンドを自動的に走らせることができます。

 

この図はいかにしてソフトウェアテストスクリプトGitHub Actionsを使って自動的に走らせているかを示しています。一つのイベントが自動的にワークフローにトリガーされたその時にjobが含まれている。jobは次にどのアクションを走らせるのかを順番にコントロールするために使います。これらのアクションはソフトウェアテストを自動かするためのアクションです。

 

 

Workflow overview

GitHub Actionsのの機能

下の図はjobを一緒に走らせるための多様なGitHub Actions機能のリストです。

あなたはこれらお互いがどのように相互作用しているかをみることができます。

Component and service overview

Workflows

workflowはリポジトリーに追加するための自動化手順です。ワークフローは一つ以上のjobsから成り立っており、イベントによってスケジュール化、またトリガーできる。ワークフローは通常test、パッケージ、releaseのbuild時またはgithubにdeployした時に使うことでができます。

Events

イベントはワークフローによってトリガーした特定の活動です。例えば、活動は誰かがリポジトリーにpushした時またはイシューやプルリクエストが作られた時にgithubから発信することができる。また、外部イベントが怒った時にrepository dispatch webhook を用いてワークフローをトリガーできる。ワークフローのトリガーで使われたイベントのリストをコンパイルするためにはEvents that trigger workflows.こちらを拝見してください

Jobs

jobは同じランナーで行う一連のステップです。デフォルトでは多くのjobのワークフローはこれらのjobと並行して作動する。jobを順序立って行うこともできます。例えば、ワークフローはビルドの状態と依存してテストをジョブするビルドとテストの二つの一連のjobを持つことができる。もしbuildのジョブが失敗したらテストジョブは作動しません。

Steps

ステップはjobないでコマンドを走らせることができる個人タスクです。

ステップはactionまたはshellコマンドを使うことができる。jobないのそれぞれのstepは同じrunnrでjobないのアクションは他とデータをシェアすることができる。

Actions

actionはjobを作るためのステップ内を結びつけるスタンドアロンコマンドである。

actionはワークフローブロック内をbuildしている最小のポータブルである。自分のactionを作ったり、github comunityで作られているアクションを使うことができる。workflowないでactionを使うために、stepとして含める必要がある。

Runners

runnerはGitHub Actions runner application でインストールされた、サーバーである。GitHubを使うことによってrunnerを使うことができる。または、自分自身でも作れる。runnnerはジョブを実行し、進捗のログを報告しその結果をGitHubに返す。GitHubのランナーはUbuntu Linux, Microsoft Windows, and macOSに基づいており、仮想の新鮮な環境で実行されます。もし自分好みのカスタマイズをしたかったら、オリジナルrunnerを作ることができる。Hosting your own runnersその場合ここ見てね。

Create an example workflow

GitHub Actions uses YAML syntax to define the events, jobs, and steps. These YAML files are stored in your code repository, in a directory called .github/workflows.

You can create an example workflow in your repository that automatically triggers a series of commands whenever code is pushed. In this workflow, GitHub Actions checks out the pushed code, installs the software dependencies, and runs bats -v.

GitHub ActionsはYAML構文を使ってイベントやステップを定義しているよ。yaml構文は.github/workflowsディレクトリで書いてね。コードをpushした時いつでも使えるよ。GitHub Actionsの中でもchecks outとパッケージのインストールが必要だよ。

 

  1. In your repository, create the .github/workflows/ directory to store your workflow files.
  2. In the .github/workflows/ directory, create a new file called learn-github-actions.yml and add the following code.
    name: learn-github-actions
    on: [push]
    jobs:
      check-bats-version:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-node@v1
          - run: npm install -g bats
          - run: bats -v
    
  3. Commit these changes and push them to your GitHub repository.

Your new GitHub Actions workflow file is now installed in your repository and will run automatically each time someone pushes a change to the repository. For details about a job's execution history, see "Viewing the workflow's activity."

Understanding the workflow file

To help you understand how YAML syntax is used to create a workflow file, this section explains each line of the introduction's example:

name: learn-github-actions
Optional - The name of the workflow as it will appear in the Actions tab of the GitHub repository.
on: [push]
Specify the event that automatically triggers the workflow file. This example uses the push event, so that the jobs run every time someone pushes a change to the repository. You can set up the workflow to only run on certain branches, paths, or tags. For syntax examples including or excluding branches, paths, or tags, see "Workflow syntax for GitHub Actions."
jobs:
Groups together all the jobs that run in the learn-github-actions workflow file.
check-bats-version:
Defines the name of the check-bats-version job stored within the jobs section.
  runs-on: ubuntu-latest
Configures the job to run on an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub. For syntax examples using other runners, see "Workflow syntax for GitHub Actions."
  steps:
Groups together all the steps that run in the check-bats-version job. Each item nested under this section is a separate action or shell command.
    - uses: actions/checkout@v2
The uses keyword tells the job to retrieve v2 of the community action named actions/checkout@v2. This is an action that checks out your repository and downloads it to the runner, allowing you to run actions against your code (such as testing tools). You must use the checkout action any time your workflow will run against the repository's code or you are using an action defined in the repository.
    - uses: actions/setup-node@v1
This action installs the node software package on the runner, giving you access to the npm command.
    - run: npm install -g bats
The run keyword tells the job to execute a command on the runner. In this case, you are using npm to install the bats software testing package.
    - run: bats -v
Finally, you'll run the bats command with a parameter that outputs the software version.

Visualizing the workflow file

In this diagram, you can see the workflow file you just created and how the GitHub Actions components are organized in a hierarchy. Each step executes a single action or shell command. Steps 1 and 2 use prebuilt community actions. Steps 3 and 4 run shell commands directly on the runner. To find more prebuilt actions for your workflows, see "Finding and customizing actions."

Workflow overview

Viewing the job's activity

Once your job has started running, you can see a visualization graph of the run's progress and view each step's activity on GitHub.

  1. On GitHub, navigate to the main page of the repository.

  2. Under your repository name, click Actions.

    Navigate to repository

     

  3. In the left sidebar, click the workflow you want to see.

    Screenshot of workflow results

     

  4. Under "Workflow runs", click the name of the run you want to see.

    Screenshot of workflow runs

     

  5. Under Jobs or in the visualization graph, click the job you want to see.

    Select job

     

  6. View the results of each step.

    Screenshot of workflow run details