Cloud Infrastructure

Git integration for Power Actions

Ever since we introduced Power Actions back in April, one of the most frequently asked questions we get from our customers is whether we plan to integrate with source control systems like Git. While we still don’t have that out-of-the-box in Power Actions, in this blog post, I’ll show you how to easily create a script that you can run with Power Actions to sync your script library with a Git folder.

Let’s get started.

Since Git is not available out-of-the-box in Power Actions, the first thing we need to do in our script is to install Git.

#Intall git
tdnf install -y git

The next step is to pull the scripts that we want from Git to our machine. In this sample script, we’ll sync our script library with the PowerActions folder in the PowerCLI sample scripts repo on GitHub (https://github.com/vmware/PowerCLI-Example-Scripts/tree/master/PowerActions).

#Clone the repo
git clone -n --depth=1 --filter=tree:0 https://github.com/vmware/PowerCLI-Example-Scripts.git
cd PowerCLI-Example-Scripts
git sparse-checkout set --no-cone PowerActions
git checkout
cd PowerActions

Now that we have the scripts from the Git repo, the next step is to check what we have in our script library. Since the script library is actually backed by a content library, we’ll use the PowerCLI cmdlets for content library to do this. For every script that we have retrieved from Git, we’ll check if a script with the same name already exists in the script library. If it doesn’t, that means we have to upload it there using the New-ContentLibraryItem cmdlet. If it already exists in the script library, we’ll download it from there and compare it with the file that we have retrieved from Git. No action is needed if the files are identical. However, if the file that we have downloaded from Git is different, we’ll update the script in the script library.

#Get all the files that we have cloned from the repo
$files = Get-ChildItem -Path . -File
foreach ($file in $files) {
    $name = $file.BaseName


    #Check if the item for this file already exists in the content library
    $item = Get-ContentLibraryItem -Name $name -ContentLibrary $contentLibrary -ErrorAction SilentlyContinue
    if ($item) {
        #If the item exists, check if it is up to date
        #Create a folder to store the current content library item
        if (-not (Test-Path -Path ./cl_versions -PathType Container))
        {
            New-Item -Path ./cl_versions -ItemType Directory
        }
        #Download the item from the content library
        $clFile = Export-ContentLibraryItem -ContentLibraryItem $item -Destination ((Get-Location).Path + "/cl_version") -Force
        #Compare if it's the same as the file we have downloaded from the repo
        $compResult = Compare-Object -ReferenceObject (Get-Content $file.FullName) -DifferenceObject (Get-Content ($clFile.FullName+"/"+$file.Name))
        if ($compResult) {
            #If the item is not up to date, update it
            Write-Host "Updating $name"
            Set-ContentLibraryItem -ContentLibraryItem $item -Files $file.FullName
        } else {
            Write-Host "$name is up to date"
        }
    } else {
        #If the item does not exist, create it
        New-ContentLibraryItem -Name $name -Files $file.FullName -ContentLibrary $contentLibrary
    }
}

And that’s it!

Using this script you can sync your script library with a Git folder. This way, you can easily keep your scripts up to date and share them with your team. You can also customize this script to work with other source control systems or other repos and folders.

You can find the script in the PowerActions folder in the PowerCLI sample scripts repo on GitHub. I hope you found this blog post useful and learned something new. Happy scripting!

The post Git integration for Power Actions appeared first on VMware PowerCLI Blog.

Related Articles