如何在 terraform entreprise 中使用 npm?

ben*_*ite 4 terraform

我尝试构建一个 React.js 应用程序,然后使用以下代码压缩它:

resource "null_resource" "build" {
  triggers = {
    always_run = "${timestamp()}"
  }
  provisioner "local-exec" {
    command = "cd ${path.module}/.. && npm run build"
    environment = var.environment_variables
  }
}
data "archive_file" "source_zip" {
  depends_on = [null_resource.build]
  type        = "zip"
  source_dir  = "../build"
  output_path = "dist/source.zip"
}
Run Code Online (Sandbox Code Playgroud)

该代码在我的本地计算机上运行良好,但在 Terraform 云(https://www.terraform.io/)上运行失败。NPM 似乎没有安装在 terraform 运行的机器上。这是我得到的错误:

Error: Error running command 'cd ./.. && npm run build': exit status 127. Output: /bin/sh: 1: npm: not found
Run Code Online (Sandbox Code Playgroud)

那么,如何在 Terraform 云上安装 npm 呢?

ben*_*ite 5

我发现的唯一可能性是在使用之前在 terraform VM 上安装 node/npm。这是我的代码:


resource "null_resource" "build" {
  triggers = {
    always_run = "${timestamp()}"
  }
  provisioner "local-exec" {
    command = <<-EOF
      cd ${path.module}/.. &&\
      mkdir ./node_install &&\
      cd ./node_install &&\
      curl https://nodejs.org/dist/latest-v10.x/node-v10.19.0-linux-x64.tar.gz | tar xz --strip-components=1 &&\
      export PATH="$PWD/bin:$PATH" &&\
      cd .. &&\
      npm install &&\
      npm run build
    EOF

    environment = var.environment_variables
  }
}
data "archive_file" "source_zip" {
  depends_on = [null_resource.build]
  type        = "zip"
  source_dir  = "../build"
  output_path = "dist/source.zip"
}
Run Code Online (Sandbox Code Playgroud)

在本command节中,我使用curl下载nodejs,然后将其(暂时)添加到路径中,以便我可以npm install然后npm run build再进行