Multiple substitution


#1

Greetings Friends,

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?


#2

you need to use $${DRONE_BRANCH. See this section of the docs for more details http://docs.drone.io/environment/


#3

Hello Brad,

Thanks for the swift reply! My other problem is if I want to do the same thing for a “tag section” in for a docker section

Here is my old (works with 0.5)

docker:
    repo: "gcr.io/ypcloud-io/${DRONE_REPO_LC}"
    dockerfile: Dockerfile
    environment:
      - "BRANCH_ENV=${DRONE_BRANCH|s/develop/dev/|s/master/prod/|s/^release.+/qa/}"
    tag:
      - "${DRONE_BUILD_NUMBER}"
      - "${DRONE_BRANCH|s/develop/dev/|s/master/prod/|s/^release.+/qa/}"
    when:
      branch: [master, develop, release/*]

I am looking for a similar solution. $${DRONE_BRANCH should work in the environment section but it will not work in the TAG section. I only want 3 specific tags + number

Is it not possible to work with the ${param/old/new} like mentioned in the documentation?


#4

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/


#5

I have read the documentation about variable substitution… which is the source of my question.

By reading the documentation found here http://docs.drone.io/substitution/ they show an example of a single substitution variable.

${FOO/old/new}

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…

Thanks a lot for your reply once again!


#6

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.

docker_master:
    repo: octocat/hello-world
    tags: master
    when:
      branch: master

docker_develop:
    repo: octocat/hello-world
    tags: dev
    when:
      branch: develop

docker_release:
    repo: octocat/hello-world
    tags: ${DRONE_BRANCH:8}
    when:
      branch: release/*

#7

Hahah, yeah I was trying to avoid something like that :slight_smile:

Thanks a lot for your help!


#8

How to combine toLower with remove substring ?
example
my DRONE_COMMIT_BRANCH=feature/XP-9999
I need create docker tag: “xp-9999”

so I need to combine this two substitutions:
${DRONE_COMMIT_BRANCH##feature/}

${DRONE_COMMIT_BRANCH,,}