Azure Pipelines Set Working Directory for Efficient Builds

Author

Reads 1.1K

Woman in focus working on software development remotely on laptop indoors.
Credit: pexels.com, Woman in focus working on software development remotely on laptop indoors.

Setting the working directory in Azure Pipelines is a crucial step in ensuring efficient builds. You can set the working directory using the `workingDirectory` property in the `pool` section of your pipeline configuration.

Azure Pipelines allows you to specify a custom working directory for your pipeline, which can be a subdirectory of the repository root. This is especially useful when you have multiple projects in the same repository and want to isolate their builds.

Azure Pipelines Configuration

To configure Azure Pipelines, you need to create a YAML file that defines the pipeline's structure and behavior. This file is called the pipeline configuration file.

You can specify the working directory for your pipeline by using the `workingDirectory` variable in your YAML file. For example, if you want to set the working directory to `$(System.DefaultWorkingDirectory)/myproject`, you can do so by adding the following line to your YAML file: `workingDirectory: $(System.DefaultWorkingDirectory)/myproject`.

Azure Pipelines uses a concept called "variables" to store and manage data that's used throughout the pipeline. You can define variables in your pipeline configuration file using the `variables` keyword. For instance, you can define a variable called `myvariable` with a value of `Hello, World!` by adding the following code to your YAML file: `variables: myvariable: Hello, World!`.

Credit: youtube.com, Azure DevOps Tutorial for Beginners | CI/CD with Azure Pipelines

To make your pipeline more flexible, you can use conditional statements to execute different tasks based on certain conditions. Azure Pipelines supports conditional statements using the `if` keyword. For example, you can use the following code to check if a variable is true or false: `if: $[ variables.myvariable == 'Hello, World!' ]`.

Storage and Repository

If you're not using an inline code task to run a script, you'll have to save a script somewhere. You've got a few options where to store scripts executed via the pipeline.

Storing scripts in a source control repository is a great idea, especially if you're using a pipeline trigger from a GitHub or AzDo repository running a CI pipeline. This way, all files related to a project are managed together.

You can store your scripts in the same repo as your pipeline trigger, which is easier to manage. AzDo will check out all code in the source repo, downloading all files onto the pipeline agent.

Credit: youtube.com, Azure DevOps Tutorial for Beginners | CI/CD with Azure Pipelines

If you have scripts in another GitHub repo, you can check out multiple repos to download and run scripts stored in other repos too. This is a convenient option if you have multiple scripts across different repos.

The pipeline agent will place the scripts in the System.DefaultWorkingDirectory folder path, where they can be referenced in a task. For example, if you have a PowerShell script called script.ps1 stored in the root of your source repo, AzDo will check it out and make it available for execution.

PowerShell Task

The PowerShell task is a powerful tool in Azure Pipelines, allowing you to run PowerShell scripts directly in your pipeline.

You can specify the PowerShell code to execute either by providing a path to a script file or by adding the code directly in the YAML pipeline.

The `targetType` attribute determines how the PowerShell code is executed, with options for `filePath` or `inline`.

Credit: youtube.com, Azure DevOps and PowerShell Pipelines

If you choose to execute a script from a file, you'll need to specify the `filePath` attribute, which should point to a script in your source repository.

The `arguments` attribute allows you to pass values to parameters in your script, but only if you're executing a script from a file.

The `script` attribute is used to specify the PowerShell code to execute when using inline code.

You can use the `errorActionPreference` attribute to set the `$ErrorActionPreference` variable in your script, with options for `SilentlyContinue`, `Continue`, `Inquire`, or `Stop`.

Here's a summary of the attributes you can use with the PowerShell task:

The `workingDirectory` attribute allows you to specify the directory where the script will be executed, with a default value of `$(Build.SourcesDirectory)`.

Frequently Asked Questions

What is the default directory for pipelines in Azure?

The default directory for pipelines in Azure DevOps is the root directory of your source code repository. This is where your pipeline checks out your source code and runs subsequent steps.

Beatrice Giannetti

Senior Writer

Beatrice Giannetti is a seasoned blogger and writer with over a decade of experience in the industry. Her writing style is engaging and relatable, making her posts widely read and shared across social media platforms. She has a passion for travel, food, and fashion, which she often incorporates into her writing.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.