Skip to content

Commit e61904a

Browse files
authored
Auto publish (#45)
* vsts path is different * nugetkey from env also * VSTS specific publish * better auto-variable * env variables * throw not write * VSTS specific copy * better wording * different copy * better copy * more verbose * and even more verbose * expand short name * reorg * list files before publishing them * ignore one more file if there * adding dedicated script * undone original script * variable from environment * proper place for env variable
1 parent 9d40de5 commit e61904a

File tree

4 files changed

+120
-16
lines changed

4 files changed

+120
-16
lines changed

.publishignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ PesterTestResults.xml
44
tools/*
55
Tests/*
66
img/*
7-
Docs/*.md
8-
cab/*
7+
Docs/*
8+
cab/*
9+
en-US/InstallModuleFromGit-help.xml

InstallModuleFromGit.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Author = 'iricigor@outlook.com'
3030
# Copyright = '(c) 2019 Igor. All rights reserved.'
3131

3232
# Description of the functionality provided by this module
33-
Description = 'Installs module from Git repository (i.e. GitHUb or Azure DevOps)'
33+
Description = 'Installs module from Git repository (i.e. GitHub or Azure DevOps aka VSTS)'
3434

3535
# Minimum version of the Windows PowerShell engine required by this module
3636
# PowerShellVersion = ''

tools/PublishModule-VSTS.ps1

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#
2+
# Script should be executed inside of VSTS pipeline
3+
#
4+
5+
$ModuleName = 'InstallModuleFromGit'
6+
7+
if ($Env:TF_BUILD -ne 'True') {
8+
throw 'This script should be executed only from VSTS. Please use the other publishing script.'
9+
}
10+
11+
12+
# check running folder
13+
if (Test-Path "..\s\$ModuleName.psd1") {
14+
"Checking module $(Resolve-Path ".\$ModuleName.psd1")"
15+
} else {
16+
throw "We are not in correct folder, please run this tool as .\tools\PublishModule.ps1"
17+
}
18+
19+
20+
# test manifest
21+
try {
22+
$Module = Test-ModuleManifest "$ModuleName.psd1" -ea Stop
23+
"Module $ModuleName.psd1 is OK"
24+
} catch {
25+
throw 'Module manifest not in proper format'
26+
}
27+
28+
29+
# test version, must be x.y.z
30+
if (($Module.Version.ToString() -split '\.').Count -lt 3) {
31+
throw "Module version must have three segments at least, currently it is $($Module.Version.ToString())"
32+
} else {
33+
"Module version tag '$($Module.Version.ToString())' is OK"
34+
}
35+
36+
37+
# test if the module is not already published with same version number
38+
"Checking for module with version $($Module.Version) online..."
39+
if (Find-Module -Name $ModuleName -RequiredVersion ($Module.Version) -Repository PSGallery -ea 0) {
40+
throw 'Module with same version already exists'
41+
} else {
42+
"No module with version $($Module.Version) found online"
43+
}
44+
45+
46+
# get publishing key from pipeline or directly
47+
if ($Env:NugetKey) {
48+
"NugetKey found"
49+
} else {
50+
throw 'Please define NugetKey (secret) variable'
51+
}
52+
53+
# copy entire folder to temp location
54+
if ($IsLinux -or $IsMacOS) {$Destination = '/tmp'} else {$Destination = $Env:TEMP}
55+
$Destination2 = Join-Path $Destination $ModuleName
56+
if (Test-Path $Destination2) {Remove-Item $Destination2 -Recurse -Force}
57+
58+
"Copying to $Destination2"
59+
New-Item -Path $Destination2 -ItemType Directory | Out-Null
60+
Get-ChildItem -Force | Copy-Item -Destination $Destination2 -Container -Recurse
61+
"`nverifying content of $Destination2"
62+
Get-ChildItem $Destination2 -Force
63+
64+
65+
# remove not needed files (as per .publishignore)
66+
"Removing not needed files"
67+
$pwdLength = (Get-Item $Destination2).FullName.Length + 1 # expands 'C:\Users\VSSADM~1\AppData\Local\Temp'
68+
foreach ($line in (Get-Content '.publishignore'| where {$_ -notlike '#*'})) {
69+
"Checking files like $line"
70+
foreach ($file in (Get-ChildItem -Path $Destination2 -Recurse -Force -File)) {
71+
$relativeName = $file.FullName.Substring($pwdLength) -replace '\\','/'
72+
#"$relativeName"
73+
if ($relativeName -like $line) {
74+
"Removing $relativeName"
75+
Remove-Item $file.FullName -Recurse -Force
76+
}
77+
}
78+
}
79+
80+
81+
# publish
82+
"Publishing total of $((Get-ChildItem $Destination2 -Recurse -File).Count) files"
83+
Get-ChildItem $Destination2 -Recurse -File
84+
if ($Env:ModuleVersionToPublish -eq $Module.Version) {
85+
Publish-Module -Path $Destination2 -Repository PSGallery -NuGetApiKey $Env:NugetKey -Verbose
86+
"Module $ModuleName published to PowerShell Gallery"
87+
} else {
88+
throw "Mismatching module versions $($Env:ModuleVersionToPublish) and $($Module.Version), please update pipeline variable ModuleVersionToPublish"
89+
}

tools/PublishModule.ps1

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
#
12
# Script should be executed manually by developer
3+
#
4+
25
$ModuleName = 'InstallModuleFromGit'
36

7+
if ($Env:TF_BUILD -eq 'True') {
8+
throw 'This script should not be executed from VSTS. Please use dedicated script for that.'
9+
}
10+
11+
412
# check running folder
5-
if (!(Test-Path "..\$ModuleName\$ModuleName.psd1")) {
6-
throw "We are not in correct folder, please run this tool as .\tools\PublishModule.ps1"
13+
if (Test-Path "..\$ModuleName\$ModuleName.psd1") {
14+
"Checking module $(Resolve-Path ".\$ModuleName.psd1")"
715
} else {
8-
"Checking module $(Resolve-Path "..\$ModuleName\$ModuleName.psd1")"
16+
throw "We are not in correct folder, please run this tool as .\tools\PublishModule.ps1"
917
}
1018

19+
1120
# test manifest
1221
try {
1322
$Module = Test-ModuleManifest "$ModuleName.psd1" -ea Stop
@@ -16,42 +25,46 @@ try {
1625
throw 'Module manifest not in proper format'
1726
}
1827

28+
1929
# test version, must be x.y.z
2030
if (($Module.Version.ToString() -split '\.').Count -lt 3) {
2131
throw "Module version must have three segments at least, currently it is $($Module.Version.ToString())"
2232
} else {
23-
"Module version $($Module.Version.ToString()) is OK"
33+
"Module version tag '$($Module.Version.ToString())' is OK"
2434
}
2535

26-
# test if remote is not the same
36+
37+
# test if the module is not already published with same version number
2738
"Checking for module with version $($Module.Version) online..."
2839
if (Find-Module -Name $ModuleName -RequiredVersion ($Module.Version) -Repository PSGallery -ea 0) {
2940
throw 'Module with same version already exists'
3041
} else {
3142
"No module with version $($Module.Version) found online"
3243
}
3344

34-
# get nuget key from somewhere?
45+
46+
# get publishing key
3547
if ($NugetKey) {
3648
"NugetKey found"
3749
} else {
3850
throw 'Please define $NugetKey variable (run $NugetKey = Read-Host)'
3951
}
4052

41-
# copy entire folder to temp location
42-
if ($IsLinux -or $IsMacOS) {$Destination = '/tmp'}
43-
else {$Destination = $Env:TEMP}
4453

54+
# copy entire folder to temp location
55+
if ($IsLinux -or $IsMacOS) {$Destination = '/tmp'} else {$Destination = $Env:TEMP}
4556
$Destination2 = Join-Path $Destination $ModuleName
46-
"Copying to $Destination2"
4757
if (Test-Path $Destination2) {Remove-Item $Destination2 -Recurse -Force}
58+
59+
"Copying to $Destination2"
4860
Copy-Item -Path . -Destination $Destination -Recurse # it creates folder $ModuleName
4961

62+
5063
# remove not needed files (as per .publishignore)
5164
"Removing not needed files"
52-
$pwdLength = $Destination2.Length + 1
65+
$pwdLength = (Get-Item $Destination2).FullName.Length + 1 # expands 'C:\Users\VSSADM~1\AppData\Local\Temp'
5366
foreach ($line in (Get-Content '.publishignore'| where {$_ -notlike '#*'})) {
54-
#"Checking files like $line"
67+
"Checking files like $line"
5568
foreach ($file in (Get-ChildItem -Path $Destination2 -Recurse -Force -File)) {
5669
$relativeName = $file.FullName.Substring($pwdLength) -replace '\\','/'
5770
#"$relativeName"
@@ -63,6 +76,7 @@ foreach ($line in (Get-Content '.publishignore'| where {$_ -notlike '#*'})) {
6376
}
6477

6578
# publish
79+
"Publishing total of $((Get-ChildItem $Destination2 -Recurse -File).Count) files"
6680
Read-Host "All prerequisites check. Press Enter to Publish module or Ctrl+C to abort"
6781
Publish-Module -Path $Destination2 -Repository PSGallery -NuGetApiKey $NugetKey -Verbose
68-
"Module $ModuleName published to PowerShell Gallery"
82+
"Module $ModuleName published to PowerShell Gallery"

0 commit comments

Comments
 (0)