PowerShell and GitLab CI – Part 4

Part 3 of this series on using PowerShell and GitLab CI gave us an initial insight into how to setup and run PowerShell code as part of a build script. During it, we were introduced to the YAML build file, .gitlab-ci.yml.

This article, and the following one will cover some of the gotchas I’ve encountered whilst getting to grips with this file, and how to get these working as you’d expect. Hopefully these will help save you some time setting these up.It will also touch on some oddities you might experience creating build scripts.

Overview

At this point, I’m going to continue to use the same project with the same job name, even though it will deviate from the original script we created earlier. This is to allow us to go through the situations I’ll mention slightly faster, and I’ll indicate for each example what the subject is about. Let’s begin!

Keep Things in Line

The YAML specification demands a bit extra attention to formatting of your build scripts. Keep your ‘-‘ and labels in line.

Fails

gitlab commit 1

Succeeds

gitlab commit 2

Don’t use the TAB character

Unless you are using an editor which automatically translate the use of the TAB key into spaces, your scripts will fail. Use normal spaces.

Fails

The script below, whilst appearing indentical to the one that succeeds, uses table characters for indentation.

gitlab commit 3

Succeeds

gitlab commit 4

Watch Out For ScriptBlocks

The formatting for the use of a scriptblock can sometimes be a bit confusing. The final brace that closes the scriptblock should not have a ‘-‘ prefixing it. Additionally, if there is only one line of code within the scriptblock, it is not neccessary to use this prefix (but see below)

Fails

gitlab commit 5

Succeeds

gitlab commit 6
And if we actually look a the build output, we will see the details.
You might be quite correctly thinking that something looks a bit unusual with the output. This will be covered shortly……..

gitlab commit 7

And a look at helloworld.txt to make sure it has worked correctly.
gitlab commit 8

You Must Use ‘-‘ For Any Code Within Braces If It Consists of More Than One Line

If you have any code that surrounds itself in braces, such as a scripblock, function, or reiteration action, it will execute successfully without using the ‘-‘ character if there is only one line. However, two or more lines of code will fail.

Fails

Succeeds

It is probably best to start using ‘-‘ even with one single line within braces.

The script works, but the output on the screen is even stranger!
gitlab commit13

But the results are good….
gitlab commit14

Watch for error handling

Watch your syntax if you are wanting to use a Try..Catch block
Also, code within

Fails

gitlab commit18

Succeeds

gitlab commit19

So the script has successfully executed. Again, the output on the screen is a bit strange, but let’s check the helloworld.txt file out.

gitlab commit20

However, the file itself has not been created, despite the fact that we know this should raise a division by zero error, which should mean the catch scriptblock is executed.

Functions Behave Differently Than You Would Expect

This final part is the greatest challenge i’ve come across so far, and deserves a bit extra detail. It will also be covered further in the next article in this series.

Let’s modify the build script more. Here’s the PowerShell we’re going to use :

Here, we’ve modified our build script by adding a function which returns a string, and then output it to the same text file we’ve been using previously. If you want, you can verify the PowerShell code in the ISE, and also that the helloworld.txt file has been successfully created.

This would be put into our build script like this :

And it builds successfully
gitlab commit15

So let’s take a look at helloworld.txt
gitlab commit16

This is strange!!! There is other items there that shouldn’t be. In fact, only the last line of the file should be there.

Let’s do a bit of detective work and see if we can get more information about what’s being returned. A good starting place would be to see if we are getting a multiline string or an array returned.

So let’s take a look at helloworld.txt
gitlab commit17

This is real strange, an array is being returned. But we know that only a string is returned from the function. In the next article, we’ll dig deeper into what’s actually happening during the build process, which will shed some light on some of the strange things we’ve been noticing. Then we’ll also take a look at Lint, and how we can use it carry out checking of our YAML files.

Leave a Reply

Your email address will not be published. Required fields are marked *