Create a pre-build bash script to edit files in a cloud pipeline

In order to make changes to code files from our pipeline, sometimes we have to create a bash script. A pipeline can use a bash script to edit files before the files are compiled into an application. I used an Azure DevOps pipeline in this example but the bash script task can be applied to an AWS pipeline as well.

To get started, you need to add a Bash script task to your pipeline, and add an inline script. This task must occur before the code builds to ensure that your changes are compiled. After adding a bash task to an Azure pipeline and running the pipeline you will see the task echo ‘Hello World’ in the job details of a pipeline run.

The first thing you need to consider is how can you locate the file you wish to edit. A pipeline will be connected to source control and we need to assign a file path to the file we wish to edit. You can start by adding an ls command to your new bash script. This will echo out all the folders within the current directory.

The OS of the build agent that your pipeline runs on will depict how we formulate the path to the file. On Mac/Linux build agents, you can find all the files you need in $(System.DefaultWorkingDirectory). System.DefaultWorkingDirectory is a pre-defined variable that is used during pipeline execution or staging downloaded artifacts from build pipeline. For Windows, I found that I could not echo any folders using ls $(System.DefaultWorkingDirectory), rather I could echo everything with just ls. You can list out folder contents until you find the file you are looking to edit. Once you’ve formulated the path, store it in a variable in your bash script.

# Windows - Store path to file you wish to edit in variable
File_To_Change='Path/To/File.cs'
# Linux/Mac - Store path to file you wish to edit in variable
File_to_Change=$(System.DefaultWorkingDirectory)Path/To/File.cs

In order to prove that you edited the file, it’s a good idea to echo out it’s contents before you make your changes. You can do this by using the cat command to call the File_To_Change variable you created earlier.

# Echo's out files contents before
echo 'BEFORE'
cat $File_To_Change

To edit the file in bash, we use the stream editor command, sed. The sed command can be used to perform crud operations on a file using complex pattern matching. If you want to edit the file in your variable you can replace all the instances of “Hello” with “World” using this script.

# Linux based build agent, Replace code in file
sed -i "" "s/Hello/World/g" $File_To_Change
# Windows build agent, Replace code in file
sed -i "s/Hello/World/g" $File_To_Change

After you make your changes you need to echo out it’s contents using cat again, so you can ensure that you made your changes.

echo 'AFTER'
cat $File_To_Change

You should now have a basic script that can edit files from a branch in source control as a step in your pipeline. It’s important to ensure that this step occurs before the build task that compiles your code into an application binary. Consider storing data in variables when swapping out keys or other pipeline specific information. I hope that you found this article useful and that you’re able to get started adding a pre-build script to your pipeline