gpt4 book ai didi

terraform - 如何在 terraform entreprise 中使用 npm?

转载 作者:行者123 更新时间:2023-12-05 03:55:10 26 4
gpt4 key购买 nike

我试图在用这段代码压缩它之前构建一个 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"
}

此代码在我的本地计算机上运行良好,但在 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

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

最佳答案

我发现的唯一可能性是在使用之前在 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"
}

command 部分,我使用 curl 下载 nodejs,然后将其(临时)添加到路径中,以便我可以 npm installnpm run build之后

关于terraform - 如何在 terraform entreprise 中使用 npm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60397854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com