5

How do I ensure a Task Group variable is updated and accessible in non-script tasks?

In a Task Group, after setting a CONTAINER_PORT variable in a script, it's available in later script tasks, but when accessing it via $(CONTAINER_PORT), then the original task group variable value is returned.

e.g. After creating a build using the task group, I have to set the CONTAINER_PORT to 81 since all task group variables are required by default.

Task 1 - Configure Script (Bash)

containerPort=8080
if [ $(framework) == 'dotnet' ]; then containerPort=80; fi;
echo "##vso[task.setvariable variable=CONTAINER_PORT]$containerPort";

Task 2 - Generates Dockerfile (File Creator)

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE $(CONTAINER_PORT)
...

Task 2 (Output) - I expect EXPOSE 8080 here

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 81
...

Task 3 - Commit Script (Bash)

echo "Port should be set to: $CONTAINER_PORT"

Task 3 (Output)

Port should be set to: 8080

tldr; The correct $CONTAINER_PORT value is accessible in future script tasks, but not in the File Creator task which expects the file contents.

File Creator Example: File Creator

Levi Fuller
  • 13,631
  • 4
  • 38
  • 44

1 Answers1

0

Your command string is missing a ; and the agent is picky:

echo "##vso[task.setvariable variable=CONTAINER_PORT]$containerPort";

should be:

echo "##vso[task.setvariable variable=CONTAINER_PORT;]$containerPort";
                                                    ^
jessehouwing
  • 106,458
  • 22
  • 256
  • 341