GitHub Actionsについて勉強するため、ChatGPTに質問したり公式ドキュメントを読んだりしました。
まずGitHub ActionsからTerraformを実行する方法をChatGPTに質問しました。
入力
How can I use terraform from GitHub Actinos?
出力
To use Terraform from GitHub Actions, you can follow these steps:
First, create a workflow file in your GitHub repository, typically in the
.github/workflows
directory, with a name liketerraform.yaml
.In this workflow file, define the event trigger, such as a push to a specific branch or a pull request, that will trigger the workflow.
Next, add a job to the workflow file that will execute the Terraform commands. You can use one of the pre-built GitHub Actions for Terraform, such as
hashicorp/setup-terraform
orhashicorp/terraform-action
, to install and set up Terraform.Once Terraform is installed, you can use the
terraform
command to apply your infrastructure changes. You can specify the Terraform configuration files and the necessary environment variables as inputs to theterraform
command.Finally, you can use the GitHub Actions
actions/upload-artifact
action to upload any relevant files, such as Terraform state files or log files, as artifacts.Here is an example workflow file that uses the
hashicorp/setup-terraform
action and applies a Terraform configuration:name: Terraform on: push: branches: - main jobs: terraform: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Setup Terraform uses: hashicorp/setup-terraform@v1 - name: Terraform Init run: terraform init - name: Terraform Plan run: terraform plan - name: Terraform Apply run: terraform apply -auto-approve - name: Upload artifacts uses: actions/upload-artifact@v2 with: name: terraform-output path: | terraform.tfstate terraform.tfstate.backup
This workflow will trigger on pushes to the main branch, set up Terraform, run
terraform init
,terraform plan
, andterraform apply
, and then upload the Terraform state files as artifacts.Note that you should always take care to use best practices when storing sensitive information like API keys or access tokens in your GitHub Actions workflows. You can use the GitHub Actions secrets feature to securely store and use secrets in your workflows.
このworkflowファイル内でブランチへのpushやプルリクエストなど、workflowが実行されるトリガーを定義する
workflowファイルにTerraformコマンドを実行するjobを追加する。Terraformをインストールおよびセットアップするためにhasicorp/setup-terraformやhashicorp/terraform-actionなどのpre-built GitHub Actionsを使用できる
Terraformがインストールされたら、terraformコマンドを使用してインフラ構成の変更を適用できる。terraformコマンドへのinputとしてTerraformの設定ファイルと必要な環境変数を指定できる
最後にactions/upload-artifactなどのactionでTerraformのstateファイルやログファイルなどの関連ファイルをartifactsとしてアップロードできる
注意点として、GitHub Actionsのworkflow内でAPIキーやアクセストークンなどの機密情報を保存する場合は、GitHub Actionsのsecrets機能を使用してworkflow内で安全にsecretsを使用できるようにする必要がある。
という感じのようでした。
こういう感じの情報が書かれている公式ドキュメントがあるか聞いてみました。
入力
Could you give me an URL of official docs for this?
出力
Yes, of course! You can find the official documentation for using Terraform with GitHub Actions on the HashiCorp website:
This documentation provides an overview of using Terraform with GitHub Actions, including how to set up your workflow files, how to use pre-built GitHub Actions for Terraform, and how to securely store and use secrets in your workflows. It also includes examples and best practices for using Terraform with GitHub Actions.
Additionally, you may find the following links helpful:
These resources provide more information on GitHub Actions in general, including how to create and customize workflows and how to use and contribute to the GitHub Actions Marketplace.
GitHub ActionsからTerraformを実行する方法についての詳細は以下のドキュメントを読んで
GitHub Actions自体の情報は以下を読むのが良さそうです。
あと、GitHub Actionsのドキュメントなどでrunnerという用語がよく出てくるので質問してみました。
入力
what is the runner machine? That is the machine for executing GitHub Actions workflow? If so, GitHub Actions uses 'runner machine' not 'Docker container'?
出力
Yes, you are correct! The runner machine is the machine that is used by GitHub Actions to execute your workflows. When you run a GitHub Actions workflow, GitHub spins up a runner machine in a cloud environment and executes your workflow steps on that machine.
GitHub Actions does not use Docker containers by default, although it is possible to use Docker containers in your workflows if you choose to. Instead, GitHub Actions uses a virtual machine (VM) that is created specifically for your workflow run.
The runner machine is where the code for your repository is checked out by the
actions/checkout
action, as I mentioned earlier. This enables your workflow to access and modify the code as needed.When your workflow completes, the runner machine is destroyed and any resources that were created during the workflow (such as temporary files or environment variables) are cleaned up.
Overall, the runner machine is a key part of the GitHub Actions infrastructure, as it enables your workflows to execute in a consistent and isolated environment.
GitHub Actionsのworkflowはrunnerが実行しているということのようです。Dockerコンテナを使うこともできるようですが、デフォルトはVM(仮想マシン)を使うとのこと。 この辺りの違いはGitHub Actionsの基本的なユースケースではあまり気にしなくても良い気がするので自分はデフォルトのまましばらく使おうと思います。
そして、actions/checkoutなどでリポジトリのコードをrunnerにチェックアウトして、workflowがコードにアクセスできるようになり、workflowが全て終了したらrunner machineは破棄されてworkflowの中で作成されたファイルや環境変数などは消去される、という流れのようです。
一応runner machineについてのドキュメントのURLも聞いてみました。
入力
Please give me an URL of official docs for GItHub Actions runner machine.
出力
Certainly! Here are some resources from the official GitHub Actions documentation that provide more information about the runner machine:
Virtual environments for GitHub-hosted runners: This page provides an overview of the virtual machine (VM) environments that are used by GitHub-hosted runners, including the different types of VMs that are available and the software that is pre-installed on each environment.
About self-hosted runners: This page provides information about self-hosted runners, which are runners that you can host on your own machines instead of using GitHub-hosted runners. This can be useful if you need to customize the runner environment, or if you need to run workflows in a private network.
Virtual environments used for GitHub-hosted runners: This is a GitHub repository that provides detailed information about the virtual environments that are used by GitHub-hosted runners. This includes the specific versions of software that are pre-installed on each environment, as well as any other configuration details that may be relevant.
I hope you find these resources helpful!
まずはGitHub Actionsの使い方からいろいろ試したいので、実行環境について調べる機会はしばらく無いと思いますが、必要になれば上記のドキュメントも読んでみたいと思います。