Home > OS >  Why would `sed` behave differenty in Gitlab CI?
Why would `sed` behave differenty in Gitlab CI?

Time:12-11

sed -i "s|{{PLACEHOLDER}}|${KEY_B64}|g" <path>

The command above is executed in a Gitlab CI runner and throws the following:

sed: unmatched '|'

I have double-checked the KEY_B64 environment variable, it is set and looks valid. This variable is a base-64 encoded JWT token (Kubernetes secrets expect to be base-64 encoded.

What is really strange though is that this command works fine if I run it locally (Ubuntu 22.04) and replace the env variable with the output from echo -n <JWT_TOKEN> | base64.

Based on the error message, it seems that the env value might contain the delimiter, but changing it to anything else doesn't solve the problem. On top of that, the encoded value for sure doesn't include such symbols.

What could be the cause of the issue?

Updates:

Running sed --version outputs:

$ sed --version
This is not GNU sed version 4.0

Looking with the set -x option on, I can see that the encoded string includes newlines (outputted the variable in the pipeline logs).

  • Using printf %s $VAR did not solve the issue
  • Surprisingly, base64 doesn't support -w0

CodePudding user response:

Using set -x revealed that for some reason the base64 command added line breaks to the output.

As base64 -w0 is not supported, I had to use the command below to remove the newlines in the base64 output, which solved the problem.

export MY_KEY_B64=$(echo -n $MY_KEY | base64 | tr -d \\n)

Note: as I was fairly told in the comments, using printf is preferable but in this case, it was not the cause of the issue so I did not modify this command to emphasize what cause the problem.

  • Related