我有以下地形代码:
provider "aws" {
region = "eu-central-1"
}
terraform {
backend "s3" {
bucket = "aws-project-terra"
key = "tf-state"
region = "eu-central-1"
}
}
module "ec2" {
source = "./modules/ec2"
ami = "ami-0d1ddd83282187d18"
instance_type = "t2.micro"
}
我通过 github 操作运行:
name: Terraform-ansible-apply
on:
workflow_dispatch:
jobs:
Terraform:
name: Terraform Plan & Apply
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Terraform Setup
uses: hashicorp/setup-terraform@v1
- name: Terraform Init
run: terraform init
working-directory: ./Terraform
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Terraform Validate
run: terraform validate
working-directory: ./Terraform
- name: Terraform Apply
id: tf-apply
run: terraform apply -auto-approve
working-directory: ./Terraform
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Save Terraform Outputs
id: save-outputs
run: |
echo 'sg=$(terraform output -raw ec2_sg)' >> "$GITHUB_ENV"
working-directory: ./Terraform
- name: Use the value
id: step_two
run: |
echo ${{ env.sg }}
问题是我在最后一步中没有获得环境变量 env.sg 。 我收到错误:
Run echo $(terraform output -raw ec2_sg)
/home/runner/work/_temp/a9903635-fab4-4149-89fe-056eb08152ef/terraform-bin output -raw ec2_sg
Warning: No outputs found
The state file either has no outputs defined, or all the defined outputs
are empty. Please define an output in your configuration with the `output`
keyword and run `terraform refresh` for it to become available. If you are
using interpolation, please verify the interpolated value is not empty. You
can use the `terraform console` command to assist.
::debug::Terraform exited with code 0. ::debug::stdout:
Warning: No outputs found
The state file either has no outputs defined, or all the defined outputs
are empty. Please define an output in your configuration with the `output`
keyword and run `terraform refresh` for it to become available. If you are
using interpolation, please verify the interpolated value is not empty. You
can use the `terraform console` command to assist.
::debug::stderr: ::debug::exitcode: 0 ::set-output name=stdout::
Warning: No outputs found
The state file either has no outputs defined, or all the defined outputs
are empty. Please define an output in your configuration with the `output`
keyword and run `terraform refresh` for it to become available. If you are
using interpolation, please verify the interpolated value is not empty. You
can use the `terraform console` command to assist.
::set-output name=stderr:: ::set-output name=exitcode::0
我在 github 工作流程日志中看到输出:
Outputs:
ec2_sg = "sg-015sdf8ad839881"
instance_ip = "3.75.232.345"
subnet_id = "subnet-05d345458f6c1a9"
我也可以从命令行打印输出:
terraform output -raw ec2_sg
sg-01599f8ad873c9881
那么为什么我不能通过 github 环境变量使用这个输出?
您需要在
setup-terraform操作中将
terraform_wrapper
设置为 false。有一个未决问题 https://github.com/hashicorp/setup-terraform/issues/20 可以更改此行为。
尝试将此部分添加到您的管道中
env:
ec2_sg: ""