Terraform GitHub Actions 项目教程
1. 项目的目录结构及介绍
.
├── README.md
├── action.yml
├── entrypoint.sh
├── main.tf
├── outputs.tf
├── variables.tf
└── versions.tf
- README.md: 项目说明文档,包含项目的基本信息和使用指南。
- action.yml: GitHub Actions 的配置文件,定义了动作的输入、输出和运行步骤。
- entrypoint.sh: 项目的启动脚本,负责执行 Terraform 命令。
- main.tf: Terraform 的主配置文件,定义了基础设施的资源。
- outputs.tf: Terraform 的输出配置文件,定义了资源的输出值。
- variables.tf: Terraform 的变量配置文件,定义了可用的变量。
- versions.tf: Terraform 的版本配置文件,定义了所需的 Terraform 版本和提供者版本。
2. 项目的启动文件介绍
entrypoint.sh
entrypoint.sh
是一个 Bash 脚本,作为项目的启动文件。它负责执行 Terraform 命令,并处理相关的输入和输出。以下是 entrypoint.sh
的基本内容:
#!/bin/bash
# 设置 Terraform 命令
terraform init
terraform plan
terraform apply -auto-approve
该脚本首先初始化 Terraform 环境,然后生成执行计划,并自动应用该计划。
3. 项目的配置文件介绍
action.yml
action.yml
是 GitHub Actions 的配置文件,定义了动作的输入、输出和运行步骤。以下是 action.yml
的基本内容:
name: 'Terraform GitHub Actions'
description: 'Run Terraform commands in GitHub Actions'
inputs:
terraform_version:
description: 'Terraform version to use'
required: false
default: 'latest'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.terraform_version }}
该文件定义了一个名为 Terraform GitHub Actions
的动作,并接受一个可选的输入参数 terraform_version
,默认值为 latest
。
main.tf
main.tf
是 Terraform 的主配置文件,定义了基础设施的资源。以下是 main.tf
的基本内容:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
}
该文件定义了一个 Azure 资源提供者,并创建了一个名为 example-resources
的资源组。
variables.tf
variables.tf
是 Terraform 的变量配置文件,定义了可用的变量。以下是 variables.tf
的基本内容:
variable "location" {
description = "The Azure region to deploy resources"
default = "West Europe"
}
该文件定义了一个名为 location
的变量,默认值为 West Europe
。
outputs.tf
outputs.tf
是 Terraform 的输出配置文件,定义了资源的输出值。以下是 outputs.tf
的基本内容:
output "resource_group_name" {
description = "The name of the resource group"
value = azurerm_resource_group.example.name
}
该文件定义了一个名为 resource_group_name
的输出,值为创建的资源组的名称。
versions.tf
versions.tf
是 Terraform 的版本配置文件,定义了所需的 Terraform 版本和提供者版本。以下是 versions.tf
的基本内容:
terraform {
required_version = ">= 0.12"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.0"
}
}
}
该文件定义了所需的 Terraform 版本为 >= 0.12
,并指定了 AzureRM 提供者的版本为 `~> 2.0