Home > other >  Setting Docker environment variables in Azure DevOps using bash task
Setting Docker environment variables in Azure DevOps using bash task

Time:12-08

I am having a problem setting up an environment variable as part of Releasing my container. in Azure DevOps, I have a bash task in which I am trying to set an environment variable (CUSER)

export CUSER=$(CUSER) && \
echo $(MYPASS) | sudo -S docker run \
-e CUSER \
--name $(CNAME) \
-p 80:80 $(INAME):$(Build.BuildId) &

The container runs but the environment variable is not set. But it is set when I execute the script directly on the host like export CUSER="Dev9" && docker run -e CUSER --name demo1 -p 80:80 myimage:256 I suspect there is a problem with the way my command is formatted but I am not sure where or what.

CodePudding user response:

You used a subshell evaluation command in your script: $(CUSER) tries to execute a command CUSER and evaluates to the output, however CUSER is probably not a valid command in your environment.

You should use curly braces instead to just get the value of the said variable.

Try the replacing the parentheses with {} like so: ${CUSER}. Do it also for MYPASS etc.

export CUSER=${CUSER} && \
echo ${MYPASS} | sudo -S docker run \
-e CUSER \
--name ${CNAME} \
-p 80:80 ${INAME}:${Build.BuildId} &

I don't know what Build.BuildId is supposed to be. Bash variables cannot have a . in their name. You need to figure that out.

CodePudding user response:

Resolved it by changing how I authenticate sudo.

sudo -S <<< $(MYPASS) echo export CUSER=$(CUSER) && \
docker run -e CUSER \
--name $(CNAME) \
-p 80:80 $(INAME):$(Build.BuildId) &
  • Related