For small Runbooks, the use of the web interface seems reasonable. However, when your script starts getting larger and more professional with a lot of functions and contributors to the code, you need to step up your game and source control is the answer for most of your challenges.
In this article, we are going to integrate three components: Azure Automation, GitHub, and Visual Studio Code (VSCode) to demonstrate end-to-end how we can manage code in a better but still an easy-to-manage format. In the diagram below, we can see the communication flow between the on-prem (workstation) and GitHub is bi-directional, and any changes performed at GitHub are replicated to the Azure Automation in a one-way direction.
Managing your GitHub
I assume that you already have an account created in GitHub. If not, you can sign up before continuing this section.
The first step is to create a new repository. Logged on to GitHub, click on your photo on the upper right corner, and click on Your Repositories. Click on the New button, and on the new page, define a repository name (I recommend using the name of the Automation Account that you have already provisioned in Azure), a description, and if it is going to be public or private. When complete, click on Create repository.
One last step is to get the URL for this repository. In the new page, click on Clone or Download button, and in the new dialogue box, copy the URL. We are going to use that information when cloning the repository in our Visual Studio Code.
Configuring your workstation to use GitHub
In your machine that you use to test and validate code, we must make sure that we have at least Git and Visual Studio Code installed on the system. I wrote an article going over all those steps, and if you are not sure or want to review, I recommend skimming through it here.
We are going to start creating a folder to store all scripts related to the Azure Automation that we are planning to use source control. Let’s start simple and create an initial structure having all our future Automation Accounts under C:\DevOps.
For each Automation Account, we are going to create a subfolder. In this article we are going to call it svc-azdev-automation. Right-click on it, and click on Open with Code.
In the Visual Studio Code, click on Source Control (icon), and click on + (add). The command pallet will pop-up from the top center of the screen, select the folder that we are currently working on.
We will start the cloning process by hitting F1, then type git clone, and select it from the list. The command palette will ask for the URL (we have it already from the previous section, paste it) and hit enter. A new dialogue box will be displayed, and we need to select the folder where the cloned files will be placed, in our case C:\DevOps\svc-azdev-automation folder. Click on Select Repository Location when done. A small information popup will be displayed in the Visual Studio Code notification area. Click on Open.
A GitHub Login window will be displayed. Provide your credentials and wait for the completion of the cloning on your computer.
Now that we have cloned the GitHub repository, it is time to perform some local changes and see how it impacts the environment. In Visual Studio Code, click on File and then New file. Write some content and save it. (In our example: EnvClone.ps1.)
You will notice that there is an indicator on the source control icon informing how many changes we have locally. To commit those changes locally, click on the Source Control icon, then … and click on commit all. The command palette will ask for a commit message. Type something meaningful and hit enter.
As soon as we commit the changes locally, we have an indicator located on the left bottom corner of the window (the second position from left to right), which helps to understand how many changes we have locally vs. remote to synchronize. Click on that area. A new dialogue box saying This action will push and pull commits to and from “origin/master” click either OK or OK, Don’t show it again.
A notification will show up asking to enable automatically fetch content from GitHub, accept it when requested.
The result of that operation is going to be a new file on the GitHub repository, as we can see in the image below.
When a file is created or changed in GitHub, as soon as we synchronize in the Visual Studio Code, the changes will be updated automatically on your local git.
Connecting Azure Automation with GitHub
The final step is to configure GitHub with Azure Automation. All code changed or added in GitHub can be added to the Azure Automation account.
In the Automation account properties blade, click on Source Control (Item 1), and click Add (Item 2). In the new blade (Item 3), assign a name, select GitHub as a Source control type. Click on Authenticate and provide your credentials to access GitHub in the new page. When authenticated, select the Repository and Branch.
We can also define a specific folder within the repo, and if we want to auto sync and publish the changes, I would recommend at least Auto Sync enabled. Click on Save to create the connection.
Note: We can always click on the existing connections and click on Start Sync to trigger a manual synchronization.
After the connection is established, we can always check the tab Sync jobs, and a list of all synchronization that has taken place will be listed. We can click on any given synchronization job and see more logs and information about that specific run.
As soon as we commit a file in the GitHub, synchronization will be triggered, and a new Runbook will be available in Azure Automation.
Azure Automation and GitHub: Final points
When using source control, the recommendation is to focus on using GitHub as a central repository. All updates in GitHub will be carried to the Azure Automation automatically. Keep in mind that changes performed in the Runbooks using Azure Portal will not be updated in the GitHub. That requires a difference in the process to edit Runbooks.
Featured image: Freepik / katemangostar