Commit 38dd0a69 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

refactor(script): retrieve function params directly from env

parent 029b0d6a
Loading
Loading
Loading
Loading
+36 −43
Original line number Diff line number Diff line
@@ -369,13 +369,26 @@ stages:
    awk '!/# *nosubst/{while(match($0,"[$%]{[^}]*}")) {var=substr($0,RSTART+2,RLENGTH-3);val=ENVIRON[var]; gsub(/["\\]/,"\\\\&",val); gsub("\n","\\n",val);gsub("\r","\\r",val); gsub("[$%]{"var"}",val)}}1'
  }

  # login to the hosting platform
  function {{ cookiecutter.template_prefix }}_login() {
    api_url=${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL}
    api_token=${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN}
    project=$ENV_PROJECT
    
    assert_defined "$api_url" 'Missing required API url'
    assert_defined "$api_token" 'Missing required API token'
    assert_defined "$project" 'Missing required project'
    
    {{ cookiecutter.cli_tool }} login --url="$api_url" --token="$api_token" --project="$project"
  }

  # application deployment function
  function deploy() {
    export environment_type=$1
    export environment_name=$2
    environment_url=$3
  function {{ cookiecutter.template_prefix }}_deploy() {
    export environment_type=$ENV_TYPE
    export environment_name=${ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}
    environment_url=${ENV_URL:-${{ cookiecutter.template_PREFIX }}_ENVIRONMENT_URL}
    # also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    environment_name_ssc=$(to_ssc "$2")
    environment_name_ssc=$(to_ssc "$environment_name")
    export environment_name_ssc

    # variables expansion in $environment_url
@@ -410,11 +423,11 @@ stages:
  }

  # environment cleanup function
  function delete() {
    export environment_type=$1
    export environment_name=$2
  function {{ cookiecutter.template_prefix }}_delete() {
    export environment_type=$ENV_TYPE
    export environment_name=${ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}
    # also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
    environment_name_ssc=$(to_ssc "$2")
    environment_name_ssc=$(to_ssc "$environment_name")
    export environment_name_ssc

    log_info "--- \\e[32mcleanup\\e[0m"
@@ -558,6 +571,10 @@ stages:
{%- elif cookiecutter.template_type == 'deploy' %}
# job prototype
# defines default Docker image, tracking probe, cache policy and tags
# Required vars for login:
# @var ENV_API_URL   : env-specific {{ cookiecutter.template_name }} API url
# @var ENV_API_TOKEN : env-specific {{ cookiecutter.template_name }} API token
# @var ENV_PROJECT   : env-specific project name
.{{ cookiecutter.template_prefix }}-base:
  image: ${{ cookiecutter.template_PREFIX }}_IMAGE
  services:
@@ -566,33 +583,22 @@ stages:
  before_script:
    - *{{ cookiecutter.template_prefix }}-scripts
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - {{ cookiecutter.template_prefix }}_login

# Deploy job prototype
# Can be extended to define a concrete environment
#
# @arg ENV_TYPE      : environment type
# @arg ENV_APP_NAME  : env-specific application name
# @arg ENV_APP_SUFFIX: env-specific application suffix
# @arg ENV_URL       : env-specific application url
# @arg ENV_API_URL   : env-specific {{ cookiecutter.template_name }} API url
# @arg ENV_API_TOKEN : env-specific {{ cookiecutter.template_name }} API token
# @arg ENV_PROJECT   : env-specific project name
# @var ENV_TYPE      : environment type
# @var ENV_APP_NAME  : env-specific application name
# @var ENV_APP_SUFFIX: env-specific application suffix
# @var ENV_URL       : env-specific application url
.{{ cookiecutter.template_prefix }}-deploy:
  extends: .{{ cookiecutter.template_prefix }}-base
  stage: deploy
  variables:
    ENV_APP_SUFFIX: "-$CI_ENVIRONMENT_SLUG"
  before_script:
    - *{{ cookiecutter.template_prefix }}-scripts
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - assert_defined "${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL}" 'Missing required {{ cookiecutter.template_PREFIX }} API url'
    - assert_defined "${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN}" 'Missing required {{ cookiecutter.template_PREFIX }} API token'
    - assert_defined "$ENV_PROJECT" 'Missing required {{ cookiecutter.template_PREFIX }} project'
    - eval_secret "ENV_API_TOKEN"
    - eval_secret "{{ cookiecutter.template_PREFIX }}_API_TOKEN"
    - {{ cookiecutter.cli_tool }} login --url=${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL} --token=${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN} --project=$ENV_PROJECT
  script:
    - deploy "$ENV_TYPE" "${ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}" "${ENV_URL:-${{ cookiecutter.template_PREFIX }}_ENVIRONMENT_URL}"
    - {{ cookiecutter.template_prefix }}_deploy
  artifacts:
    name: "$ENV_TYPE env url for $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    # TODO: propagate deployed env url in a environment_url.txt file
@@ -607,12 +613,9 @@ stages:
# Cleanup job prototype
# Can be extended for each deletable environment
#
# @arg ENV_TYPE      : environment type
# @arg ENV_APP_NAME  : env-specific application name
# @arg ENV_APP_SUFFIX: env-specific application suffix
# @arg ENV_API_URL   : env-specific {{ cookiecutter.template_name }} API url
# @arg ENV_API_TOKEN : env-specific {{ cookiecutter.template_name }} API token
# @arg ENV_PROJECT   : env-specific {{ cookiecutter.template_name }} project name
# @var ENV_TYPE      : environment type
# @var ENV_APP_NAME  : env-specific application name
# @var ENV_APP_SUFFIX: env-specific application suffix
.{{ cookiecutter.template_prefix }}-cleanup:
  extends: .{{ cookiecutter.template_prefix }}-base
  stage: deploy
@@ -620,21 +623,11 @@ stages:
  dependencies: []
  variables:
    ENV_APP_SUFFIX: "-$CI_ENVIRONMENT_SLUG"
  before_script:
    - *{{ cookiecutter.template_prefix }}-scripts
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - assert_defined "${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL}" 'Missing required {{ cookiecutter.template_PREFIX }} API url'
    - assert_defined "${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN}" 'Missing required {{ cookiecutter.template_PREFIX }} API token'
    - assert_defined "$ENV_PROJECT" 'Missing required {{ cookiecutter.template_PREFIX }} project'
    - eval_secret "ENV_API_TOKEN"
    - eval_secret "{{ cookiecutter.template_PREFIX }}_API_TOKEN"
    - {{ cookiecutter.cli_tool }} login --url=${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL} --token=${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN} --project=$ENV_PROJECT
  script:
    - delete "$ENV_TYPE" "${ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}"
    - {{ cookiecutter.template_prefix }}_delete
  environment:
    action: stop


# deploy to review env (only on feature branches)
# disabled by default, enable this job by setting ${{ cookiecutter.template_PREFIX }}_REVIEW_PROJECT.
{{ cookiecutter.template_prefix }}-review: