GitHub初心者向け: GitHub Actionsの始め方
GitHub公式ブログは、GitHub Actionsの基本概念と仕組みを初心者向けに解説し、自動化ワークフローの作成方法を紹介するチュートリアル記事を公開した。
キーポイント
GitHub Actionsの定義と目的
GitHub ActionsはGitHubに組み込まれたCI/CDおよび自動化プラットフォームであり、反復的なタスクやデプロイメントプロセスをYAMLファイルで自動化することを可能にする。
ワークフローの基本構成要素
ワークフローはイベント(コードプッシュ、プルリクエストなど)でトリガーされ、ホストされたランナー上で実行されるジョブ(シェルコマンドや事前構築アクションの集合)から構成される。
実践的な学習リソースの提供
記事ではサンプルリポジトリのフォークとActionsタブの操作手順を説明し、さらに「Hello GitHub Actions」スキル演習を推奨して実践的な学習を促進している。
GitHub Actionsのワークフロー構成要素
ワークフローはYAMLファイルで定義され、Name(ワークフローの説明)、On(トリガー条件)、Jobs(実行内容)の3つの主要セクションから構成される。
ワークフローのトリガー設定
onセクションでワークフローの起動条件を指定でき、例ではissuesイベントのopenedタイプ(新規Issue作成時)をトリガーに設定している。
ジョブの実行環境と権限設定
runs-onでUbuntuなどの実行環境を指定し、permissionsでリポジトリの読み取りやIssueへの書き込み権限を付与できる。
GitHub Actionsのテスト方法
ワークフローをテストするには、変更をリポジトリのメインブランチにプッシュしてマージし、新しいIssueを作成して自動ラベル付けを確認する。
影響分析・編集コメントを表示
影響分析
この記事は既存機能の入門解説であり、技術的な革新性は低いが、GitHubユーザーのオンボーディングとプラットフォーム活用促進に貢献する。開発者の生産性向上とDevOps文化の普及に間接的に寄与する内容である。
編集コメント
GitHub公式の初心者向けチュートリアルシリーズの一環として、実用的な自動化技術の導入を促す教育コンテンツ。AI業界との直接的な関連性は薄いが、開発者エコシステムの基盤整備として評価できる。
GitHub初心者向けシリーズ第3シーズンへようこそ!前回のエピソードでは、GitHub IssuesとProjectsの基本について説明しました。今回は、非常に人気の高い機能であるGitHub Actionsについて詳しく見ていきましょう。この記事を読み終える頃には、GitHub Actionsの使い方を理解し、最初の自動化ワークフローを作成できるようになります。
動画で視聴したい場合は、GitHub初心者向けシリーズの全エピソードをYouTubeでご覧いただけます(このチュートリアルの完全な動画は上記で視聴できます)。
GitHub Actionsとは?
GitHub Actionsは、GitHubに組み込まれた継続的インテグレーション/継続的デリバリー(CI/CD)および自動化プラットフォームです。「アクションワークフロー」または単に「ワークフロー」と呼ばれることもあります。リポジトリに保存されたYAMLファイルを使用して、繰り返しのタスクやデプロイメントプロセスを自動化できます。脆弱性スキャンの実行、テスト、リリース作成、チームへの重要なお知らせなど、さまざまなタスクにGitHub Actionsを利用できます。
ワークフローは、プッシュ、プルリクエスト、スケジュールなどのGitHubイベントによってトリガーされ、仮想環境で実行されます。
ワークフローの仕組み
ワークフローの詳細な仕組みに入る前に、いくつかの重要な用語を定義しておきましょう。
- イベント: ワークフローをトリガーする特定のアクティビティ(例:コードのプッシュ、プルリクエストの作成、Issueの作成など)。
- ホステッドランナー: ワークフロー内のジョブを実行する仮想マシン。GitHubが提供するホステッドランナーを使用するか、独自のセルフホステッドランナーを設定できます。
- ジョブ: 同じランナー内で実行される一連のステップ。各ステップは、シェルコマンドかGitHub Marketplaceの事前構築済みアクションのいずれかです。
ワークフローは、リポジトリ内で発生するイベント(プルリクエスト、ブランチのマージ、Issueの作成など)によってトリガーされます。ワークフローを作成する際に、どのイベントがトリガーとなるかを指定します。
イベントが発生してワークフローがトリガーされると、GitHubはランナー上で実行する1つ以上のジョブを起動します。その後、GitHubはワークフローに定義されたステップを最後まで自動的に実行します。これはすべて、ユーザーの操作を必要とせずに行われます。これがGitHub Actionsワークフローの基本的な仕組みです。
独自のワークフローを作成してGitHub Actionsにさらに慣れたい方は、スキルリポジトリにある「Hello GitHub Actions」スキル演習を試してみてください。
リポジトリのアクションを確認する
サンプルリポジトリに移動し、フォークした後、上部の「Actions」タブを選択してください。このページには、リポジトリのすべてのワークフローが表示されます。左側の列にある緑色の「New workflow」ボタンを選択します。すると、このリポジトリに対して推奨されるいくつかのワークフローが表示された新しいページに移動します。
推奨されるアクションのいずれかの「Configure」ボタンを選択してください(現時点ではどれを選んでも構いません)。これにより、YAMLで記述されたテンプレートが表示されたエディターが開きます。ワークフローを構築する際に理解しておく必要がある主なセクションは3つです:
- name: ワークフローの内容を説明します。
- on: ワークフローをトリガーする条件を定義します。
- jobs: このワークフローに関連する実際の作業が行われる場所です。
ワークフローを構築する
学ぶ最良の方法は実践することです。それでは、リポジトリに作成された新しいIssueに自動的にラベルを付ける簡単なワークフローを作成してみましょう。まず、サンプルリポジトリのメインページに戻り、お使いのマシンにクローンします。次に、action-startブランチに移動します。
GitHub ActionsはYAML構文を使用してワークフローを定義します。各ワークフローは、リポジトリ内の特別な.github/workflowsディレクトリにある.ymlファイルに保存されます。そのディレクトリに移動してください。
ワークフローファイルを作成する際は、内容が一目でわかる説明的な名前(例:build-and-test.yml、security-scanner.yml)を使用することをお勧めします。今回は新しいIssueにラベルを付けるワークフローを作成したいので、このディレクトリにlabel-new-issue.ymlという名前の新しいファイルを作成します。ファイルの先頭に以下を追加します:
name: Label New Issues次に、このワークフローをトリガーする条件を決定します。GitHubには多くのトリガーオプションがあり、すべてトリガーワークフローのリファレンスドキュメントに記載されています。今回は、誰かがIssueを開いたときにこのワークフローをトリガーしたいと思います。YAMLファイルに以下のコードを追加します。
on:
issues:
types: [opened]さて、いよいよ実際の作業が行われるジョブセクションです。まず、ジョブに名前を付け、実行するマシンの種類を選択します。GitHubは、Ubuntu、Windows、macOSのさまざまなバージョンがインストールされたホステッドランナーを提供しています。
このジョブをlabel-issuesと名付け、ubuntu-latestで実行しましょう。これにより、GitHubは利用可能な最新バージョンのUbuntuランナーをプールから取得します。
また、アクションがリポジトリの内容を読み取り、ラベルを追加できるようにするための権限を設定する必要があります。ジョブ内でpermissionsキーワードを使用すると、そのジョブ内のすべてのアクションと実行コマンドに指定されたアクセス権が付与されます。
jobs:
label-issues:
runs-on: ubuntu-latest
permissions:
issues: write
contents: read次に、実際のステップを追加します。ここで、ジョブ内で何が行われるかを定義します。
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Add triage label
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
LABEL: "triage"
run: gh issue edit "$ISSUE_NUMBER" --add-label "$LABEL"ここでは2つの主要なキーワード、usesとrunが使われています。
usesは、GitHub Marketplaceから事前構築済みアクションを呼び出します。これらはオープンソースの再利用可能なアクションで、一般的なタスクを処理するため、すべてを一から作成する必要がありません。例としては、コードのチェックアウトやNode.jsのセットアップなどがあります。runは、コマンドやスクリプトを実行します。このキーワードは、1行のコードでも複数行のスクリプトでも同じように使用されます。
envセクションは環境変数を定義する場所です。runセクションでGitHub CLIを使用しているため、アクションがリポジトリにアクセスできるようにアクセストークンを提供する必要があります。これはGH_TOKEN環境変数を設定することで行います。GitHubはジョブ作成時に自動的に一時的なアクセストークンを生成し、GITHUB_TOKEN変数として提供します。
また、更新したいIssueと追加したいラベルを示す変数も必要です。この機能を動作させるには、そのラベルがすでにリポジトリに存在している必要があります。
おめでとうございます!これで最初のワークフローが完成しました!
ワークフローをテストする
ワークフローが完成したので、テストしてみましょう。まず、これらの変更をリポジトリにプッシュし、mainブランチにマージする必要があります。コードのプッシュ方法についてご不明な点があれば、リポジトリへのコード追加方法を説明した以前のGitHub初心者向けエピソードを参照してください。
ワークフローがアップロードされたら、動作を確認してみましょう。ブラウザでリポジトリに移動し、「Issues」タブを選択します。右上隅にある緑色の「New issue」ボタンを選択します。ワークフローのテストであることがわかるタイトルと説明を入力し、ウィンドウ下部の「Create」をクリックします。
GitHubがIssueを作成すると、Issueの履歴が表示されるページに自動的に移動します。すべて正常に動作していれば、Issue作成から数秒以内に「triage」ラベルが追加されているのが確認できるはずです。
これで、特定のトリガーに基づいてアクションが自動的にタスクを処理する便利さがお分かりいただけたでしょう。
現在のワークフローを確認する
ウィンドウ上部の「Actions」タブを選択します。左側の列にすべてのアクションが表示されます。「Label New Issues」を選択して、このワークフローの実行履歴のみを表示するようにフィルタリングします。
先ほど作成したテストIssueに対応する一番上のワークフローを選択します。これにより、この特定のワークフローインスタンスに関する詳細情報が表示されるページに移動します。メインウィンドウで「label-issues」を選択します。
これで、ワークフローを完了するために実行されたすべてのステップを確認できます。これは、ワークフローが期待通りに動作しない場合のデバッグに非常に役立ちます。このワークフローを再実行したい場合は、右上の「Re-run all jobs」ボタンをクリックします。
ワークフローを一時停止する必要がある場合は、「Actions」タブから行えます。そのタブに戻り、左側の列で再度「Label New Issues」を選択します。検索ボックスの隣、右側のウィンドウにある3点リーダー(…)をクリックします。表示されるオプションの1つが「Disable workflow」です。これを選択すると、ワークフローの実行は停止しますが、リポジトリから削除されるわけではないため、後で再有効化できます。
「Actions」タブは、すべてのワークフローを表示および管理する場所です。このビューから、デプロイメント、ランナー、メトリクス、パフォーマンス、キャッシュを確認できます。また、このタブから直接ワークフローファイルを表示および編集することも可能です。
次は何をしましょうか?
GitHub ActionsはIssueへのラベル付けだけではありません。パッケージの公開、新しいコントリビューターへの歓迎メッセージ送信、コードのビルドとテスト、セキュリティチェックの実行など、さまざまなアクションを作成できます。
GitHub Actionsのドキュメントを確認して学習を続け、独自のワークフローを作成して実験を始めましょう。
Happy coding!
レベルアップする準備はできましたか?これらの実践的なGitHubスキル演習をチェックして、GitHub Actionsで迅速に自動化ワークフローを構築しましょう >
「GitHub初心者向け: GitHub Actionsを使い始める」は、The GitHub Blogで最初に公開されました。
原文を表示
Welcome back to our ongoing GitHub for Beginners series, now in its third season! Our previous episode was the first of the season, and we talked about getting started with GitHub Issues and Projects. This time we’re jumping into another very popular GitHub feature called GitHub Actions. By the end of this post, you’ll know how to use GitHub Actions and will have created your first automated workflow.
As always, if you prefer to watch the video or want to reference it, we have all of our GitHub for Beginners episodes available on YouTube. (And you can watch the full video of this tutorial above!)
What are GitHub Actions?
GitHub Actions is a Continuous Integration/Continuous Delivery (CI/CD) and automation platform built right into GitHub. You’ll often hear people call these “action workflows” or just “workflows.” It allows you to automate repetitive tasks and deployment processes using YAML files stored in your repository. You might use GitHub Actions for tasks like running vulnerability scans, tests, creating releases, or even reminding your team about important updates.
Action workflows are triggered by GitHub events like pushes, pull requests, or schedules, and they run in a virtual environment.
How workflows work
Before jumping right into the details about how workflows do their job, we need to define some essential terms.
Event: A specific activity that triggers a workflow (e.g., pushing code, opening a pull request, creating an issue, etc.)
Hosted runners: A virtual machine that executes jobs in a workflow. GitHub provides hosted runners, or you can set up your own self-hosted runners.
Jobs: A set of steps executed within the same runner. Each step is either a shell command or a prebuilt action from the GitHub Marketplace.
Workflows are triggered by events in a repository. This can be something like a pull request, merging a branch, or even opening an issue. When you create the workflow, you determine what the triggering event is.
When that event fires and triggers the workflow, GitHub spins up one or more jobs that execute on a runner. GitHub then follows the steps you programmed into the workflow until it reaches the end. And it does this all on its own, without any interaction necessary on your behalf. This is essentially how the GitHub Action workflow works.
Want more practice creating your own workflows and getting more familiar with GitHub Actions? Try out the Hello GitHub Actions skill exercise in our skills repository.
Looking at a repository’s actions
Navigate to our sample repository, fork it, then select the Actions tab at the top. This page lists all of the current workflows for the repository. Select the green New workflow button in the left-hand column. This brings you to a new page, which presents several suggested workflows for this repository.
Select the Configure button for one of the suggested actions. For right now, it doesn’t matter which one you select. This pulls up an editor with a template written in YAML with three main sections you need to know when building an action workflow:
Name: Describes what your workflow does.
On: Defines what triggers the workflow.
Jobs: Where the actual work associated with this workflow happens.
Building a workflow
One of the best ways to learn is by doing, so let’s go ahead and create a simple workflow that will automatically label new issues created in our repository. First, head back to the main page for the sample repository and clone it to your machine. Then, navigate to the action-start branch.
GitHub Actions uses YAML syntax to define workflows. Each workflow is stored in a .yml file in a special .github\workflows directory in your repository. Go ahead and navigate to that directory.
When creating workflow files, you should use descriptive names that tell you what they do at a glance (e.g., build-and-test.yml, security-scanner.yml). Since we know that we want to have this workflow label new issues, create a new file in this directory and call it label-new-issue.yml. Add the following to the top of the file:
Name: Label New Issues
Now we need to decide what will trigger this workflow. GitHub has tons of trigger options, all of which are listed in our trigger workflows reference documentation, so you can choose based on what you’re building. In our case, we specifically want to have this workflow trigger whenever someone opens an issue. We do this by adding the following code to the bottom of the YAML file.
on:
issues:
types: [opened]
Now we get to the good stuff: the jobs section, where the actual work happens. The first thing we’ll do is give our job a title and choose what type of machine runs it. GitHub provides hosted runners with different versions of Ubuntu, Windows, and macOS.
Let’s call this job label-issues and run it on ubuntu-latest. This tells GitHub to grab a machine from their pool of Ubuntu runners using the latest version available.
We also need to add permissions so the action can read the content of the repository and add the label. When we use the permissions keyword inside of a job, all actions and run commands within that job gain the access rights specified.
jobs:
label-issues:
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
Okay, now we need to add the actual steps. This is where we define what happens in the job.
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Add triage label
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
LABEL: "triage"
run: gh issue edit "$ISSUE_NUMBER" --add-label "$LABEL"
You’ll see two main keywords here: uses and run.
When you see uses, that’s pulling in a prebuilt action from the GitHub Marketplace. These are open-source reusable actions that handle common tasks, so you don’t have to write everything from scratch. Some examples of these type of actions include checking out your code or setting up NodeJS.
When you see run, that’s executing a command or script. This same keyword is used if it’s just a single line of code or a script that spans multiple lines.
The env section is where we define our environment variables. Since we are using the GitHub CLI in our run section, we need to provide an access token for the action to be able to access the repository. We do this by setting the GH_TOKEN environment variable. GitHub automatically provides a temporary access token when it creates this job and provides it in the GITHUB_TOKEN variable.
We also need to have variables to indicate which issue we want to update, as well as which label we want to add. In order for this to work, that label needs to already exist in the repository.
Congratulations! You’ve now successfully completed authoring your first workflow!
Testing your workflow
Now that we have the workflow, it’s time to test it. In order to do so, we first need to push these changes up to the repository and merge them into the main branch. So go ahead and do that now. Once it’s in the main branch, we can see it in action. If you have any questions about how to push your code, you can check out our earlier GitHub for Beginners episode about adding code to your repository.
Once your workflow is uploaded, let’s see it work. Navigate to the repository in your browser, and select the Issues tab. In the top-right corner, select the green New issue button. Provide a title and description indicating that you’re testing the workflow, and then click Create at the bottom of the window.
GitHub will create the issue and automatically bring you to the page showing the issue’s history. Assuming everything is working correctly, you’ll see the triage label added within seconds of when you created the issue.
Nice work! You can now see how convenient it can be to have some actions automatically handle tasks based on specific triggers.
Reviewing current workflows
Select the Actions tab at the top of the window. You’ll see all of the actions listed in a column on the left-hand side of the window. Select Label New Issues to filter the view to show just the times this workflow has run.
Select the top workflow, which should be for the test issue you created in the previous step. This brings you to a detail page where you can see more information about this specific instance of the workflow. Select label-issues in the main window.
Now you can see all of the steps taken in order to complete the workflow. This can be very useful for debugging purposes if one of your workflows is not working as expected. If you want to rerun this workflow, you can do so by clicking the Re-run all jobs button in the top-right.
If there’s ever a situation where you need to pause a workflow, you can do that from the Actions tab. Navigate back to that tab, and once again select Label New Issues in the left column. Click the three dots (…) in the window on the right, next to the search box. One of the options is Disable workflow. If you select this, it will stop the workflow from running, but it will still exist in the repository, so you can enable it later.
The Actions tab is where you’ll go to see and manage all of your workflows. You can check deployments, runners, metrics, performance, and caches all from this view. You can also view and edit your workflow files directly from this tab.
What’s next?
GitHub Actions can do a lot more than just label your issues. You can create actions to publish packages, greet new contributors, build and test your code, and even run security checks.
Keep learning about GitHub Actions by checking out our GitHub Actions documentations and start experimenting with creating your own workflows.
Happy coding!
Ready to level up? Check out these hands-on GitHub Skills exercises to get you building fast automate workflows with GitHub Actions >
The post GitHub for Beginners: Getting started with GitHub Actions appeared first on The GitHub Blog.
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み