I am having a small issue, I am upgrading from Drone 0.5 to drone 0.8 and I cannot do this anymore:
environment:
- “BRANCH_ENV=${DRONE_BRANCH|s/develop/dev/|s/master/prod/|s/^release.+/qa/”
Because I get a “bad substitution error”
While checking at the substitution documentation I have seen an example like this:
${param/old/new}
But I am looking for a “multiple substitution solution” I have tried doing something like this and it did not help:
BRANCH_ENV=${DRONE_BRANCH/develop/dev/master/prod/^release.+/qa}
Does any of you have any idea how it could be achieved?
That syntax is not supported in the tag section. The docker tag field is a Go string literal (not a bash string) which means it will not evaluate your bash expression. The same is true of the environment section. Please see http://docs.drone.io/environment/, specifically the last example which explains this further.
Drone does try to emulate bash expressions, but has limited support. You can see supported syntax of emulated expressions here: http://docs.drone.io/substitution/
But I ws wondering if it was possible to do something like
${FOO/old1/new1/old2/new2/old3/new3}
If old1 replace with new1 (if branch == master tag must be prod)
if old2 replace with new2 (if branch == develop tag must be dev)
if old3 replace with new3 (if branch == release/* tag must be qa)
Or something similar with drone support? It needs to be done in the tag section…
I think the first step if to create a substitution string that works from a posix-compliant shell. The substitution commands that you provided returns an error on my shell:
$ export DRONE_BRANCH=master
$ echo ${DRONE_BRANCH|s/develop/dev/|s/master/prod/|s/^release.+/qa/}
-bash: ${DRONE_BRANCH|s/develop/dev/|s/master/prod/|s/^release.+/qa/}: bad substitution
If you have a posix-compliant expression, and it does not work in Drone, we can work on supporting it in our emulation code in a future release.
You can also split these into multiple steps (below). If you are concerned with keeping things DRY you can use YAML anchors.
It’s a pity that there is still almost no documentation on this and still no ternary operator for the parameter values. Duplicating steps because of no ternary operation is not a solution, it is not DRY and looks confusing.
I found this information:
but no ternary operator. We really need something like $(var == something ? one : two)
The system only emulates shell parameter expansion [1]. Support for ternary operators would require us to emulate bash command substitution [2] which requires executing the command in a bash subshell environment and would therefore require emulating the full bash scripting language in our yaml. This is out of scope for what we want to provide and are willing to support.
If you need more advanced scripting capabilities we recommend installing the Starlark extension [3]. It was created for this exact purpose.