kakakakakku blog

Weekly Tech Blog: Keep on Learning!

GitHub Actions に入門するなら GitHub Learning Lab の Hello World コースを受講しよう

GitHub を学ぶときに「GitHub Learning Lab」を使うと便利!という紹介記事を6月に書いた.最近 GitHub Actions を使う機会があり,入門するために「GitHub Learning Lab」「GitHub Actions: Hello World」コースを受講した.今回も非常に良かった!

kakakakakku.hatenablog.com

GitHub Actions: Hello World

「GitHub Actions: Hello World」GitHub Actions を学ぶ入門コースとなる.現在はまだ日本語に翻訳されてなく,英語を選択する必要はあるけど,Bot / Issue / Pull Request を活用したインタラクティブな構成になっていて,楽しく学べる.以下のステップを進めていくことになり,最終的には GitHub Actions を使って,Docker イメージをビルドして実行できるようになる.

  • Step 1 : Add a Dockerfile
  • Step 2 : Add an entrypoint script
  • Step 3 : Add an action.yml file
  • Step 4 : Start your workflow file
  • Step 5 : Run an action from your workflow file
  • Step 6 : Trigger the workflow
  • Step 7 : Incorporate the workflow

lab.github.com

セットアップ

まず「Start free course」ボタンを押してセットアップをする.今回はリポジトリ公開設定として Private を選ぶ.すると,自動的にコースで使うリポジトリ hello-github-actions が作られる.

f:id:kakku22:20200707004432p:plain

さっそく Issue に Bot から Step 1 の手順がコメントされる.進めていくぞ!

f:id:kakku22:20200707004504p:plain

Step 1 : Add a Dockerfile

最初に Issue を読みながら GitHub Actions の概要を学んでいく.「アクション」には「Docker Container (Linux)」「JavaScript (Linux, MacOS, Windows)」の2種類があり,今回は「Docker Container」を使っていく.

help.github.com

まず,first-action ブランチを作り,以下のように action-a/Dockerfile を実装したら Pull Request を作る.GitHub の基本的な操作は書いてなく,慣れていない場合は前提コース「Introduction to GitHub」コースを受講しておく必要がありそう.

FROM debian:9.5-slim

ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

Step 2 : Add an entrypoint script

次に ENTRYPOINT に指定したスクリプト action-a/entrypoint.sh を実装する.単純に echo するだけではなく,GitHub Actions の inputs シンタックスを試すため,環境変数も含めておく.

#!/bin/sh -l

sh -c "echo Hello world my name is $INPUT_MY_NAME"

Step 3 : Add an action.yml file

今度は GitHub Actions のアクションを定義していくため action.yml を作る.ポイントは runs シンタックスを使って Dockerfile を指定している点と,inputs シンタックスを使って環境変数のデフォルト値を設定している点になる.branding シンタックスはアクションを GitHub Marketplace に公開するときに使えるアイコン設定(Feather から選べる)となり,今回は関係なさそう.

name: "Hello Actions"
description: "Greet someone"
author: "octocat@github.com"

inputs:
  MY_NAME:
    description: "Who to greet"
    required: true
    default: "World"

runs:
  using: "docker"
  image: "Dockerfile"

branding:
  icon: "mic"
  color: "purple"

help.github.com

Step 4 : Start your workflow file

アクションを定義したため,次にワークフローを定義していく.設定ファイルは .github/workflows/main.yml に置く.

  • name : ワークフロー名
  • on : ワークフローを実行するトリガー設定
  • jobs : アクション設定
    • uses: actions/checkout@v1 : リポジトリをチェックアウトする(現在は既に v2 もリリースされている)
name: A workflow for my Hello World file
on: push
jobs:
  build:
    name: Hello world action
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: ./action-a
        with:
          MY_NAME: "Mona"

docs.github.com

github.com

Step 5 : Run an action from your workflow file

実際にワークフローを確認すると,正常に実行されていた.

Step 6 : Trigger the workflow

標準出力に Hello world my name is Mona と表示されていた.

f:id:kakku22:20200707004724p:plain

Step 7 : Incorporate the workflow

最後は Pull Request を master ブランチに Merge する.

最終的に Issue / Pull Request は以下のようになっていた.今回も非常に良かった!楽しかった!

f:id:kakku22:20200707004739p:plain

まとめ

「GitHub Learning Lab」「GitHub Actions: Hello World」コースを受講して,GitHub Actions に入門した.一歩一歩インタラクティブに進めることで理解度を高められる点は,やはり「GitHub Learning Lab」の最大のメリットだと思う.実は「GitHub Learning Lab」には,コースとドキュメントをまとめた「Learning Path」という仕組みもあり,例えば「DevOps with GitHub Actions」という Learning Path もある.「Publish to GitHub Packages」コースは特に気になる!

  1. GitHub Actions: Hello World
  2. GitHub Actions: Continuous Integration
  3. GitHub Actions: Publish to GitHub Packages
  4. GitHub Actions: Continuous Delivery with Azure
  5. GitHub Actions: Continuous Delivery with AWS
  6. GitHub Actions: Writing JavaScript Actions
  7. GitHub Actions: Write Docker container actions
  8. GitHub Actions: Using GitHub Script
  9. Migrating from Azure Pipelines to GitHub Actions
  10. Migrating from Jenkins to GitHub Actions
  11. Migrating from CircleCI to GitHub Actions

lab.github.com