将teamcity参数传递给terraform

时间:2019-03-26 15:28:34

标签: docker teamcity terraform

我正在尝试为涉及AWS的代码构建btd管道。我正在使用在其中运行docker的teamcity。对于AWS资源动态创建,我正在使用terraform。我在teamcity环境变量中设置了我的AWS访问权限和密钥,但是将这些凭据传递给terraform cmd无效

dockerfile拥有所有terraform命令,虽然terraform可以初始化,但是由于无效的AWS凭证而无法运行apply

docker文件

RUN apt-get -y install wget unzip
RUN wget https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip
RUN unzip terraform_0.11.11_linux_amd64.zip
RUN mv terraform /usr/local/bin/

ADD main.tf /usr/local/bin

RUN chmod +x /usr/local/bin

RUN terraform init
RUN terraform apply -auto-approve /usr/local/bin/terraform -var 'access_key=${AWS_ACCESS_KEY}' -var 'secret_key=${AWS_SECRET_KEY}'

AWS_ACCESS_KEY和AWS_SECRET_KEY作为环境变量存储在teamcity中。 main.tf

provider "aws" {
    region = "region-name"
}

resource "aws_instance" "aws_test" {
  ami = "ami"
  instance_type = "t2.micro"

  subnet_id = "subnet-id"

  #Security group
  security_groups = ["security-group"]

}

1 个答案:

答案 0 :(得分:0)

有关使用ARG和ENV指令的详细信息,请参阅Dockerfile参考。

https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

在Dockerfile中

`ARG CODE_VERSION =最新 FROM base:$ {CODE_VERSION} CMD / code / run-app

FROM Extras:$ {CODE_VERSION} CMD / code / run-extras`

要构建docker映像,可以使用以下命令。 docker build --build-arg some_variable_name=a_value

要在密码文件中传递密码,

version: "3.1"
services:

  database:
    image: postgres:9.6
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
    secrets:
      - postgres_password

secrets:
  postgres_password:
    external: true

https://github.com/docker-library/postgres/issues/111#issuecomment-293053904

在teamcity中,您可以配置隐藏参数,请参阅 http://pinter.org/archives/3681 enter image description here