我尝试构建一个 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 呢?
我发现的唯一可能性是在使用之前在 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
再进行
归档时间: |
|
查看次数: |
2562 次 |
最近记录: |