Commit 0c121fca authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

refactor: use $environment_type & $environment_name

Deprecated $appname and $env dynamic variables with $environment_name & $environment_type
With backward compatibility (legacy vars still usable)
parent 93e8bca2
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -115,13 +115,13 @@ deployment and cleanup using the [`gcloud` CLI](https://cloud.google.com/sdk/gcl

The deployment script is searched as follows:

1. look for a specific `gcp-deploy-$env.sh` in the `$GCP_SCRIPTS_DIR` directory in your project (e.g. `gcp-deploy-staging.sh` for staging environment),
1. look for a specific `gcp-deploy-$environment_type.sh` in the `$GCP_SCRIPTS_DIR` directory in your project (e.g. `gcp-deploy-staging.sh` for staging environment),
2. if not found: look for a default `gcp-deploy.sh` in the `$GCP_SCRIPTS_DIR` directory in your project,
3. if not found: the deployment job will fail.

The cleanup script is searched as follows:

1. look for a specific `gcp-cleanup-$env.sh` in the `$GCP_SCRIPTS_DIR` directory in your project (e.g. `gcp-cleanup-staging.sh` for staging environment),
1. look for a specific `gcp-cleanup-$environment_type.sh` in the `$GCP_SCRIPTS_DIR` directory in your project (e.g. `gcp-cleanup-staging.sh` for staging environment),
2. if not found: look for a default `gcp-cleanup.sh` in the `$GCP_SCRIPTS_DIR` directory in your project,
3. if not found: the cleanup job will fail.

@@ -141,8 +141,8 @@ In order to be able to implement some **genericity** in your scripts and templat
2. any [custom variable](https://docs.gitlab.com/ee/ci/variables/#custom-environment-variables)
    (ex: `${SECRET_TOKEN}` that you have set in your project CI/CD variables)
3. **dynamic variables** set by the template:
    * `${appname}`: the application target name to use in this environment (ex: `myproject-review-fix-bug-12` or `myproject-staging`)
    * `${env}`: the environment type (`review`, `integration`, `staging` or `production`)
    * `${environment_name}`: the application target name to use in this environment (ex: `myproject-review-fix-bug-12` or `myproject-staging`)
    * `${environment_type}`: the environment type (`review`, `integration`, `staging` or `production`)
    * `${hostname}`: the environment hostname, extracted from `${CI_ENVIRONMENT_URL}` (has to be explicitly declared as [`environment:url`](https://docs.gitlab.com/ee/ci/yaml/#environmenturl) in your `.gitlab-ci.yml` file)
    * `${gcp_project_id}`: the current Google Cloud project ID associated to your environment

+22 −15
Original line number Diff line number Diff line
@@ -288,27 +288,30 @@ stages:

  # application deployment function
  function deploy() {
    export env=$1
    export appname=$2
    export environment_type=$1
    export environment_name=$2
    export gcp_project_id=$3
    export environment_url=$4

    # backwards compatibility
    export env=$environment_type
    export appname=$environment_name

    # extract hostname from $environment_url
    hostname=$(echo "$environment_url" | awk -F[/:] '{print $4}')
    export hostname

    log_info "--- \\e[32mdeploy\\e[0m (env: \\e[33;1m${env}\\e[0m)"
    log_info "--- \$appname: \\e[33;1m${appname}\\e[0m"
    log_info "--- \$env: \\e[33;1m${env}\\e[0m"
    log_info "--- \\e[32mdeploy\\e[0m"
    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$hostname: \\e[33;1m${hostname}\\e[0m"
    log_info "--- \$gcp_project_id: \\e[33;1m${gcp_project_id}\\e[0m"

    # unset any upstream deployment env & artifacts
    unset environment_name
    unset environment_type
    rm -f gcloud.env
    rm -f environment_url.txt

    deployscript=$(ls -1 "$GCP_SCRIPTS_DIR/gcp-deploy-${env}.sh" 2>/dev/null || ls -1 "$GCP_SCRIPTS_DIR/gcp-deploy.sh" 2>/dev/null || echo "")
    deployscript=$(ls -1 "$GCP_SCRIPTS_DIR/gcp-deploy-${environment_type}.sh" 2>/dev/null || ls -1 "$GCP_SCRIPTS_DIR/gcp-deploy.sh" 2>/dev/null || echo "")
    if [[ -f "$deployscript" ]]
    then
      log_info "--- deploy script (\\e[33;1m${deployscript}\\e[0m) found: execute"
@@ -328,21 +331,25 @@ stages:
    else
      echo "$environment_url" > environment_url.txt
    fi
    echo -e "environment_type=$env\\nenvironment_name=$appname\\nenvironment_url=$environment_url" > gcloud.env
    echo -e "environment_type=$environment_type\\nenvironment_name=$environment_name\\nenvironment_url=$environment_url" > gcloud.env
  }

  # environment cleanup function
  function delete() {
    export env=$1
    export appname=$2
    export environment_type=$1
    export environment_name=$2
    export gcp_project_id=$3

    log_info "--- \\e[32mdelete\\e[0m (env: ${env})"
    log_info "--- \$appname: \\e[33;1m${appname}\\e[0m"
    log_info "--- \$env: \\e[33;1m${env}\\e[0m"
    # backwards compatibility
    export env=$environment_type
    export appname=$environment_name

    log_info "--- \\e[32mdelete"
    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$gcp_project_id: \\e[33;1m${gcp_project_id}\\e[0m"

    cleanupscript=$(ls -1 "$GCP_SCRIPTS_DIR/gcp-cleanup-${env}.sh" 2>/dev/null || ls -1 "$GCP_SCRIPTS_DIR/gcp-cleanup.sh" 2>/dev/null || echo "")
    cleanupscript=$(ls -1 "$GCP_SCRIPTS_DIR/gcp-cleanup-${environment_type}.sh" 2>/dev/null || ls -1 "$GCP_SCRIPTS_DIR/gcp-cleanup.sh" 2>/dev/null || echo "")
    if [[ -f "$cleanupscript" ]]
    then
      log_info "--- cleanup script (\\e[33;1m${cleanupscript}\\e[0m) found: execute"