.├── README.md├── environments│ ├── dev│ │ ├── main.tf│ │ └── provider.tf│ └── prod│ ├── cicd│ │ └── main.tf│ ├── local.tf│ ├── main.tf│ ├── provider.tf│ └── qta│ └── main.tf└── modules├── network│ ├── main.tf│ ├── outputs.tf│ ├── provider.tf│ └── variables.tf├── security_group│ ├── main.tf│ ├── outputs.tf│ ├── provider.tf│ └── variables.tf└── tke├── main.tf├── outputs.tf├── provider.tf└── variables.tf
environments和modules两个目录。environments为目录方式隔离环境dev和prod,用来给不同环境设置各自的配置,每个环境目录都是独立的根模块。modules为封装的资源信息,用以复用。本目录中包含 vpc、安全组和容器服务 TKE 的 Module 演示。https://github.com/${USER}/${PROJECT}/settings/secrets/actions设置环境变量。请替换为已复制的 SecretId 和 SecretKey。


terraform fmt、terraform init、terraform validate、terraform plan来检查代码和展示构建计划,方便判断是否执行部署。terraform plan的输出复合预期,那么就可以进行 merge 操作。terraform apply)的操作。示意图如下:
# This is a basic workflow to help you get started with Actionsname: CI# Controls when the workflow will runon:pull_request:# A workflow run is made up of one or more jobs that can run sequentially or in paralleljobs:# This workflow contains a single job called "build"build:# The type of runner that the job will run onruns-on: ubuntu-latestenv:TENCENTCLOUD_SECRET_KEY: ${{ secrets.TENCENTCLOUD_SECRET_KEY }}TENCENTCLOUD_SECRET_ID: ${{ secrets.TENCENTCLOUD_SECRET_ID }}# Steps represent a sequence of tasks that will be executed as part of the jobsteps:- uses: actions/checkout@v3- uses: hashicorp/setup-terraform@v2with:terraform_wrapper: false- name: check envrun: |if [ ! -d "environments/$GITHUB_HEAD_REF" ]; thenecho "*************************SKIPPING************************************"echo "Branch '$GITHUB_HEAD_REF' does not represent an oficial environment."echo "*********************************************************************"exit 1fi- name: terraform fmtid: fmtrun: terraform fmt -recursive -check- name: terraform initid: initworking-directory: environments/${{ github.head_ref }}run: terraform init- name: terraform validateid: validateworking-directory: environments/${{ github.head_ref }}run: terraform validate- name: terraform planid: planif: github.event_name == 'pull_request'working-directory: environments/${{ github.head_ref }}run: |plan_info=""dir_count=`ls -l | grep "^d" | wc -l`if [ $dir_count -gt 0 ]; thenfor dir in ./*/doenv=${dir%*/}env=${env#*/}echo ""echo "========> Terraform Plan <========"echo "At environment: ${{ github.head_ref }}"echo "At workspace: ${env}"echo "=================================="terraform workspace select ${env} || terraform workspace new ${env}plan_info="$plan_info\\n$(terraform plan -no-color)"doneelseplan_info="$(terraform plan -no-color)"fiplan_info="${plan_info//'%'/'%25'}"plan_info="${plan_info//$'\\n'/'%0A'}"plan_info="${plan_info//$'\\r'/'%0D'}"echo "::set-output name=plan_info::$plan_info"continue-on-error: true- uses: actions/github-script@v6if: github.event_name == 'pull_request'with:script: |const output = `#### Terraform Format and Style \\`${{ steps.fmt.outcome }}\\`#### Terraform Initialization \\`${{ steps.init.outcome }}\\`#### Terraform Validation \\`${{ steps.validate.outcome }}\\`#### Terraform Plan \\`${{ steps.plan.outcome }}\\`<details><summary>Show Plan</summary>\\`\\`\\`\\n${{ steps.plan.outputs.plan_info }}\\`\\`\\`</details>*Pushed by: @${{ github.actor }}, Action: \\`${{ github.event_name }}\\`*`;github.rest.issues.createComment({issue_number: context.issue.number,owner: context.repo.owner,repo: context.repo.repo,body: output})
name: Applyon:pull_request:types:- closedbranches:- mainjobs:build:if: github.event.pull_request.merged == trueruns-on: ubuntu-latestenv:TENCENTCLOUD_SECRET_KEY: ${{ secrets.TENCENTCLOUD_SECRET_KEY }}TENCENTCLOUD_SECRET_ID: ${{ secrets.TENCENTCLOUD_SECRET_ID }}steps:- uses: actions/checkout@v3- uses: hashicorp/setup-terraform@v2- name: terraform initid: initworking-directory: environments/${{ github.head_ref }}run: terraform init- name: terraform applyworking-directory: environments/${{ github.head_ref }}run: |dir_count=`ls -l | grep "^d" | wc -l`if [ $dir_count -gt 0 ]; thenfor dir in ./*/doenv=${dir%*/}env=${env#*/}echo ""echo "========> Terraform Apply <========"echo "At environment: ${{ github.head_ref }}"echo "At workspace: ${env}"echo "=================================="terraform workspace select ${env} || terraform workspace new ${env}terraform apply -auto-approvedoneelseterraform apply -auto-approvefi
文档反馈