/build/static/layout/Breadcrumb_cap_w.png

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 configfile
    The full path of a json config file. All command line parameters except configfile and commit default to values from this
.PARAMETER srcurl
   URL of the git repo with the sources
.PARAMETER srctag
   Tag or branch to check out from the source repo. Optional
.PARAMETER trgurl
   URL of the git repo to commit the WAR file to. Optional
.PARAMETER trgbranch
   Tag or branch in the target repo to use. Optional
.PARAMETER warname
   File name of the WAR file to build
.PARAMETER wd
   Working directory. Optional. Will be deleted!
.PARAMETER jarexe
   Full path of the Java 'jar' tool
.PARAMETER gitexe
   Full path of the git binary
.PARAMETER export
   Full path and filename to export the WAR file to. Optional
.PARAMETER commit
   Flag 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 file
if (! $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 parameters

if (! $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

# processing

cd $wd


$wardir = New-Item -path $wardir -itemtype directory
Invoke-Expression -Command "$gitexe clone -q $srcurl $srcdir"
if ($trgurl) {
Invoke-Expression -Command "$gitexe clone -q $trgurl $trgdir"
if ($trgbranch) {
cd $trgdir
Invoke-Expression -Command "$gitexe checkout -q $trgbranch"
cd $wd
}
} else {
$trgdir = New-Item -path $trgdir -itemtype directory
}
if (Test-Path $warfile) {
Remove-Item $warfile
}


cd $srcdir
if ($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 $wardir
Set-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 $trgdir
Invoke-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 $homedir
Remove-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)

Be the first to answer this question

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ