Open7

Learning GitHub Actions for DevOps CI/CD視聴メモ

ta.toshiota.toshio

Chapter 1 : Introduction to the GitHub Action Workflows

Overview of GitHub Action Workflow

Exploring Workflow Components – Events

Github Action Workflow Directory: .github/workflows

eventとは例えば、誰かがプルリクエストを作成したりリクエストを作成したり、課題をオープンしたり、リポジトリにコミットをプッシュしたりしたときに発生する。

また、スケジュール、REST APIへの投稿、または手動でワークフローの実行をトリガーすることもでる。

ドキュメントリファレンス
https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows

Exploring Workflow Components – Job, Step, and Runner

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Ex010 Git Action Demo.yml

ジョブは同じランナー上で実行されるワークフロー内のステップのセットである。各ステップは実行されるスクリプトまたは実行されるアクションである。ステップは順番に実行され、互いに依存し合う。各ステップは同じランナーマシン上で実行されるため、ステップ間でデータを転送することができます。例えば、アプリケーションをビルドするステップに続いて、前のステップでビルドされたアプリケーションをテストするステップを持つことができます。デフォルトでは、ジョブは依存関係を持たず、互いに並行して実行されますが、他のジョブとの依存関係を設定することができます。

Running the Workflow and Reading the Logs

workflow_dispatchの補足
workflow_dispatchイベントトリガーを使用するためには、ワークフローが最初にメインブランチ(デフォルトブランチ)にコミットされている必要があります。そうでなければ、このオプションは使用できません。ワークフローがメインブランチまたはデフォルトブランチにコミットされると、そのワークフローをフィーチャーブランチから使用し、実行することが可能になる。しかし、ワークフローがメインブランチにコミットされていなければ、このオプションは表示されない。

Lessons Learned
✘ How to create a workflow
✘ Event Trigger
✘ Job
✘ Step
✘ Runner
✘ How to run a workflow
✘ View Workflow run logs

ta.toshiota.toshio

Chapter 2 : GitHub Actions: Exploring the Features

Environment Variables

変数定義

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Custom Env Vars.yml

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Set Env Vars .yml#L9-L17

Default GitHub Variables

https://docs.github.com/en/actions/learn-github-actions/variables

デフォルトで設定されているenvを確認するサンプル

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/List_Default_GitHub_vars.yml

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/c3497b0f851fbaafce9b3d95731c2b767c01ef2a/Source Code/github/workflows/List_Default_GitHub_vars_demo2.yml#L11-L24

GitHub Actions Store Your Secrets and Passwords Securely

Setting -> Secrets / Actions

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Action Secrets.yml

GitHub_Artifacts

アーティファクトにより、ジョブ完了後のデータを永続化し、同じワークフロー内の別のジョブとデータを共有することができます。アーティファクトとは、ワークフロー実行中に生成されるファイルまたはファイルのコレクションです。

例えば以下の様のものが挙げられる

  • Log files and core dumps
  • Test results, failures, and screenshots
  • Binary or compressed files
  • Stress test performance output and code coverage results

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Artifacts.yml

GitHub Environments | How to Add Manual Approvals

環境ごとのシークレット変数を設定することができる
また承認フローも設定できる

参考
https://zenn.dev/kenpi/articles/af6fb5b4c2675e

https://dev.classmethod.jp/articles/github-actions-environment-secrets-and-environment-variables/

Sharing Values Between Steps and Jobs in a Workflow

Step間で値の受け渡し

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Passing values between steps.yml

Job間で値の受け渡し

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/Passing values between Jobs.yml

ta.toshiota.toshio

Chapter 4 : GitHub Actions for Infrastructure Deployment

Azureを使った例を紹介。Azureを使う予定はないので流し見。

Understanding the Need of Infrastructure as Code

Developing IAC Template with Bicep to Deploy Infrastructure - Part 1

Developing IAC Template with Bicep to Deploy Infrastructure - Part 2

Setting Up Authentication

Developing GitHub Actions Workflow for Infra Deployment – Part 1

Developing GitHub Actions Workflow for Infra Deployment – Part 2

ta.toshiota.toshio

Chapter 6 : Building, Pushing, and Deploying Docker Containers

https://github.com/PacktPublishing/Learning-GitHub-Actions-for-DevOps-CI-CD/blob/main/Source Code/github/workflows/techsckool-cicd-container.yml

dockerコマンドを使う準備

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

コンテナレジストリサービスへのログイン

Log in to Azure container registry
        uses: docker/login-action@v1.10.0
        with:
          registry: demo010.azurecr.io
          username: demo010
          password: sHidiT36xMp7b+T2No6TBOC6j7wZKlm3

githubレポジトリ名が大文字が含んでいるとエラーが起こったそうなのでその回避とのこと(Azure特有のものかも)

      - name: Lowercase the repo name and username
        run: echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}

docker imageをコンテナレジストリにpush

      - name: Build and push container image to registry
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: demo010.azurecr.io/${{ env.REPO }}:${{ github.sha }}
          file: ./WebApplication02/Dockerfile