Exporting .war file from GIT repository to another using Powershell and Json
Dear all ,
Hope you are all well today..I have been working on a script that reads parameters described in a Json file , builds war files from one GIT repository and exports them to a destination GIT repository. So far my script works processes single War files at a time but I would like it to process multiple war files. Am fairly new to Json but will be heavily using it now instead of XML.
Here is the script
#.PARAMETER configfileThe full path of a json config file. All command line parameters except configfile and commit default to values from this.PARAMETER srcurlURL of the git repo with the sources.PARAMETER srctagTag or branch to check out from the source repo. Optional.PARAMETER trgurlURL of the git repo to commit the WAR file to. Optional.PARAMETER trgbranchTag or branch in the target repo to use. Optional.PARAMETER warnameFile name of the WAR file to build.PARAMETER wdWorking directory. Optional. Will be deleted!.PARAMETER jarexeFull path of the Java 'jar' tool.PARAMETER gitexeFull path of the git binary.PARAMETER exportFull path and filename to export the WAR file to. Optional.PARAMETER commitFlag to enable committing and pushing to the target repo#[CmdletBinding()]param ([string]$configfile = $null,[string]$srcurl = $null,[string]$srctag = $null,[string]$trgurl = $null,[string]$trgbranch = $null,[string]$warname = $null,[string]$wd = $null,[string]$jarexe = $null,[string]$gitexe = $null,[string]$export = $null,[switch]$commit = $false)if (! $configfile) {$configfile = $PSCommandPath.Replace(".ps1", ".json")}try {$json = ConvertFrom-Json -InputObject (Gc $configfile -Raw -ErrorAction Stop)} catch {$Error[0]exit 1}# defaults from config fileif (! $srcurl) {$srcurl = $json.srcurl}if (! $srctag) {$srctag = $json.srctag}if (! $trgurl) {$trgurl = $json.trgurl}if (! $trgbranch) {$trgbranch = $json.trgbranch}if (! $warname) {$warname = $json.warname}if (! $wd) {$wd = $json.wd}if (! $jarexe) {$jarexe = $json.jarexe}if (! $gitexe) {$gitexe = $json.gitexe}if (! $export) {$export = $json.export}# internal defaults and compueted parametersif (! $jarexe) {$jarexe = "jar.exe"}if (! $gitexe) {$gitexe = "git.exe"}if (! $srcurl) {throw "-srcurl is required"}if (! $warname) {if (!$commit) {$warname = "temp.war"} else {throw "-warname is required"}}if (!$commit -and !$export) {$export = $PSCommandPath.Replace(".ps1", ".war")}function New-TemporaryDirectory {$parent = [System.IO.Path]::GetTempPath()[string] $name = [System.Guid]::NewGuid()New-Item -ItemType Directory -Path (Join-Path $parent $name)}if (! $wd) {$wd = New-TemporaryDirectory}$srcdir = (Join-Path $wd "srcdir")$trgdir = (Join-Path $wd "trgdir")$wardir = (Join-Path $wd "wardir")$warfile = (Join-Path $trgdir $warname)$infofile = (Join-Path $wardir "build.info")$buildts = Get-Date$homedir = Get-Location# processingcd $wd$wardir = New-Item -path $wardir -itemtype directoryInvoke-Expression -Command "$gitexe clone -q $srcurl $srcdir"if ($trgurl) {Invoke-Expression -Command "$gitexe clone -q $trgurl $trgdir"if ($trgbranch) {cd $trgdirInvoke-Expression -Command "$gitexe checkout -q $trgbranch"cd $wd}} else {$trgdir = New-Item -path $trgdir -itemtype directory}if (Test-Path $warfile) {Remove-Item $warfile}cd $srcdirif ($srctag) {Invoke-Expression -Command "$gitexe checkout -q $srctag"#git checkout tags/}Invoke-Expression -Command "$gitexe checkout-index -q -a -f --prefix=$wardir\"$id = Invoke-Expression -Command "$gitexe rev-parse HEAD"cd $wardirSet-Content -Path $infofile -Value "Git Tag: $srctag`r`nGit ID: $id`r`nBuilt date: $buildts"if ($json.exclude) {foreach ($exclude in $json.exclude) {Remove-Item $exclude}}Invoke-Expression -Command "$jarexe cf $warfile ."if ($trgurl) {cd $trgdirInvoke-Expression -Command "$gitexe add $warfile"if ($commit) {Invoke-Expression -Command "$gitexe commit -q -m 'auto build'"Invoke-Expression -Command "$gitexe push -q"}}echo "export is $export"if ($export) {Copy-Item $warfile -Destination $export}cd $homedirRemove-Item $wd -Recurse -Force
This is a sample Json file am using..
{"srcurl":"https://applebee01.webnet.net/codebase/gruntree.git","srctag":"","trgurl":"","trgbranch":"","warname":"","wd":"","jarexe":"f:\\java\\bin\\jar","gitexe":"git","export":"","exclude":["readme.txt","readme.md"]}
Any advice/suggestions will be highly appreciated.
0 Comments
[ + ] Show comments
Answers (0)
Please log in to answer
Be the first to answer this question