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

feat: support dynamic env url

parent 2850276b
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ The OpenShift template uses some global configuration used throughout all jobs.
| `OS_URL`                 | Default OpenShift API url              | **has to be defined** |
| :lock: `OS_TOKEN`        | Default OpenShift API [token](#token-generation) | **has to be defined** |
| `OS_BASE_APP_NAME`       | Base application name                  | `$CI_PROJECT_NAME` ([see GitLab doc](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)) |
| `OS_ENVIRONMENT_URL`    | Default environments url _(only define for static environment URLs declaration)_<br/>_supports late variable expansion (ex: `https://%{environment_name}.openshift.acme.com`)_ | _none_ |
| `OS_SCRIPTS_DIR`         | directory where OpenShift scripts (templates, hook scripts) are located | `.` _(root project dir)_ |
| `OS_BASE_TEMPLATE_NAME`  | Base OpenShift template name           | `openshift` |
| `OS_APP_LABEL`           | The OpenShift [label](https://docs.openshift.com/container-platform/3.11/dev_guide/templates.html#writing-labels) set with the `$environment_name` [dynamic variable](#environment-variables) value. _Advanced usage_ | `app` |
@@ -133,8 +134,7 @@ Here are variables supported to configure review environments:
| `OS_REVIEW_URL`          | OpenShift API url for `review` env  _(only define if different from default)_    | `$OS_URL` |
| :lock: `OS_REVIEW_TOKEN` | OpenShift API [token](#token-generation) for `review` env  _(only define if different from default)_    | `$OS_TOKEN` |
| `OS_REVIEW_APP_NAME`     | Application name for `review` env      | `"${OS_BASE_APP_NAME}-${CI_ENVIRONMENT_SLUG}"` (ex: `myproject-review-fix-bug-12`) |
| `OS_REVIEW_ENVIRONMENT_SCHEME` | The review environment protocol scheme | `https` |
| `OS_REVIEW_ENVIRONMENT_DOMAIN` | The review environment domain. | _none_ |
| `OS_REVIEW_ENVIRONMENT_URL`| The review environments url _(only define for static environment URLs declaration and if different from default)_ | `$OS_ENVIRONMENT_URL` |

Note: By default, review `environment.url` will be built as `${OS_REVIEW_ENVIRONMENT_SCHEME}://${$CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${OS_REVIEW_ENVIRONMENT_DOMAIN}`

@@ -152,7 +152,7 @@ Here are variables supported to configure the integration environment:
| `OS_INTEG_URL`           | OpenShift API url for `integration` env  _(only define if different from default)_    | `$OS_URL` |
| :lock: `OS_INTEG_TOKEN`  | OpenShift API [token](#token-generation) for `integration` env  _(only define if different from default)_    | `$OS_TOKEN` |
| `OS_INTEG_APP_NAME`      | Application name for `integration` env | `${OS_BASE_APP_NAME}-integration` |
| `OS_INTEG_ENVIRONMENT_URL` | The integration environment url **including scheme** (ex: `https://my-application-integration.nonpublic.domain.com`). Do not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that. | _none_ |
| `OS_INTEG_ENVIRONMENT_URL`| The integration environment url _(only define for static environment URLs declaration and if different from default)_ | `$OS_ENVIRONMENT_URL` |

#### Staging environment

@@ -168,7 +168,7 @@ Here are variables supported to configure the staging environment:
| `OS_STAGING_URL`         | OpenShift API url for `staging` env  _(only define if different from default)_   | `$OS_URL` |
| :lock: `OS_STAGING_TOKEN`| OpenShift API [token](#token-generation) for `staging` env  _(only define if different from default)_    | `$OS_TOKEN` |
| `OS_STAGING_APP_NAME`    | Application name for `staging` env     | `${OS_BASE_APP_NAME}-staging` |
| `OS_STAGING_ENVIRONMENT_URL` | The staging environment url **including scheme** (ex: `https://my-application-staging.nonpublic.domain.com`). Do not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that. | _none_ |
| `OS_STAGING_ENVIRONMENT_URL`| The staging environment url _(only define for static environment URLs declaration and if different from default)_ | `$OS_ENVIRONMENT_URL` |

#### Production environment

@@ -184,7 +184,7 @@ Here are variables supported to configure the production environment:
| `OS_PROD_URL`            | OpenShift API url for `production` env  _(only define if different from default)_| `$OS_URL` |
| :lock: `OS_PROD_TOKEN`   | OpenShift API [token](#token-generation) for `production` env  _(only define if different from default)_    | `$OS_TOKEN` |
| `OS_PROD_APP_NAME`       | Application name for `production` env  | `$OS_BASE_APP_NAME` |
| `OS_PROD_ENVIRONMENT_URL`| The production environment url **including scheme** (ex: `https://my-application.public.domain.com`) Do not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that. | _none_ |
| `OS_PROD_ENVIRONMENT_URL`| The production environment url _(only define for static environment URLs declaration and if different from default)_ | `$OS_ENVIRONMENT_URL` |
| `AUTODEPLOY_TO_PROD`     | Set this variable to auto-deploy to production. If not set deployment to production will be `manual` (default behaviour). | _none_ (disabled) |

### Deployment jobs
+15 −15
Original line number Diff line number Diff line
@@ -27,6 +27,11 @@
      "default": "$CI_PROJECT_NAME",
      "advanced": true
    },
    {
      "name": "OS_ENVIRONMENT_URL",
      "type": "url",
      "description": "The default environments url _(only define for static environment URLs declaration)_\n\n_supports late variable expansion (ex: `https://%{environment_name}.openshift.acme.com`)_"
    },
    {
      "name": "OS_SCRIPTS_DIR",
      "description": "directory where OpenShift scripts (templates, hook scripts) are located",
@@ -63,15 +68,10 @@
          "advanced": true
        },
        {
          "name": "OS_REVIEW_ENVIRONMENT_SCHEME",
          "description": "The review environment protocol scheme",
          "default": "https",
          "mandatory": true
        },
        {
          "name": "OS_REVIEW_ENVIRONMENT_DOMAIN",
          "description": "The review environment domain (ex: `noprod-openshift.domain.com`).\n\nBy default review `environment.url` will be built as `${OS_REVIEW_ENVIRONMENT_SCHEME}://${$CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${OS_REVIEW_ENVIRONMENT_DOMAIN}`",
          "mandatory": true
          "name": "OS_REVIEW_ENVIRONMENT_URL",
          "type": "url",
          "description": "The review environments url _(only define for static environment URLs declaration and if different from default)_",
          "advanced": true
        },
        {
          "name": "OS_REVIEW_URL",
@@ -109,8 +109,8 @@
        {
          "name": "OS_INTEG_ENVIRONMENT_URL",
          "type": "url",
          "description": "The integration environment url including scheme (ex: `https://my-application-integration.noprod-openshift.domain.com`).\n\nDo not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that.",
          "mandatory": true
          "description": "The integration environment url _(only define for static environment URLs declaration and if different from default)_",
          "advanced": true
        },
        {
          "name": "OS_INTEG_URL",
@@ -143,8 +143,8 @@
        {
          "name": "OS_STAGING_ENVIRONMENT_URL",
          "type": "url",
          "description": "The staging environment url including scheme (ex: `https://my-application-staging.noprod-openshift.domain.com`).\n\nDo not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that.",
          "mandatory": true
          "description": "The staging environment url _(only define for static environment URLs declaration and if different from default)_",
          "advanced": true
        },
        {
          "name": "OS_STAGING_URL",
@@ -177,8 +177,8 @@
        {
          "name": "OS_PROD_ENVIRONMENT_URL",
          "type": "url",
          "description": "The production environment url including scheme (ex: `https://my-application.openshift.domain.com`).\n\nDo not use variable inside variable definition as it will result in a two level cascade variable and gitlab does not allow that.",
          "mandatory": true
          "description": "The production environment url _(only define for static environment URLs declaration and if different from default)_",
          "advanced": true
        },
        {
          "name": "AUTODEPLOY_TO_PROD",
+35 −20
Original line number Diff line number Diff line
@@ -352,6 +352,7 @@ stages:
    export project
    export environment_type=$1
    export environment_name=$2
    environment_url=$3
    # also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with OpenShift env variables)
    environment_name_ssc=$(to_ssc "$2")
    export environment_name_ssc
@@ -362,8 +363,11 @@ stages:
    export appname=$environment_name
    export appname_scc=$environment_name_ssc

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

    log_info "--- \\e[32mdeploy\\e[0m"
@@ -405,8 +409,15 @@ stages:
    check_readiness

    # finally persist environment url
    echo "$CI_ENVIRONMENT_URL" > environment_url.txt
    echo -e "environment_type=$environment_type\\nenvironment_name=$environment_name\\nenvironment_url=$CI_ENVIRONMENT_URL" > openshift.env
    if [[ -f environment_url.txt ]]
    then
      environment_url=$(cat environment_url.txt)
      export environment_url
      log_info "--- dynamic environment url found: (\\e[33;1m$environment_url\\e[0m)"
    else
      echo "$environment_url" > environment_url.txt
    fi
    echo -e "environment_type=$environment_type\\nenvironment_name=$environment_name\\nenvironment_url=$environment_url" > openshift.env
  }

  # $1 deployment name
@@ -597,7 +608,8 @@ stages:
# @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 OpenShift API url
# @arg ENV_URL       : env-specific deployment url (static)
# @arg ENV_API_URL   : env-specific OpenShift API url
# @arg ENV_TOKEN     : env-specific OpenShift API token
# @arg ENV_PROJECT   : env-specific OpenShift project name
.os-deploy:
@@ -608,18 +620,20 @@ stages:
  before_script:
    - *os-scripts
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - assert_defined "${ENV_URL:-$OS_URL}" 'Missing required OpenShift url'
    - assert_defined "${ENV_API_URL:-$OS_URL}" 'Missing required OpenShift url'
    - assert_defined "${ENV_TOKEN:-$OS_TOKEN}" 'Missing required OpenShift token'
    - assert_defined "$ENV_PROJECT" 'Missing required OpenShift project'
    - oc login ${ENV_URL:-$OS_URL} --token=${ENV_TOKEN:-$OS_TOKEN} -n $ENV_PROJECT
    - oc login ${ENV_API_URL:-$OS_URL} --token=${ENV_TOKEN:-$OS_TOKEN} -n $ENV_PROJECT
  script:
    - deploy "$ENV_TYPE" "${ENV_APP_NAME:-${OS_BASE_APP_NAME}${ENV_APP_SUFFIX}}"
    - deploy "$ENV_TYPE" "${ENV_APP_NAME:-${OS_BASE_APP_NAME}${ENV_APP_SUFFIX}}" "${ENV_URL:-${OS_ENVIRONMENT_URL:-$ENV_URL_LEGACY}}"
  artifacts:
    name: "$ENV_TYPE env url for $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    paths:
      - environment_url.txt
    reports:
      dotenv: openshift.env
  environment:
    url: "$environment_url" # can be either static or dynamic

# Cleanup job prototype
# Can be extended for each deletable environment
@@ -627,7 +641,7 @@ stages:
# @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 OpenShift API url
# @arg ENV_API_URL   : env-specific OpenShift API url
# @arg ENV_TOKEN     : env-specific OpenShift API token
# @arg ENV_PROJECT   : env-specific OpenShift project name
.os-cleanup:
@@ -640,10 +654,10 @@ stages:
  before_script:
    - *os-scripts
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - assert_defined "${ENV_URL:-$OS_URL}" 'Missing required OpenShift url'
    - assert_defined "${ENV_API_URL:-$OS_URL}" 'Missing required OpenShift url'
    - assert_defined "${ENV_TOKEN:-$OS_TOKEN}" 'Missing required OpenShift token'
    - assert_defined "$ENV_PROJECT" 'Missing required OpenShift project'
    - oc login ${ENV_URL:-$OS_URL} --token=${ENV_TOKEN:-$OS_TOKEN} -n $ENV_PROJECT
    - oc login ${ENV_API_URL:-$OS_URL} --token=${ENV_TOKEN:-$OS_TOKEN} -n $ENV_PROJECT
  script:
    - delete "$ENV_TYPE" "${ENV_APP_NAME:-${OS_BASE_APP_NAME}${ENV_APP_SUFFIX}}"
  environment:
@@ -658,12 +672,13 @@ os-review:
  variables:
    ENV_TYPE: review
    ENV_APP_NAME: "$OS_REVIEW_APP_NAME"
    ENV_URL: "$OS_REVIEW_URL"
    ENV_URL: "${OS_REVIEW_ENVIRONMENT_URL}"
    ENV_URL_LEGACY: "${OS_REVIEW_ENVIRONMENT_SCHEME}://${CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${OS_REVIEW_ENVIRONMENT_DOMAIN}"
    ENV_API_URL: "$OS_REVIEW_URL"
    ENV_TOKEN: "$OS_REVIEW_TOKEN"
    ENV_PROJECT: "$OS_REVIEW_PROJECT"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: "${OS_REVIEW_ENVIRONMENT_SCHEME}://${CI_PROJECT_NAME}-${CI_ENVIRONMENT_SLUG}.${OS_REVIEW_ENVIRONMENT_DOMAIN}"
    on_stop: os-cleanup-review
  resource_group: review/$CI_COMMIT_REF_NAME
  rules:
@@ -682,7 +697,7 @@ os-cleanup-review:
  variables:
    ENV_TYPE: review
    ENV_APP_NAME: "$OS_REVIEW_APP_NAME"
    ENV_URL: "$OS_REVIEW_URL"
    ENV_API_URL: "$OS_REVIEW_URL"
    ENV_TOKEN: "$OS_REVIEW_TOKEN"
    ENV_PROJECT: "$OS_REVIEW_PROJECT"
  environment:
@@ -735,12 +750,12 @@ os-integration:
  variables:
    ENV_TYPE: integration
    ENV_APP_NAME: "$OS_INTEG_APP_NAME"
    ENV_URL: "$OS_INTEG_URL"
    ENV_URL: "${OS_INTEG_ENVIRONMENT_URL}"
    ENV_API_URL: "$OS_INTEG_URL"
    ENV_TOKEN: "$OS_INTEG_TOKEN"
    ENV_PROJECT: "$OS_INTEG_PROJECT"
  environment:
    name: integration
    url: "${OS_INTEG_ENVIRONMENT_URL}"
  resource_group: integration
  rules:
    # only on integration branch(es), with $OS_INTEG_PROJECT set
@@ -757,12 +772,12 @@ os-staging:
  variables:
    ENV_TYPE: staging
    ENV_APP_NAME: "$OS_STAGING_APP_NAME"
    ENV_URL: "$OS_STAGING_URL"
    ENV_URL: "${OS_STAGING_ENVIRONMENT_URL}"
    ENV_API_URL: "$OS_STAGING_URL"
    ENV_TOKEN: "$OS_STAGING_TOKEN"
    ENV_PROJECT: "$OS_STAGING_PROJECT"
  environment:
    name: staging
    url: "${OS_STAGING_ENVIRONMENT_URL}"
  resource_group: staging
  rules:
    # only on production branch(es), with $OS_STAGING_PROJECT set
@@ -776,12 +791,12 @@ os-production:
    ENV_TYPE: production
    ENV_APP_SUFFIX: "" # no suffix for prod
    ENV_APP_NAME: "$OS_PROD_APP_NAME"
    ENV_URL: "$OS_PROD_URL"
    ENV_URL: "${OS_PROD_ENVIRONMENT_URL}"
    ENV_API_URL: "$OS_PROD_URL"
    ENV_TOKEN: "$OS_PROD_TOKEN"
    ENV_PROJECT: "$OS_PROD_PROJECT"
  environment:
    name: production
    url: "${OS_PROD_ENVIRONMENT_URL}"
  resource_group: production
  rules:
    # exclude non-production branches