In this article, we’re going to show you how to use GitLab CI/CD to automate the shutdown and restart of Clever Cloud environments during the weekend, or at any other time of your choosing to avoid unnecessary consuming resources and save money.
Prerequisites
- A Clever Cloud account with environments to manage
- A project hosted on GitLab
- Clever Cloud API tokens (CLEVER_SECRET and CLEVER_TOKEN) (read the doc)
Step 1: Configuring GitLab CI/CD
We’ll start by setting up GitLab CI/CD with a .gitlab-ci.yml file that describes the tasks to perform. The .gitlab-ci.yml file outlines the structure and order of GitLab CI/CD pipelines.
Here is an example of a .gitlab-ci.yml configuration:
stages:
// other stages
- switch_env
// main jobs
include:
- '.gitlab/.clever-cloud-environment-switcher.yml'
Here, we have a stage named switch_env
which is used to manage the shutdown and restart of our environments. We also include another configuration file called .clever-cloud-environment-switcher.yml
located in our project’s .gitlab directory.
Step 2: Configuring the Environment Switcher
In the .clever-cloud-environment-switcher.yml
file, we define two jobs, switch_env_off
and switch_env_on
, to stop and restart our environments, respectively.
stages:
- switch_env
switch_env_off:
stage: switch_env
image:
name: clevercloud/clever-tools:latest
entrypoint: ['/bin/sh', '-c']
script:
- clever login --secret $CLEVER_SECRET --token $CLEVER_TOKEN
- clever stop -a $ENVIRONMENT_NAME
only:
variables:
- $PIPELINE_SCHEDULE_NAME == "switch_env_off"
switch_env_on:
stage: switch_env
image:
name: clevercloud/clever-tools:latest
entrypoint: ['/bin/sh', '-c']
script:
- clever login --secret $CLEVER_SECRET --token $CLEVER_TOKEN
- clever restart -a $ENVIRONMENT_NAME
only:
variables:
- $PIPELINE_SCHEDULE_NAME == "switch_env_on"
Both jobs use the Docker image clevercloud/clever-tools
which includes the Clever CLI tool. We use this tool to log into Clever Cloud with our tokens (clever login
), and to stop (clever stop
) or restart (clever restart
) our environments.
Note that each job is only executed when the
PIPELINE_SCHEDULE_NAME
environment variable matches the specified value (eitherswitch_env_off
for stopping the environment orswitch_env_on
for restarting it).
Step 3: Configuring the Scheduled Pipelines
Now we’ll set up the scheduled pipelines in GitLab to stop and restart our environments. I won’t explain here the basics of Scheduled pipelines, instead read the awesome related doc here if you don’t know about it. Anyway, for each environment we want to manage, we’ll create two scheduled pipelines, one for switch_env_off
and another for switch_env_on
.
When creating each scheduled pipeline, we define two environment variables:
PIPELINE_SCHEDULE_NAME: switch_env_off
ENVIRONMENT_NAME: preprod
or
PIPELINE_SCHEDULE_NAME: switch_env_on
ENVIRONMENT_NAME: staging
Conclusion
With this setup, GitLab CI/CD will automatically handle the shutdown and restart of your Clever Cloud environments according to the schedule you set in the scheduled pipelines. This is an efficient and flexible solution for managing your resources and reducing hosting costs.
Remember to secure your Clever Cloud API tokens by storing them as secret variables in GitLab rather than in plain text in your configuration files.