diff --git a/.gitlab/README.md.template b/.gitlab/README.md.template index 28a0c49fe32afb2d80f3051c0bb4eb54fdae4be0..f1737298909cc067c8d36837c87e358240ac7f1a 100644 --- a/.gitlab/README.md.template +++ b/.gitlab/README.md.template @@ -280,6 +280,26 @@ plan: - apk add jq ``` +### Source `gitlab-tofu` script to run custom commands later + +The `gitlab-tofu` script can be sourced instead of executed. +This allows you to setup the shell and run your own `tofu` or +`gitlab-tofu` commands. + +You can use the following snipped either in your script, +directly in the shell and also in `script` keywords of your +pipeline job: + +```shell +. $(which gitlab-tofu) +``` + +There is a slight chance when doing this in a more exotic environment +or shell that `gitlab-tofu` is not able to detect that it is sourced +and will try to execute a `tofu` command. +In this case you can set the `GITLAB_TOFU_SOURCE` environment variable +to `true` before sourcing `gitlab-tofu`. + ### Best Practices This section is a collection of *some* best practices. diff --git a/README.md b/README.md index 06023aa86c75427dd7368efba237450c0becbba8..c2e88f42bd106ab79e482b207465a79e9e2bfb3a 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,26 @@ plan: - apk add jq ``` +### Source `gitlab-tofu` script to run custom commands later + +The `gitlab-tofu` script can be sourced instead of executed. +This allows you to setup the shell and run your own `tofu` or +`gitlab-tofu` commands. + +You can use the following snipped either in your script, +directly in the shell and also in `script` keywords of your +pipeline job: + +```shell +. $(which gitlab-tofu) +``` + +There is a slight chance when doing this in a more exotic environment +or shell that `gitlab-tofu` is not able to detect that it is sourced +and will try to execute a `tofu` command. +In this case you can set the `GITLAB_TOFU_SOURCE` environment variable +to `true` before sourcing `gitlab-tofu`. + ### Best Practices This section is a collection of *some* best practices. diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh index 5ffb0a14e3e6d46d37418b870f5d4ee05af55497..af20851d03575ec783b545ec800873cce2bc9ab2 100644 --- a/src/gitlab-tofu.sh +++ b/src/gitlab-tofu.sh @@ -2,7 +2,7 @@ # set some shell options set -o errexit - + if [ "${DEBUG_OUTPUT}" = "true" ]; then set -o xtrace fi @@ -24,7 +24,9 @@ fi # Evaluate if this script is being sourced or executed directly. # See https://stackoverflow.com/a/28776166 sourced=0 -if [ -n "$ZSH_VERSION" ]; then +if [ "$GITLAB_TOFU_SOURCE" = 'true' ]; then + sourced=1 +elif [ -n "$ZSH_VERSION" ]; then case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac elif [ -n "$KSH_VERSION" ]; then # shellcheck disable=SC2296