Pass Sitecore CLI version dynamically from env file to dockerfile

In my last blog, I forced/hard-coded the Sitecore CLI version in the build\init dockerfile, I actually wanted to pass this value dynamically from .env file! Isn't that exciting enough a requirement to blog? 

But, first things first, diagrams to explain it all better!




As depicted in the above diagram:

1. Define the variable and the dynamic value in the .env file

In our case, env variable name is SITECORECLI_VERSION while the value is 4.2.1

-------------------------------------------------------------------------------------
# Sitecore CLI Version
SITECORECLI_VERSION=4.2.1
-------------------------------------------------------------------------------------

2. In the docker-compose.override.yml file, under the build section, define the variable under the args section, left side is the override file's variable while right side with $ is the variable from the .env file


--------------------------------------------------------------------------------------
init:
    healthcheck:
      test: ["CMD", "powershell", "ps"]
    image: ${REGISTRY}demo/${COMPOSE_PROJECT_NAME}-init:${SITECORE_VERSION}-${WINDOWSSERVERCORE_VERSION}-${DEMO_VERSION}
    isolation: ${ISOLATION}
    build:
      context: .\docker\build\init
      args:
        BUILD_IMAGE: mcr.microsoft.com/dotnet/sdk:3.1-nanoserver-${NANOSERVER_VERSION}
        BASE_IMAGE: mcr.microsoft.com/dotnet/sdk:3.1-nanoserver-${NANOSERVER_VERSION}
        SOLUTION_IMAGE: ${REGISTRY}demo/${COMPOSE_PROJECT_NAME}-solution:${SITECORE_VERSION}-${NANOSERVER_VERSION}-${DEMO_VERSION}
        NODEJS_VERSION: ${NODEJS_VERSION}        
        NUGET_STRING: ${NUGET_STRING}
        SITECORECLI_VERSION: ${SITECORECLI_VERSION}
-------------------------------------------------------------------------------------

3. In the dockerfile, receive the arg and then use the arg within %% to use it as part of the RUN command

---------------------------------------------------------------------------------------

ARG SITECORECLI_VERSION

RUN dotnet tool install --add-source=https://sitecore.myget.org/F/sc-packages/api/v3/index.json sitecore.cli --version %SITECORECLI_VERSION%

--------------------------------------------------------------------------------------

Note that anywhere in the chain you misspell the variable, all you get is a very vague error like this and it is up to you to interpret what went wrong where:

Specified version '%SITECORECLI_VERSION%' is not a valid NuGet version range.


if all goes well, you can see the env value getting passed dynamically during the build stage of init container:


Now, you just need to change the value in the env file and dockerfile will pick it up automatically!

While this covers how a env variable value can be passed to dockerfile, I saw one more place in Sitecore Demo Edge Project wherein the version is specified and it is in \Sitecore.Demo.Edge\Website\.config\dotnet-tools,json: 

I just updated this value manually for now:





Comments

Popular Posts