-
Notifications
You must be signed in to change notification settings - Fork 0
v0.4.0 Lab 7 Contribution
In this lab, you will learn how a contribution to the source repository (in this example the CARML main repository) would look like. At a customer, the same approach could be used to allow teams to contribute to a central library.
NOTE: The contribution described in this lab may already be implemented in CARML. However, my just slightly modifying the given examples (e.g. add another word) the lab will work as intended.
- Step 1 - Create a contribution branch
- Step 2 - Implement the contribution
- Step 3 - Run local test(s)
- Step 4 - Re-generate the documentation
- Step 5 - Re-run local test(s)
- Step 6 - Upload your changes and run the module pipeline
- Step 7 - Create a Pull Request
- Step 8 - Exclude environment-specific changes
For the subsequent contribution, you first need a new branch. Similar to the other labs, create one by performing the following sequence of steps:
-
In VSCode, change the branch to
main
and fetch the latest changes. You can achieve this in two ways:Alternative 1: Via VSCode's terminal
-
If Terminal is not in sight, you can alternatively open it by expanding the Terminal-dropdown on the top, and selecting New Terminal
-
Executing the following commands
git checkout 'main' git pull
Alternative 2: Via VSCode's UI
-
Initiate the branch change by selecting the current branch on the bottom left of the VSCode window
-
Next, a dropdown opens where you select the
main
branch -
Finally, you only have to trigger the
Sychronize
symbol on the bottom left next to the active branch
-
-
Next, create a new branch
contribution
. You can achieve this in two ways:Alternative 1: Via VSCode's terminal
-
If a Terminal is not in sight, you can alternatively open it by expanding the
Terminal
-dropdown on the top, and selectingNew Terminal
-
Now, execute the following PowerShell commands:
git checkout -b 'contribution' git push --set-upstream 'origin' 'contribution'
Alternative 2: Via VSCode's UI
-
Select the current branch on the bottom left of VSCode
-
Select
+ Create new branch
in the opening dropdown -
Enter the new branch name
contribution
-
Push the new branch to your GitHub fork by selecting
Publish Branch
to the left in the 'Source Control' tab
-
The first step of any contribution is its implementation. For the sake if this lab, we will suggest a simple contribution that will allow you the make use of some of our tools:
-
In your local VSCode, navigate via
modules
, on toMicrosoft.Network
and finally to therouteTable
's templatedeploy.bicep
-
A simple contribution to perform is to define an additional
output
. The Bicep template will return its value upon successful execution. As you will notice in a later lab, these outputs are particularly useful when multiple modules are orchestrated together. Please add the following snippet to the end of the file:@description('The routes of the deployed route table.') output route array = routeTable.properties.routes
-
As adding an output is a feature update, we also have to update the
version.json
file, incrementing the minor version number by 1.
CARML comes with a number of tools that you can use to perform several automated tasks for you. One of these tools can be use to perform the same tests locally as you would see in the pipeline. To do so, please perform the following tasks:
-
Navigate through the folders
utilities
andtools
and click on the scriptTest-ModuleLocally.ps1
. -
Next, you must load the function implemented in the script by triggering the
Run
button to the top right of VSCode.Note: Should VSCode ask you (in the terminal) whether you are sure you want to execute the script, please confirm
-
Once confirmed, the function will be loaded and can be invoked via the command of the same name
Test-ModuleLocally
. As stated earlier, the script replicates the feeling of the pipeline. That means, it can run a simple Pester test, but also a deployment which includes a parameter file using tokens. For our purpose, please invoke the function as follows$TestModuleLocallyInput = @{ templateFilePath = '<PathToTheUpdatedTemplate>' # Get the path via a right-click on the updated template file in VSCode & select 'Copy Path' PesterTest = $true } Test-ModuleLocally @TestModuleLocallyInput -Verbose
-
Confirm to execute the script. After a moment, the terminal will show the test cases that are executed and should show one failed test
So why did it fail? Well, as per its description: The ReadMe outputs section should document all outputs defined in the template file. Before, you added a new template output, but the readme remained in its original state.
To update the readme, we provide another utility called Set-ModuleReadMe
. This script again takes the template file path as an input an creates / updates almost all content of the module's readme file for you.
-
To get started you need to load the function first. Like before, load the script in path
utilities\tools\Set-ModuleReadMe.ps1
. -
Next, you can invoke the function as follows
Set-ModuleReadMe -TemplateFilePath '<PathToTheUpdatedTemplate>' # Get the path via a right-click on the updated template file in VSCode & select 'Copy Path'
-
Once you confirmed the execution of the script you should notice that the module's
readme.md
file is marked as modified. -
If you open version control to the left you should notice at least the template file
deploy.bicep
and readme filereadme.md
to be marked as changed. If you click on the readme specifically, a comparison view will open and show you that the readme was correctly updated
With the readme updated we can now re-run the test to confirm everything is in order.
-
Select again the terminal and either use your arrow-up key go through your previous commands until the get to the one that triggered the test (
Test-ModuleLocally
), or copy the same snipped from Step 2.$TestModuleLocallyInput = @{ templateFilePath = '<PathToTheUpdatedTemplate>' # Get the path via a right-click on the updated template file in VSCode & select 'Copy Path' PesterTest = $true } Test-ModuleLocally @TestModuleLocallyInput -Verbose
-
This time, however, none of the tests should fail:
Now that the contribution is implemented and the tests are green, you can continue to prepare everything for the subsequent pull request.
-
The first thing you have to do is to upload your changes. You can do this either via the terminal or by using the Git integration of VSCode:
Alternative 1: Via VSCode's terminal
-
If a Terminal is not in sight, you can alternatively open it by expanding the
Terminal
-dropdown on the top, and selectingNew Terminal
-
Now, execute the following PowerShell commands:
git add . git commit -m 'Added output to route table' git push
Alternative 2: Via VSCode's UI
-
Add your changes: If not already there, navigate to the source control menu to the left and add the changed files to the commit. To do so, select the
+
icon next toChanges
(appears when hovering) -
Commit your changes: Next, you should give the commit a meaningful message such as 'Added output to route table' and can then click the checkmark symbol on the top to create the commit
-
Push your changes: Finally, you can push the changes to the repository by selecting the blue
Publish Branch
button
-
-
Back in your fork, navigate to
Actions
-
From the list of actions to the left, select
Network: RouteTables
followed by theRun workflow
dropdown to the right -
Further select your branch from the
Branch:
dropdown -
And finally trigger the pipeline with the default settings by selecting
Run workflow
Subsequently, the pipeline will start running through the the same tests you executed locally, but also execute the simulated deployment, followed by an actual test deployment in Azure.
-
While the pipeline is running, we can use the time to create a Pull Request. However, before doing so, you can take the chance to create a pipeline badge that you can attach to the later Pull Request. This badge will show the reviewer that the code changes were successfully validated & tested. To create a badge, first select the three dots (
...
) to the top right of the pipeline, and further theCreate status badge
option. -
In the opening pop up, you first need to select your branch and then click on the
Copy status badge Markdown
In this step you will create the pull request. Do do so, perform the following tasks:
-
Navigate to
Pull requests
tab on the top. Here you should have multiple options:- As you just uploaded changes to a branch, there is an automated popup that allows you to create a pull request for that branch directly
- Alternatively, you can create a blank pull request via the
New pull request
button.
Select now the
New pull request
button. -
This opens a new view in the
Azure/ResourceModules
(target) repository. Here,- make sure your
branch
is selected on the top right - you provide a meaningful
title
- you provide a meaningful
description
. This description should include the pipeline badge you copied at the end of from Step 5 - select the correct
type of change
. In this case it would be aNew feature
- make sure your
-
Once you entered all the details, you can open a preview of the final look by selecting
Preview
on the top and finally selectCreate pull request
to the bottom right.
Part of your pull request are 1 (or more) files that should not be pushed into the target repository, as they contain details specific to your environment:
settings.yml
To undo these changes you can use any of the following 2 alternatives:
Alternative 1: Via VSCode's terminal
To reset these files back to original state, run the following command to connect your local repo to the upstream repo and restoring all but the routeTables
files:
- Connect to the upstream repo and restore all but the routeTable files from it.
git remote add upstream https://github.com/Azure/ResourceModules.git
git fetch upstream
git restore --source upstream/main * ':!*routeTables*'
-
See that the files are restored in "Source Control" in VSCode, and adjust if necessary. I.e. remove any of the "restored" changes you want to bring in.
-
Commit all files and push to your branch. This will update the PR too.
git commit -a -m 'Reset settings files'
git push
Alternative 2: Via VSCode's UI
- In your Visual Studio Code, open the
settings.yml
file in the root directory
- Now, open the
settings.yml
of theCARML
repository, copy its content and overwrite it in your local file in VSCode
- Once done, perform the same actions of Step 6 to upload your changes to the branch. These changes will automatically be available in the Pull Request you created earlier.
If ready, proceed to the next lab: Lab 8 - Build an ACR driven solution
This wiki is being actively developed