@@ -283,6 +283,45 @@ The **static way** can be implemented simply by setting the appropriate configur
To implement the **dynamic way**, your post deployment hook script shall simply generate a `environment_url.txt` file in the working directory, containing only
the dynamically generated url. When detected by the template, it will use it as the newly deployed environment url.
### Multiple environments support
The Docker Compose template allows deploying multiple environments in parallel. Use cases of this include:
- monorepo, where a single Git repository might host several separate deployable components or apps,
- multi-instances deployment of the same application.
This feature can be enabled using the [parallel matrix jobs](https://docs.gitlab.com/ee/ci/yaml/#parallelmatrix)
pattern at the `.aws-env-base` job level (this is the top parent job of all deployment jobs).
Environments namespacing is ensured by the `DCMP_ENVIRONMENT_NAMESPACE` variable (must start with a slash `/`).
Here is the example of the `.gitlab-ci.yml` file for a project deploying both a frontend and a backend applications:
```yaml
.compose-env-base:
parallel:
matrix:
-DCMP_ENVIRONMENT_NAMESPACE:"/front"
# Compose deployment scripts are located in the ./front/ directory
DCMP_SCRIPTS_DIR:"front"
DCMP_BASE_APP_NAME:"wonderfront"
-DCMP_ENVIRONMENT_NAMESPACE:"/back"
# Compose deployment scripts are located in the ./back/ directory
DCMP_SCRIPTS_DIR:"back"
DCMP_BASE_APP_NAME:"wonderback"
# ⚠ on_stop must be unset when defining parallel:matrix environments
The above configuration will deploy 2 environments on each pipeline:
- on feature branches: `review/front/$CI_COMMIT_REF_NAME` and `review/back/$CI_COMMIT_REF_NAME`
- on the integration branch: `integration/front` and `integration/back`
- on the production branch: `staging/front` and `staging/back` (and finally `production/front` and `production/back`)
### Deployment output variables
Each deployment job produces _output variables_ that are propagated to downstream jobs (using [dotenv artifacts](https://docs.gitlab.com/ci/yaml/artifacts_reports/#artifactsreportsdotenv)):
@@ -294,6 +333,15 @@ Each deployment job produces _output variables_ that are propagated to downstrea
Those variables may be freely used in downstream jobs (for instance to run acceptance tests against the latest deployed environment).
You may also add and propagate your own custom variables, by pushing them to the `docker-compose.out.env` file in your [deployment scripts or hooks](#deployment-and-cleanup).
> [!important]
> If [multiple environments](#multiple-environments-support) are configured, the output variables are prefixed with a
> sluggified value of the `DCMP_ENVIRONMENT_NAMESPACE` variable (stripped of punctuation characters and converted to lowercase):
>
> * `<namespace_slug>_environment_type`: set to the type of environment (`review`, `integration`, `staging` or `production`),
> * `<namespace_slug>_environment_name`: the application name (see below),
> * `<namespace_slug>_environment_url`: set to the environment URL (whether determined statically or dynamically).
>
> The output dotenv file will be `docker-compose.out.env.<namespace_slug>` instead, and the dynamic variable `${environment_namespace}` can be used in your scripts and manifests to access the contextual value of `<namespace_slug>`.
## Configuration reference
@@ -321,6 +369,7 @@ The Docker Compose template uses some global configuration used throughout all j
| `cmd` / `DCMP_CMD` | The docker compose or stack command (`docker compose`, `docker-compose` or `docker stack`) | _none_ (auto) |
| `base-app-name` / `DCMP_BASE_APP_NAME`| Base application name | `$CI_PROJECT_NAME` ([see GitLab doc](https://docs.gitlab.com/ci/variables/predefined_variables/)) |
| `environment-namespace` / `DCMP_ENVIRONMENT_NAMESPACE` | Extra [GitLab environments](https://docs.gitlab.com/ci/environments/) namespace _(only required when deploying [multiple environments](#multiple-environments-support))_<br/>:warning: must start with a slash `/` | _none_ |
| `scripts-dir` / `DCMP_SCRIPTS_DIR`| Directory where Compose files, dotenv files and hook scripts are located | `.` _(root project dir)_ |
| `up-opts` / `DCMP_UP_OPTS` | [`compose up` options](https://docs.docker.com/reference/cli/docker/compose/up/#options) (only when using Docker Compose) | `--no-build --remove-orphans --wait --wait-timeout 180` |
| `down-opts`/ `DCMP_DOWN_OPTS` | [`compose down` options](https://docs.docker.com/reference/cli/docker/compose/down/#options) (only when using Docker Compose) | `--volumes --remove-orphans --rmi all` |
"description":"The default environments url _(only define for static environment URLs declaration)_\n\n_supports late variable expansion (ex: `https://%{environment_name}.dcmp.acme.com`)_"
},
{
"name":"DCMP_ENVIRONMENT_NAMESPACE",
"description":"Extra [GitLab environments](https://docs.gitlab.com/ci/environments/) namespace _(only required when deploying multiple environments)_\n\n:warning: must start with a slash `/`",
"advanced":true
},
{
"name":"DCMP_SCRIPTS_DIR",
"description":"Directory where Compose files, dotenv files and hook scripts are located",