Setting ENV in pipeline

%3CLINGO-SUB%20id%3D%22lingo-sub-2453688%22%20slang%3D%22en-US%22%3ESetting%20ENV%20in%20pipeline%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2453688%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20have%20a%20devops%20setup%20where%20when%20we%20set%20a%20new%20tag%20in%20Azure%20Repos%2C%20it%20sets%20off%20a%20docker%20image%20build%20in%20Azure%20pipelines%20and%20passes%20the%20image%20over%20to%20Docker%20Hub.%20I%20have%20been%20asked%20to%20automate%20the%20final%20part%20which%20then%20automatically%20loads%20the%20latest%20version%20of%20the%20image%20to%20a%20test%20environment%2C%20which%20I%20have%20achieved%20using%20a%20webhook%20and%20setting%20the%20pipeline%20to%20build%20and%20push%202%20versions%20of%20the%20app%2C%20one%20with%20the%20version%20number%20set%20in%20the%20Repo%20tag%20and%20another%20always%20set%20to%20'latest'.%20Then%2C%20when%20the%20latest%20image%20is%20pushed%2C%20dockerhub%20calls%20the%20webhook%20and%20the%20test%20environment%20will%20pull%20down%20the%20latest%20version%20of%20this%20latest%20tag%20and%20load%20it%20to%20a%20container.%20So%20far%20so%20good.%20What%20I%20want%20to%20do%20now%20is%20when%20the%20image%20is%20being%20built%20in%20the%20pipeline%2C%20I%20want%20to%20replace%20the%20ENV%20APP_VERSION%20which%20we%20have%20set%20in%20the%20Dockerfile%20with%20a%20hardcoded%20value%20with%20the%20%24tag%20set%20in%20Repo%2C%20this%20way%20we%20will%20know%20from%20the%20front%20end%20which%20image%20build%26nbsp%3B%20we%20are%20running.%20This%20%24tag%20is%20already%20being%20used%20when%20pushed%20to%20dockerhub%2C%20I%20just%20want%20to%20utilise%20it%20further%20so%20from%20the%20front%20end%20we%20know%20which%20version%20of%20docker%20image%20is%20running%20(as%20certain%20developers%20do%20not%20have%20access%20to%20the%20backend%20to%20see%20the%20docker%20setup).%20Anyone%20have%20any%20ideas%20on%20how%20to%20achieve%20this%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2454500%22%20slang%3D%22en-US%22%3ERe%3A%20Setting%20ENV%20in%20pipeline%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2454500%22%20slang%3D%22en-US%22%3EJust%20an%20update%2C%20I%20actually%20managed%20to%20figure%20this%20out.%20In%20the%20Dockerfile%20I%20added%20the%20following%3A%3CBR%20%2F%3EARG%20REPO_TAG%3CBR%20%2F%3EENV%20APP_VERSION%3D%24REPO_TAG%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20the%20azure-pipeline.yml%20I%20added%3A%3CBR%20%2F%3Earguments%3A%20--build-arg%20REPO_TAG%3D%24(tag)%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20then%20placed%20the%20Repo%20tag%20in%20the%20ARG%20REPO_TAG%20and%20then%20on%20build%20this%20REPO_TAG%20variable%20was%20placed%20into%20the%20ENV%20APP_VERSION%20on%20build%20of%20the%20image.%3C%2FLINGO-BODY%3E
New Contributor

We have a devops setup where when we set a new tag in Azure Repos, it sets off a docker image build in Azure pipelines and passes the image over to Docker Hub. I have been asked to automate the final part which then automatically loads the latest version of the image to a test environment, which I have achieved using a webhook and setting the pipeline to build and push 2 versions of the app, one with the version number set in the Repo tag and another always set to 'latest'. Then, when the latest image is pushed, dockerhub calls the webhook and the test environment will pull down the latest version of this latest tag and load it to a container. So far so good. What I want to do now is when the image is being built in the pipeline, I want to replace the ENV APP_VERSION which we have set in the Dockerfile with a hardcoded value with the $tag set in Repo, this way we will know from the front end which image build  we are running. This $tag is already being used when pushed to dockerhub, I just want to utilise it further so from the front end we know which version of docker image is running (as certain developers do not have access to the backend to see the docker setup). Anyone have any ideas on how to achieve this?

1 Reply
Just an update, I actually managed to figure this out. In the Dockerfile I added the following:
ARG REPO_TAG
ENV APP_VERSION=$REPO_TAG

In the azure-pipeline.yml I added:
arguments: --build-arg REPO_TAG=$(tag)

This then placed the Repo tag in the ARG REPO_TAG and then on build this REPO_TAG variable was placed into the ENV APP_VERSION on build of the image.