Ant and Git Using PowerShell

In my last post I wrote about using the Ant Migration Tool to grab the latest metadata from a Salesforce Org in order to overwrite and remove outdated metadata files. The concepts learned from that post are relevant to developers that use version control as part of their workflow.

A natural next step from that last post would be committing any metadata changes to a version control repository. In this post we will be creating a PowerShell script that executes the Ant logic from the last post and some Git commands that will commit changes to a cloud repository.

The goal of this script is as follows:

  1. Retrieve the metadata listed within an unmanaged package from a Salesforce Org
  2. Check if any metadata files within the local file structure are not present in the retrieved metadata
  3. Remove the files from the local file structure that are no longer present in Salesforce
  4. Overwrite the remaining local files with the metadata files retrieved from Salesforce
  5. Reformat the package.xml metadata file in order to remove unnecessary xml
  6. Commit source code changes locally
  7. Push source code changes to the remote repository / cloud

The first four bullets are what we accomplished in the last post. But it is important to understand that the logic outlined in this post is simply building upon it.

Here is the PowerShell script:

<#
	Created by: Greg Hacic
	Last Update: 7 February 2020 by Greg Hacic
	Questions?: greg@ities.co
	
	Notes:
		- PowerShell script automates the retrieval of metadata for a Salesforce Org and backing up of that metadata to a cloud Git repository
#>

Write-Host "Backup of Salesforce unmanaged package"

Write-Host "Local Git directory"
Set-Location C:\Desktop\Git\Salesforce\org\app\

Write-Host "Turn off auto-conversion of carriage returns and line feeds for Git"
git config --global core.autocrlf false

Write-Host "Checkout the production branch"
git checkout master

Write-Host "Pull master branch from remote"
git pull origin master

Write-Host "Local Ant directory"
Set-Location C:\Desktop\Git\Salesforce\CI\app\

Write-Host "Retrieve current metadata from Salesforce"
ant retrievePackage

Write-Host "Local Git directory"
Set-Location C:\Desktop\Git\Salesforce\org\app\src\

Write-Host "Remove the fullname, apiAccessLevel, namespacePrefix and description tags from package.xml"
Get-Content package.xml | Where-Object {$_ -notmatch "<fullName>" -and $_ -notmatch "<description>" -and $_ -notmatch "<apiAccessLevel>" -and $_ -notmatch "<namespacePrefix>"} | Set-Content package2.xml

Write-Host "Delete package.xml"
Remove-Item -Path C:\Desktop\Git\Salesforce\org\app\src\package.xml

Write-Host "Rename the package2.xml to package.xml"
Rename-Item -Path "C:\Desktop\Git\Salesforce\org\app\src\package2.xml" -NewName "package.xml"

Write-Host "cd ../"
Set-Location C:\Desktop\Git\Salesforce\org\app\

Write-Host "Timestamp logic..."
$Year = Get-Date -UFormat %Y
$Month = Get-Date -UFormat %m
$Day = Get-Date -UFormat %d
$Hour = Get-Date -UFormat %H
$Minute = Get-Date -UFormat %M
$Second = Get-Date -UFormat %S
$Date = "$Year-$Month-$Day"
$Time = "$Hour$Minute$Second"
$timestamp = -join("$Date", "T", "$Time")

Write-Host "Stage all changes for commit"
git add -A

Write-Host "Commit the changes"
$MonthName = Get-Date -UFormat %B
git commit -a -m "$Day $MonthName $Year ($timestamp) backup processing."

Write-Host "Push the local version to Bitbucket cloud"
git push origin master

Write-Host "Processing complete"

The script comments should help you get your head around what's going on. Feel free to reach out if you have questions though.

Automated Exchange Rates in Salesforce.com

Reduce Repetitive Tasks, Eliminate Errors & Free Up Your Administrators.

Birthday Reminders for Salesforce.com

It might lead to a sale. Or it might make you feel good.