我有 Gitlab CICD 管道来部署我的 terraform 更改,它使用 Gitlabhttp后端来存储状态文件。但是,暂时无法保存状态文件,并出现以下错误。
创建资源后发生错误,因此重新运行同一作业有时可以解决问题,但会重复资源。
\n导致此错误的原因可能是什么?
\ngitlab-ci.yml:
variables:\n TF_ROOT: ${CI_PROJECT_DIR}\n TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${CI_PROJECT_NAME}\n TF_VAR_app_key: ${APP_KEY}\n TF_VAR_api_key: ${API_KEY}\n TF_VAR_gitlab_token: ${CI_JOB_TOKEN}\n TF_VAR_environment: ${ENV}\n TF_CLI_CONFIG_FILE: $CI_PROJECT_DIR/.terraformrc\n\ndefault:\n image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest\n\ncache:\n key: ${CI_PROJECT_NAME}\n paths:\n - ${TF_ROOT}/.terraform\n\n\nbefore_script:\n - echo -e "credentials \\"$CI_SERVER_HOST\\" {\\n token = \\"$CI_JOB_TOKEN\\"\\n}" > $TF_CLI_CONFIG_FILE\n - cd ${TF_ROOT}\n\nstages:\n - prepare\n - validate\n - build\n - deploy\n\ninit:\n stage: prepare\n script:\n - gitlab-terraform -v\n - gitlab-terraform init\n\nvalidate:\n stage: validate\n script:\n - gitlab-terraform validate\n\nplan:\n stage: build\n script:\n - gitlab-terraform plan\n - gitlab-terraform plan-json\n artifacts:\n name: plan\n paths:\n - ${TF_ROOT}/plan.cache\n reports:\n terraform: ${TF_ROOT}/plan.json\n rules:\n - if: $CI_COMMIT_REF_NAME == "main" \n when: on_success\n\napply:\n stage: deploy\n environment:\n name: $TF_VAR_environment\n script:\n - gitlab-terraform apply --auto-approve\n dependencies:\n - plan\n artifacts:\n name: errored_tfstate\n paths:\n - ${TF_ROOT}/errored.tfstate\n when: on_failure\n rules:\n - if: $CI_COMMIT_REF_NAME == "main" \n when: manual\n allow_failure: false\nRun Code Online (Sandbox Code Playgroud)\n作业输出:
\n...\nIf you'd like to know more about provider signing, you can read about it here:\nhttps://www.terraform.io/docs/cli/plugins/signing.html\nTerraform has created a lock file .terraform.lock.hcl to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run "terraform init" in the future.\nTerraform has been successfully initialized!\n2021/11/07 13:14:21 [DEBUG] POST https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources/lock\n2021/11/07 13:14:21 [DEBUG] GET https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources\nmodule....: Creating...\n...\n...\n2021/11/07 13:14:23 [DEBUG] GET https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources\n2021/11/07 13:14:23 [DEBUG] POST https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources?ID=cc520c4e-21c1-b787-46bf-5b577a9bf594\n2021/11/07 13:14:23 [DEBUG] POST https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources?ID=cc520c4e-21c1-b787-46bf-5b577a9bf594 (status: 500): retrying in 5s (2 left)\n2021/11/07 13:14:29 [DEBUG] POST https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources?ID=cc520c4e-21c1-b787-46bf-5b577a9bf594 (status: 500): retrying in 10s (1 left)\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Failed to save state\n\xe2\x94\x82 \n\xe2\x94\x82 Error saving state: Failed to upload state: POST\n\xe2\x94\x82 https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources?ID=cc520c4e-21c1-b787-46bf-5b577a9bf594\n\xe2\x94\x82 giving up after 3 attempts\n\xe2\x95\xb5\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Failed to persist state to backend\n\xe2\x94\x82 \n\xe2\x94\x82 The error shown above has prevented Terraform from writing the updated\n\xe2\x94\x82 state to the configured backend. To allow for recovery, the state has been\n\xe2\x94\x82 written to the file "errored.tfstate" in the current working directory.\n\xe2\x94\x82 \n\xe2\x94\x82 Running "terraform apply" again at this point will create a forked state,\n\xe2\x94\x82 making it harder to recover.\n\xe2\x94\x82 \n\xe2\x94\x82 To retry writing this state, use the following command:\n\xe2\x94\x82 terraform state push errored.tfstate\n\xe2\x94\x82 \n\xe2\x95\xb5\n2021/11/07 13:14:40 [DEBUG] DELETE https://gitlab.operations.mcorg.com/api/v4/projects/16/terraform/state/resources/lock\n...\nUploading artifacts as "archive" to coordinator... ok id=248 responseStatus=201 Created token=****\nCleaning up file based variables\n00:00\nERROR: Job failed: command terminated with exit code 1\nRun Code Online (Sandbox Code Playgroud)\n
小智 0
我面临着一个不同的问题,但和你的问题一样。
\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Failed to save state\n\xe2\x94\x82 \n\xe2\x94\x82 Error saving state: HTTP error: 403\n\xe2\x95\xb5\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Failed to persist state to backend\n\xe2\x94\x82 \n\xe2\x94\x82 The error shown above has prevented Terraform from writing the updated\n\xe2\x94\x82 state to the configured backend. To allow for recovery, the state has been\n\xe2\x94\x82 written to the file "errored.tfstate" in the current working directory.\n\xe2\x94\x82 \n\xe2\x94\x82 Running "terraform apply" again at this point will create a forked state,\n\xe2\x94\x82 making it harder to recover.\n\xe2\x94\x82 \n\xe2\x94\x82 To retry writing this state, use the following command:\n\xe2\x94\x82 terraform state push errored.tfstate\n\xe2\x94\x82 \n\xe2\x95\xb5\nUploading artifacts for failed job\nRun Code Online (Sandbox Code Playgroud)\n将权限(在项目中)更改为开发人员之上的任何人。
\n就我而言,我将其更改为Maintainer或以上并且运行良好。
\n| 归档时间: |
|
| 查看次数: |
2384 次 |
| 最近记录: |