Pass file content as build argument in azure devops pipeline docker build task

房东的猫 提交于 2020-12-15 06:25:09

问题


I want to pass my file content as build argument in docker build command using --build-arg. I am using following command in terminal and it is working fine

docker build -t example --build-arg PRIVATE_KEY="$(cat /home/key)" .

but when I am trying same thing in azure devops pipeline docker build task and passing --build-arg PRIVATE_KEY="$(cat /home/key)" in arguments box its taking the value as it is i.e, $(cat /home/key) and not executing the cat command.


回答1:


I do not think it is the docker build task's problem. The reason it worked for command line task is probably because when you run the docker build command on your local machine or in the command line task, the command line first will evaluate what is wrapped in "$()",and then the value is passed to the docker ARG. But for docker build task, it directly invokes docker.exe, so that the expression in "$()" cannot be evaluated.

Since you have found the workaround to use the command line task. I suggest you use command line task instead of docker task.

I also tried what @4c74356b41 suggested. I defined a pipeline variable key and put the key's value into this variable and reference this variable in docker build task.

And i worked for me with below when i put --build-arg PRIVATE_KEY="$(key)" in the Arguments box.

  • Variable

  • Docker build task




回答2:


You need to either put this value into a variable and reference a variable or use a script step to create a variable out of the contents of the file and reference the variable.

an alternative would be to pass the key to the docker build as a context and inside the build extract that value



来源:https://stackoverflow.com/questions/60463392/pass-file-content-as-build-argument-in-azure-devops-pipeline-docker-build-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!