詹金斯无法 运行 "terraform apply --auto-approve"
Jenkins unable to run "terraform apply --auto-approve"
我需要使用 terraform 脚本在 AWS 上启动几个实例,我正在使用 jenkis 自动化整个过程,
pipeline{
agent any
tools {
terraform 'terraform'
}
stages{
stage('Git Checkout'){
steps{
git branch: 'main', credentialsId: 'gitlab id', url: 'https://gitlab.com/ndey1/kafka-infra'
}
}
stage('Terraform init'){
steps{
sh 'terraform init'
}
}
stage('Terraform apply'){
steps{
sh 'terraform apply --auto-approve'
}
}
}
}
前 2 阶段没问题,但在第 3 阶段出现错误
+ terraform apply --auto-approve
[31m╷[0m[0m
[31m│[0m [0m[1m[31mError: [0m[0m[1mNo configuration files[0m
[31m│[0m [0m
[31m│[0m [0m[0mApply requires configuration to be present. Applying without a
[31m│[0m [0mconfiguration would mark everything for destruction, which is normally not
[31m│[0m [0mwhat is desired. If you would like to destroy everything, run 'terraform
[31m│[0m [0mdestroy' instead.
[31m╵[0m[0m
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
但是当从本地应用代码时,即使在 terraform init 阶段它也能完美工作,但在 terraform 阶段应用它失败
当我在 /var/lib/jenkins/workspace/infra-kaka/terraform-aws-ec2-with-vpc 中应用 ls -ltra 时。这是输出
total 60
-rw-r--r-- 1 jenkins jenkins 1036 Aug 11 14:58 vpc.tf
-rw-r--r-- 1 jenkins jenkins 1710 Aug 11 14:58 variables.tf
-rw-r--r-- 1 jenkins jenkins 1551 Aug 11 14:58 security_group.tf
-rw-r--r-- 1 jenkins jenkins 68 Aug 11 14:58 provider.tf
-rw-r--r-- 1 jenkins jenkins 420 Aug 11 14:58 output.tf
-rw-r--r-- 1 jenkins jenkins 1674 Aug 11 14:58 oregonkeypair.pem
-rw-r--r-- 1 jenkins jenkins 235 Aug 11 14:58 kafka.config
-rw-r--r-- 1 jenkins jenkins 1732 Aug 11 14:58 instance.tf
-rw-r--r-- 1 jenkins jenkins 630 Aug 11 14:58 hosts.yml
-rw-r--r-- 1 jenkins jenkins 6658 Aug 11 14:58 hosts-ssl.yml
-rw-r--r-- 1 jenkins jenkins 94 Aug 11 14:58 ansible.cfg
-rw-r--r-- 1 jenkins jenkins 630 Aug 11 14:58 all.yml
-rw-r--r-- 1 jenkins jenkins 0 Aug 11 14:58 .gitkeep
drwxr-xr-x 2 jenkins jenkins 4096 Aug 11 14:58 .
drwxr-xr-x 5 jenkins jenkins 4096 Aug 16 12:35 ..
提议的问题
我会根据您对我的评论的回复进行编辑
我相当确定问题是 terraform apply
命令找不到您的配置文件。要解决此问题,我建议您 运行 在 terraform apply
命令上方添加 ls -ltra
以查看 terraform 配置文件是否存在于您正在 运行 中执行命令的同一目录中包含你的 tf 文件。
示例代码 #1
This assumes there is a terraform/ directory in your current directory that contains your tf file
你可以在apply命令末尾加上你的tf文件所在目录的相对路径,如下:
terraform apply terraform/
示例代码 #2
This code also assumes you have a terraform/ directory in the root of your repository that contains your tf file
此选项通过将当前目录更改为 terraform/ 目录来执行这些命令
dir('terraform') {
terraform init
terraform apply --auto-approve
}
您显然需要相应地更新您的目录路径。如前所述,您可以 运行 a pwd
查看您的位置,并 ls -ltra
查看当前目录中的文件
我需要使用 terraform 脚本在 AWS 上启动几个实例,我正在使用 jenkis 自动化整个过程,
pipeline{
agent any
tools {
terraform 'terraform'
}
stages{
stage('Git Checkout'){
steps{
git branch: 'main', credentialsId: 'gitlab id', url: 'https://gitlab.com/ndey1/kafka-infra'
}
}
stage('Terraform init'){
steps{
sh 'terraform init'
}
}
stage('Terraform apply'){
steps{
sh 'terraform apply --auto-approve'
}
}
}
}
前 2 阶段没问题,但在第 3 阶段出现错误
+ terraform apply --auto-approve
[31m╷[0m[0m
[31m│[0m [0m[1m[31mError: [0m[0m[1mNo configuration files[0m
[31m│[0m [0m
[31m│[0m [0m[0mApply requires configuration to be present. Applying without a
[31m│[0m [0mconfiguration would mark everything for destruction, which is normally not
[31m│[0m [0mwhat is desired. If you would like to destroy everything, run 'terraform
[31m│[0m [0mdestroy' instead.
[31m╵[0m[0m
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
但是当从本地应用代码时,即使在 terraform init 阶段它也能完美工作,但在 terraform 阶段应用它失败
当我在 /var/lib/jenkins/workspace/infra-kaka/terraform-aws-ec2-with-vpc 中应用 ls -ltra 时。这是输出
total 60
-rw-r--r-- 1 jenkins jenkins 1036 Aug 11 14:58 vpc.tf
-rw-r--r-- 1 jenkins jenkins 1710 Aug 11 14:58 variables.tf
-rw-r--r-- 1 jenkins jenkins 1551 Aug 11 14:58 security_group.tf
-rw-r--r-- 1 jenkins jenkins 68 Aug 11 14:58 provider.tf
-rw-r--r-- 1 jenkins jenkins 420 Aug 11 14:58 output.tf
-rw-r--r-- 1 jenkins jenkins 1674 Aug 11 14:58 oregonkeypair.pem
-rw-r--r-- 1 jenkins jenkins 235 Aug 11 14:58 kafka.config
-rw-r--r-- 1 jenkins jenkins 1732 Aug 11 14:58 instance.tf
-rw-r--r-- 1 jenkins jenkins 630 Aug 11 14:58 hosts.yml
-rw-r--r-- 1 jenkins jenkins 6658 Aug 11 14:58 hosts-ssl.yml
-rw-r--r-- 1 jenkins jenkins 94 Aug 11 14:58 ansible.cfg
-rw-r--r-- 1 jenkins jenkins 630 Aug 11 14:58 all.yml
-rw-r--r-- 1 jenkins jenkins 0 Aug 11 14:58 .gitkeep
drwxr-xr-x 2 jenkins jenkins 4096 Aug 11 14:58 .
drwxr-xr-x 5 jenkins jenkins 4096 Aug 16 12:35 ..
提议的问题
我会根据您对我的评论的回复进行编辑
我相当确定问题是 terraform apply
命令找不到您的配置文件。要解决此问题,我建议您 运行 在 terraform apply
命令上方添加 ls -ltra
以查看 terraform 配置文件是否存在于您正在 运行 中执行命令的同一目录中包含你的 tf 文件。
示例代码 #1
This assumes there is a terraform/ directory in your current directory that contains your tf file
你可以在apply命令末尾加上你的tf文件所在目录的相对路径,如下:
terraform apply terraform/
示例代码 #2
This code also assumes you have a terraform/ directory in the root of your repository that contains your tf file
此选项通过将当前目录更改为 terraform/ 目录来执行这些命令
dir('terraform') {
terraform init
terraform apply --auto-approve
}
您显然需要相应地更新您的目录路径。如前所述,您可以 运行 a pwd
查看您的位置,并 ls -ltra
查看当前目录中的文件