Using Push Options in Gitlab CI/CD

Recently, due to adjustments in our company’s container cloud environment, I needed to adapt previously stable CI/CD configuration files to the new runtime environment. After modifying the CI/CD configuration file, I confidently executed git push
. Of course, it didn’t pass, otherwise this article wouldn’t exist…
Since everything had been running stably before, I hadn’t enabled verbose
mode during deployment. As a result, I couldn’t determine the specific reason for the CI/CD execution failure. I didn’t want to keep verbose
mode enabled all the time, so I looked through Gitlab’s documentation to see if I could control whether to enable verbose
mode through environment variables.
This search indeed led me to a viable solution, along with other discoveries.
Push option⌗
This feature is provided by Git, allowing you to attach user-defined Push options when executing the git push
command. In Gitlab’s documentation, I discovered that this feature can be used to skip CI/CD. Why would you want to do this? For instance, when we’re frequently debugging, we might not want every Push to trigger CI/CD. In that case, we can use the following command to skip the CI/CD pipeline:
git push -o ci.skip
ci.skip is a predefined option in Gitlab. For more options, see Gitlab’s official documentation.
Using custom variables⌗
Since Gitlab can receive Git Push option values, I had a bold idea: could we control whether to enable verbose
mode through custom environment variables?
So I added the following configuration to the project’s .gitlab-ci.yml
file:
stages:
- deploy
variables:
VERBOSE: "" # The default value here is an empty string
deploy:preview:
image: betterde/deployer:6.8.0
only:
refs:
- develop
stage: deploy
tags:
- DevOps
script:
- deployer deploy preview $VERBOSE
VERBOSE
is what we use to receive the -vvv
parameter. Note that in the configuration file, we set this variable to an empty string value, and in the script
block, we directly use $VERBOSE
to get the environment variable value (don’t wrap it in double quotes, otherwise it will be recognized as a parameter even if it’s empty).
We only need to execute the following command to control whether to enable verbose
mode:
# Default: not enabled
git push
# When debugging requires enabling it
git push -o ci.variable="VERBOSE=-vvv"
# If you need to pass multiple parameters:
git push -o ci.variable="ONE=1" -o ci.variable="TWO=2"
At this point, we’ve achieved more fine-grained control over CI/CD, so let those freed-up hands do more meaningful things!
I hope this is helpful, Happy hacking…