Commit f39ae7ec authored by Cédric OLIVIER's avatar Cédric OLIVIER Committed by Pierre Smeyers
Browse files

fix: remove gitlab authent from terraform init to allow authent without init

parent 9d8d9cdc
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -782,6 +782,19 @@ stages:
      export TF_HTTP_RETRY_WAIT_MIN="${TF_HTTP_RETRY_WAIT_MIN:-5}"
  }

  function tf_gitlab_auth(){
    # set auth token for GitLab (if not already set)
    token_varname_for_gitlab="TF_TOKEN_${CI_SERVER_HOST//./_}"
    token_value_for_gitlab=$(eval echo "\$$token_varname_for_gitlab")
    if [[ "$token_value_for_gitlab" ]]
    then
      log_info "Auth token for GitLab registry (\$$token_varname_for_gitlab) already set: use it"
    else
      log_info "Auth token for GitLab registry (\$$token_varname_for_gitlab) not set: use \$CI_JOB_TOKEN"
      export "$token_varname_for_gitlab"="$CI_JOB_TOKEN"
    fi
  }

  function tf_init() {
    opts=${ENV_INIT_OPTS:-$TF_INIT_OPTS}
    extra_opts=${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}
@@ -809,12 +822,7 @@ stages:
    export TF_VAR_environment_name=$environment_name
    export TF_VAR_environment_slug=$environment_slug

    # set auth token for GitLab (if not already set)
    token_var_for_gitlab="TF_TOKEN_${CI_SERVER_HOST//./_}"
    if [[ -z "$(eval echo \$"$token_var_for_gitlab")" ]]; then
      log_info "setting token for GitLab registry (\$$token_var_for_gitlab)..."
      export "$token_var_for_gitlab"="$CI_JOB_TOKEN"
    fi


    # make output dir
    mkdir -p "$TF_OUTPUT_DIR"
@@ -1057,12 +1065,15 @@ stages:

.tf-commands:
  default:
    gitlab_auth: tf_gitlab_auth
    init: tf_init
    select_workspace: tf_workspace_select
    test: tf_test
    plan: tf_plan
    apply: tf_apply
    destroy: tf_destroy
  gitlab_auth:
    - !reference [ .tf-commands, default, gitlab_auth ]
  init:
    - !reference [ .tf-commands, default, init ]
  select_workspace:
@@ -1096,6 +1107,7 @@ stages:
    - export environment_name=$CI_ENVIRONMENT_NAME
    - export environment_slug=$CI_ENVIRONMENT_SLUG
    - !reference [.tf-scripts]
    - !reference [ .tf-commands, gitlab_auth ]
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - cd "$TF_PROJECT_DIR"
  cache: