Get / Generate random string for a branch

I want to generate a random string, which should be unique to the branch. I want to use that random string as a name for a kubernetes namespace, so each branch will deploy on different namespace. If any code change happen in that branch, deployment will upgrade on that respective namespace

I cant use CF_SHORT_REVISION, because it is not unique to a branch (only to a commit)

You can simply use CF_BRANCH on its own. No need for a random string.

What you want is already described in this tutorial

Yes, it would sesolve the issue. But in my case branch names are not managed. Some of them are very long and can’t use as a kubernetes namespace name

You can also use ${{CF_BRANCH_TAG_NORMALIZED_LOWER_CASE}} that makes a better name

Alternatively just write a script that takes the branch name in a freestyle step and creates something random.

example pseudocode

create-namespace:
  title: create namespace name
  image: alpine
  commands:
    - NAMESPACE_NAME=$(base64 $CF_BRANCH)
    - cf_export NAMESPACE_NAME

And then your problem has nothing to do with Codefresh. Just use normal bash/unix commands How to Generate Random String in Bash

1 Like