Commit 805b9bf7 authored by Pierre SMEYERS's avatar Pierre SMEYERS
Browse files

Merge branch 'feat/dynamic-env-urls' into 'master'

manage static or dynamic environment urls

See merge request to-be-continuous/ansible!5
parents 76cdd5f1 fd82064c
Loading
Loading
Loading
Loading
+36 −4
Original line number Diff line number Diff line
@@ -2,11 +2,17 @@

This project implements a Gitlab CI template for [Ansible](https://www.ansible.com/).

## Overview

This template implements continuous delivery/continuous deployment for projects that deploy their environments with Ansible.
## Overview: managed environments

It provides several features, usable in different modes.
This template implements Ansible-based deployments.

It allows you to manage automatic deployment & cleanup of standard predefined environments.
Each environment can be enabled/disabled by configuration.
If you're not satisfied with predefined environments and/or their associated Git workflow, you may implement you own environments and
workflow, by reusing/extending the base (hidden) jobs. This is advanced usage and will not be covered by this documentation.

The following chapters present the managed predefined environments and their associated Git workflow.

### Review environments

@@ -30,7 +36,7 @@ It is only active for your integration branch (`develop` by default).

### Production environments

Lastly, the template supports 2 environments associated to your production branch (`master` by default):
Lastly, the template supports 2 environments associated to your production branch (`master` or `main` by default):

* a **staging** environment (an iso-prod environment meant for testing and validation purpose),
* the **production** environment.
@@ -216,6 +222,32 @@ Deployment jobs also support _optional_ **hook scripts** from your project, loca
* `pre-ansible-playbook.sh` is executed **before** running `ansible-playbook`, to perform specific environment pre-initialization such as generating a dynamic inventory
* `post-ansible-playbook.sh` is executed **after** running `ansible-playbook`, to perform specific environment post-initialization

#### Static vs. Dynamic environment URLs

The Ansible template supports two ways of defining your environments url:

* a **static way**: when you know your environments url in advance, probably because you're exposing your routes through a DNS you manage,
* a [**dynamic way**](https://docs.gitlab.com/ee/ci/environments/#set-dynamic-environment-urls-after-a-job-finishes): when the url cannot be known before the
  deployment job is executed.

The static way can be implemented simply by setting the overridding the [`environment:url`](https://docs.gitlab.com/ee/ci/yaml/#environmenturl) attribute in each
deployment job.

Example:

```yaml
ansible-staging:
  environment:
    url: "https://myapp-staging.noprod.domain"

ansible-production:
  environment:
    url: "https://myapp.prod.domain"
```

To implement the dynamic way, your post deployment hook script shall simply generate a `environment_url.txt` file, containing only
the dynamically generated url. This will be considered by the template as the newly deployed environment url.

### Ansible lint job

The Ansible template enables [Ansible Lint](https://docs.ansible.com/ansible-lint/) analysis of your Ansible scripts.
+20 −2
Original line number Diff line number Diff line
@@ -285,6 +285,12 @@ stages:
    public_key=$6
    vault_password=$7

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

    # maybe execute pre ansible-playbook script
    prescript="$ANSIBLE_SCRIPTS_DIR/pre-ansible-playbook.sh"
    if [[ -f "$prescript" ]]; then
@@ -376,8 +382,18 @@ stages:
    fi

    # finally persist environment url
    echo "$CI_ENVIRONMENT_URL" > environment_url.txt
    echo -e "environment_type=$ENV_TYPE\\nenvironment_url=$CI_ENVIRONMENT_URL" > ansible.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)"
    elif [[ "$CI_ENVIRONMENT_URL" ]]
    then
      environment_url=$CI_ENVIRONMENT_URL
      echo "$environment_url" > environment_url.txt
      log_info "--- static environment url defined: (\\e[33;1m$environment_url\\e[0m)"
    fi
    echo -e "environment_type=$ENV_TYPE\\nenvironment_url=$environment_url" > ansible.env
  }

  function get_latest_template_version() {
@@ -526,6 +542,8 @@ ansible-lint-prod:
      - "$ANSIBLE_PROJECT_DIR/environment_url.txt"
    reports:
      dotenv: "$ANSIBLE_PROJECT_DIR/ansible.env"
  environment:
    url: "$environment_url" # can be either static or dynamic

# Cleanup job prototype
# Can be extended for each deletable environment