How to run different scripts on Windows and Linux as part of the same build

I have some build configurations that could run on either Windows or Linux (they build .NET Core code), except that there are a few little command line steps. Since the syntax of batch files on Windows and bash scripts on Linux differs I need these to be different, depending on the OS of the agent the build is running on. I don't really want to create separate build configurations just for those. (I know about templates, but the build is already templated for other reasons.) Is there some way I could have one script run on Windows, but another on Linux?

I can, of course, set up separate build steps for these, but then they will both run each build and there is no way to conditionally run a build step (even though https://youtrack.jetbrains.com/issue/TW-17939 has 592 votes!)

The best I've come up with so far is to write the following in the Linux-only script:

# Exit on Windows, but continue on Linux 2>NUL || EXIT /B 0

and the following in the Windows only script:

REM Exit on Linux, but continue on Windows 2>/dev/null || exit 0

This works, but it's a bit ugly! Is there a better way?

0
2 comments

There are two ways to do this. The first one is to use an OS-independent scripting language. There are many of them supported out of the box (from full-on building systems like gradle to actual scripting languages like ruby or powershell, even bash should be available in windows), and some of them via third party tools (like python)

 

The second one doesn't require changing your scripts, but does require changing a bit your teamcity organization. Instead of making everything a part of the same build configuration, you split your steps into separate build configurations (if you need to pass files, set them up as artifact dependencies), then tie them via snapshot dependencies, and build two separate chains. One of them builds for windows and the other builds for linux. In this situation, you might need to create the previous and later steps of the build chain as build templates and create separate copies for the separate chains.

 

This would also give you a better overview of whether it's failing on windows or on linux without having to dig into detail.

0

My two cents is to write the script in Powershell. You'll probably need a cross-platform language anyway, and it's usually much more hassle to get this working on Windows than on Linux, so that's why you should choose a native option for Windows that is runnable on Linux as well. 

I made a little starter kit with unit tests here that I know works on OS X, Linux and Windows using Powershell 4 (`apt get powershell` or something).

0

Please sign in to leave a comment.