GitHub Actionsでプルリクエストが作成された時とプルリクエストがmainブランチにマージされた時のjobを動かしてみる

GitHub Actionsの簡単な動作確認してみます。

まず新規にリポジトリを作成してローカルにcloneしました。 とりあえず空のREADMEを作成してmainブランチにプッシュしてみます。

touch README.md
git add README.md
git commit -m "add README.md"
git branch -M main
git push origin main

git branch -M mainの動作はなんとなくしか理解してなかったのでChatGPTに質問してみました。

入力

What does git branch -M main do?

出力

The git branch -M main command renames the current branch to main. This is useful when you want to rename your default branch from master to main, which has become a common practice in the Git community to avoid the use of non-inclusive language.

Here's what the command does in detail:

The git branch command is used to manage Git branches.

The -M option is short for --move, which is used to move or rename a branch.

main is the new name of the branch.

If you are currently on a branch other than main, the command will rename the current branch to main. If you are already on the main branch, the command will have no effect.

Here's an example of how to use the git branch -M main command:

$ git branch
* master
  feature-branch
$ git branch -M main
$ git branch
* main
  feature-branch

In this example, the current branch is master. Running git branch -M main renames the branch to main. Running git branch again shows that the current branch is now main.

次に別ブランチを作成します。

git checkout -b feature/github-actions-demo

.github/workflows/main.ymlを作成します。

mkdir -p .github/workflows && touch .github/workflows/main.yml

プルリクエスト作成時に「job-runs-when-pull-requests-created」のメッセージが出力されるように、.github/workflows/main.ymlは以下のように書いてみました。

name: "GitHub Actions demo20230422"
on:
    pull_request:
permissions:
    pull-requests: write
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: job-runs-when-pull-requests-created
              run: echo "job-runs-when-pull-requests-created"
              if: github.event_name == 'pull_request'

変更をリモートブランチにプッシュします。

git add .github/workflows/main.yml
git commit -m "add .github/workflows/main.yml"
git push origin feature/github-actions-demo

GitHubのプルリクエスト画面で「Create pull request」をクリックします。

GitHub Actionsが動作しました。

All checks have passedの表示が出ましたので、Detailsをクリックします。

想定通り「job-runs-when-pull-requests-created」のメッセージが出力されました。

次にプルリクエストがmainブランチにマージされる時のjobを追加してみます。

.github/workflows/main.yml

name: "GitHub Actions demo20230422"
on:
    pull_request:
    push:
        branches:
            - main
permissions:
    pull-requests: write
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: job-runs-when-pull-requests-created
              if: github.event_name == 'pull_request'
              run: echo "job-runs-when-pull-requests-created"
            - name: some-deployment
              if: github.event_name == 'push' && github.ref == 'refs/heads/main'
              run: echo "some-deployment"

変更をリモートブランチにプッシュします。

git add .github/workflows/main.yml
git commit -m "modify .github/workflows/main.yml"
git push origin feature/github-actions-demo

想定通りプルリクエストでは「some-deployment」は出力されませんでした。

Merge pull requestをクリックしてmainブランチにマージしてみます。

マージされた後GitHub ActionsがAll checks have passedになっているので、Detailsをクリックします。

こちらでも想定通りjob-runs-when-pull-requests-createdは出力されず、some-deploymentが出力されました。

なんとなくGitHub Actionsの動作を確認できました。次はGitHub ActionsからTerraformなどを実行してみたいと思います。