Skip to content

Instantly share code, notes, and snippets.

@esauser
Created May 6, 2019 19:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esauser/d8e937568e93dce902b0984abcab8690 to your computer and use it in GitHub Desktop.
Save esauser/d8e937568e93dce902b0984abcab8690 to your computer and use it in GitHub Desktop.
Concourse Windows Worker
@echo off
nuget restore -Verbosity normal
if %errorlevel% neq 0 exit /b %errorlevel%
msbuild -p:Configuration=Release ^
-p:WarningLevel=0
if %errorlevel% neq 0 exit /b %errorlevel%
dotnet.exe test TestProject.csproj --configuration Release --no-build -v n
if %errorlevel% neq 0 exit /b %errorlevel%
- task: build
config:
platform: windows
inputs:
- name: input
outputs:
- name: output
run:
path: runDockerImage.bat
params:
DOCKER_IMAGE: mcr.microsoft.com/dotnet/framework/sdk:4.7.2
WORKING_DIRECTORY: input
SCRIPT_PATH: build.bat
ENV_ENVIRONMENT_VARIABLE: 1
@echo off
set DOCKER_IMAGE 1>NUL 2>NUL
if errorlevel 1 echo DOCKER_IMAGE environment variable must be set && exit /b 1
set SCRIPT_PATH 1>NUL 2>NUL
if errorlevel 1 echo SCRIPT_PATH environment variable must be set && exit /b 1
for /f "tokens=*" %%G in ('dir /b') do call set volumes=%%volumes%% -v "%cd%\%%G:C:\Concourse\%%G"
if errorlevel 1 exit /b %errorlevel%
echo. 2>environmentVariables.txt
if errorlevel 1 exit /b %errorlevel%
for /f "tokens=*" %%G in ('set ^| findstr ENV_') do echo %%G >> environmentVariables.txt
if errorlevel 1 exit /b %errorlevel%
powershell -Command "(gc environmentVariables.txt) -replace 'ENV_', '' | Out-File environmentVariables.txt -Encoding ascii"
if errorlevel 1 exit /b %errorlevel%
set WORKING_DIRECTORY 1>NUL 2>NUL
if errorlevel 1 (set workingDirectory=Concourse && cmd /c "exit /b 0") else (set workingDirectory=Concourse\%WORKING_DIRECTORY%)
if errorlevel 1 exit /b %errorlevel%
docker pull %DOCKER_IMAGE%
if errorlevel 1 exit /b %errorlevel%
docker run ^
--entrypoint "%SCRIPT_PATH%" ^
-w "C:\%workingDirectory%" ^
%volumes% ^
--env-file environmentVariables.txt ^
--rm ^
%DOCKER_IMAGE%
if errorlevel 1 exit /b %errorlevel%
@esauser
Copy link
Author

esauser commented May 6, 2019

runDockerImage.bat run the SCRIPT_PATH within the DOCKER_IMAGE mounting all inputs and outputs, setting the optional WORKING_DIRECTORY, and any ENV_ params as environment variables, trimming the ENV_. Since the container is ran with --rm it is destroyed upon error completion. The only known scenario not handled is when a job is interrupted the container continues until it either completes or errors.

@marco-m
Copy link

marco-m commented May 6, 2019

thanks!

@arut95
Copy link

arut95 commented Mar 19, 2020

This was really helpful. Thank you very much..

@arut95
Copy link

arut95 commented Mar 26, 2020

Just some correction I would suggest:
While setting environment variables, set like
for /f "tokens=*" %%G in ('set ^| findstr ENV_') do echo %%G>> environmentVariables.txt
Instead of
for /f "tokens=*" %%G in ('set ^| findstr ENV_') do echo %%G >> environmentVariables.txt

Note the space before >>

So that, all environment variable' values don't have a space at the end of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment