I set up a dummy example and want to concatenate env variables that will be taken from secrets and encoded to base64 and used further.
I have two secrets MONGODB_USERNAME
, MONGODB_PASSWORD
;
Values of secrets are:
{
"MONGODB_USERNAME": "MONGODB_USERNAME_SECRET_VALUE",
"MONGODB_PASSWORD": "MONGODB_PASSWORD_SECRET_VALUE"
}
content of workflow file:
name: Deployment base64-example
on:
push:
branches:
- main
- dev
env:
CACHE_KEY: node-deps
MONGODB_DB_NAME: gha-demo
jobs:
test:
environment: testing
runs-on: ubuntu-latest
env:
MONGODB_USERNAME: ${{ secrets.MONGODB_USERNAME }}
MONGODB_PASSWORD: ${{ secrets.MONGODB_PASSWORD }}
PORT: 8080
steps:
- name: Output information
run: |
echo "MONGODB_USERNAME: $MONGODB_USERNAME"
- name: encode to base64 and save to file and show file
run: |
export "AUTH_TOKEN=$(echo -n "${MONGODB_USERNAME}:${MONGODB_PASSWORD}" | base64)"
echo $AUTH_TOKEN >> temp
cat temp
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Output information
env:
PORT: 3000
run: |
echo "MONGODB_DB_NAME: $MONGODB_DB_NAME"
echo "MONGODB_USERNAME: $MONGODB_USERNAME"
echo "${{ env.PORT }}"
I have code that concatenates variables MONGODB_USERNAME
and MONGODB_PASSWORD
encoded them and puts to a temp
file.
run: |
export "AUTH_TOKEN=$(echo -n "${MONGODB_USERNAME}:${MONGODB_PASSWORD}" | base64)"
echo $AUTH_TOKEN >> temp
cat temp
decode works correct
echo "TU9OR09EQl9VU0VSTkFNRV9TRUNSRVRfVkFMVUU6TU9OR09EQl9QQVNTV09SRF9TRUNSRVRfVkFM VUU=" | base64 --decode
result
MONGODB_USERNAME_SECRET_VALUE:MONGODB_PASSWORD_SECRET_VALUE%
I don't know why but it adding a space in between when I am doing cat temp
Why does it add this space and how to fix it? in the real scenario for example, with jfrog artifactory it can not download private packages because of this space.
This is because of the line wrapping behavior of base64
. Use the argument --wrap 0
to base64
to disable line wrapping. See: man base64.
Notice the space is included here when using base64
without arguments:
alongstring="The quick brown fox jumps over the lazy dog on Stackoverflow"
result=$(echo $alongstring | base64)
echo $result
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZyBvbiBTdGFja292ZXJm bG93Cg==
But if you use base64 --wrap 0
this doesn't happen:
result=$(echo $alongstring | base64 --wrap 0)
echo $result
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZyBvbiBTdGFja292ZXJmbG93Cg==