From 3108925b655c9b40a0af87bf10b07b6c5cafa35a Mon Sep 17 00:00:00 2001 From: danielsiegl <41949368+danielsiegl@users.noreply.github.com> Date: Fri, 30 Sep 2022 22:11:28 +0200 Subject: [PATCH] 170 create xmi after git commit and upload to nexus (#171) Post Commit XMI Upload and Download in a GitHub Action --- .gitignore | 1 + .../powershell/CreateNexusCredentials.ps1 | 10 ++++ .../powershell/DownloadPostCommitXMI.ps1 | 19 ++++++ .../powershell/PostCommitXMIExport.ps1 | 17 ++++++ example-scripts/powershell/post-commit.ps1 | 58 +++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 example-scripts/powershell/CreateNexusCredentials.ps1 create mode 100644 example-scripts/powershell/DownloadPostCommitXMI.ps1 create mode 100644 example-scripts/powershell/PostCommitXMIExport.ps1 create mode 100644 example-scripts/powershell/post-commit.ps1 diff --git a/.gitignore b/.gitignore index b150efe..c6fadaf 100644 --- a/.gitignore +++ b/.gitignore @@ -355,3 +355,4 @@ MigrationBackup/ /example-scripts/powershell/session_A_B.ltses /example-scripts/powershell/session_A_B.ltses - Copy.xml /example-scripts/powershell/session_A_B.ltses.xml +/*.xmi diff --git a/example-scripts/powershell/CreateNexusCredentials.ps1 b/example-scripts/powershell/CreateNexusCredentials.ps1 new file mode 100644 index 0000000..e08843e --- /dev/null +++ b/example-scripts/powershell/CreateNexusCredentials.ps1 @@ -0,0 +1,10 @@ +param +( + [Parameter(Mandatory = $true, HelpMessage="Enter: 'Username:Password'")][string] $Credential +) +$repoPath = git rev-parse --show-toplevel +$file = Join-Path -Path $repoPath -ChildPath "..\nexus.inf" +$file = [System.IO.Path]::GetFullPath($file) +echo $file +#not the best of all options - but it is a way to demonstrate the workflow. +$Credential | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "$file" \ No newline at end of file diff --git a/example-scripts/powershell/DownloadPostCommitXMI.ps1 b/example-scripts/powershell/DownloadPostCommitXMI.ps1 new file mode 100644 index 0000000..11191d6 --- /dev/null +++ b/example-scripts/powershell/DownloadPostCommitXMI.ps1 @@ -0,0 +1,19 @@ +#this script downloads xmi files from the artifact storage to be used by pipeline tools. +#typically it will run on an build agent like GitHub Actions + + +$gitcommitId = git rev-parse HEAD +$url = "https://nexus.lieberlieber.com/service/rest/v1/search?repository=xmi&name=$gitcommitId*" +Write-Host $url +$json = Invoke-RestMethod -Uri $url -Method Get + +foreach ($url in $json.items.assets.downloadUrl) { + $url = $url.Insert(4,"s") + $file = $url.Substring($url.lastIndexOf('/') + 1) + if($file.StartsWith($gitcommitId)) + { + Write-Host $file + $content = ((Invoke-WebRequest -Uri $url -Method Get) -replace "") + [IO.File]::WriteAllLines($file, $content) + } +} diff --git a/example-scripts/powershell/PostCommitXMIExport.ps1 b/example-scripts/powershell/PostCommitXMIExport.ps1 new file mode 100644 index 0000000..3bdf8e7 --- /dev/null +++ b/example-scripts/powershell/PostCommitXMIExport.ps1 @@ -0,0 +1,17 @@ +# Create the EA object +Echo "Create EA" +$ea = New-Object -ComObject "EA.Repository" -Strict +Echo "Created EA" +$ea.OpenFile("C:\github\LemonTree.Automation.Workflows\DemoModel.eapx") + +foreach($model in $ea.models) +{ + echo $model.PackageGUID +} + + + +$theProject = $ea.GetProjectInterface(); +$theProject.ExportPackageXMI("{02BAEABD-6A12-4489-BCA3-8D33C05B81DB}",22,0,-1,0,0,"C:\github\LemonTree.Automation.Workflows\export.xmi"); +$ea.CloseFile() +$ea.Exit() \ No newline at end of file diff --git a/example-scripts/powershell/post-commit.ps1 b/example-scripts/powershell/post-commit.ps1 new file mode 100644 index 0000000..587ada1 --- /dev/null +++ b/example-scripts/powershell/post-commit.ps1 @@ -0,0 +1,58 @@ +#powershell script to run from the post-commit file in ./git/hooks +#It will publish an XMI file to in our case Nexus to be used by pipelinetools that handle EA XMI +#to embedded this script in your hook file simply add the next line to the post-commit file. +#c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -File 'example-scripts\powershell\post-commit.ps1' + +#Parameters +#need to be setup for your repo +$eaModel = "DemoModel.eapx" + +#prepare environment +Write-Output "post-commit publish of XMI to Nexus" +$repoPath = git rev-parse --show-toplevel +$modelfile = Join-Path -Path $repoPath -ChildPath $eaModel +$gitcommitId = git rev-parse HEAD + +#prepare upload to Artifact Provider +$file = Join-Path -Path $repoPath -ChildPath "..\nexus.inf" +$file = [System.IO.Path]::GetFullPath($file) +#not the savest way on earth but a pragmatic solution for the sake of this demo +$SecureCredential = Get-Content "$file" | ConvertTo-SecureString +$login = (New-Object PSCredential "username",$SecureCredential).GetNetworkCredential().Password +$targetUrl = "https://nexus.lieberlieber.com/repository/xmi/" + +#start EA and load the Model +Write-Output "Create EA" +$ea = New-Object -ComObject "EA.Repository" -Strict +Write-Output "Created EA Instance" +Write-Output "Model File : $modelfile" +Write-Output "Open Model" +$ea.OpenFile($modelfile) +$theProject = $ea.GetProjectInterface(); + +#XMI Export one file per Model Root +Write-Output "Running XMI Export" +foreach($model in $ea.models) +{ + $modelName = $model.Name + $packageGuid = $model.PackageGUID + #we don't want to upload the LemonTree.Components Stuff + if (-Not $modelName.Equals("MPMS Specification and Configuration")) + { + $xmiFileName = "$gitcommitId-$modelName.xmi" + $xmiFile = Join-Path -Path $repoPath -ChildPath "$xmiFileName" + Write-Output "Exporting $modelName with $packageGuid to $xmiFile" + $result = $theProject.ExportPackageXMI("$packageGuid",22,0,-1,0,0,$xmiFile); + + Write-Output "Uploading $xmiFile to Nexus: $targetUrl" + #it only works like this for me with curl.exe + &curl.exe "-u$login" -T "$xmiFile" "$targetUrl" + } +} +Write-Output "Finished XMI Export" + +#cleanup +$ea.CloseFile() +$ea.Exit() +Write-Output "Disposed EA" +Write-Output "Finished" \ No newline at end of file