|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Publishes Git Tags |
| 4 | +.DESCRIPTION |
| 5 | + Publishes Git Tags if the version has changed. |
| 6 | +
|
| 7 | + This is required to create releases on github. |
| 8 | +.NOTES |
| 9 | + Must provide a single package to publish. |
| 10 | +
|
| 11 | + Must be run from the repository in question. |
| 12 | +#> |
| 13 | +[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')] |
| 14 | +param( |
| 15 | +[Parameter(Mandatory,ValueFromPipeline)] |
| 16 | +[IO.Packaging.Package] |
| 17 | +$Package |
| 18 | +) |
| 19 | + |
| 20 | +# Throw if there is no version |
| 21 | +if (-not $package.Version) { throw "No Package Version" } |
| 22 | + |
| 23 | +# Throw if there is no repository relationship |
| 24 | +if (-not $package.RelationshipExists('repository')) { |
| 25 | + throw "Package not related to repository" |
| 26 | +} |
| 27 | + |
| 28 | +# Get the gip application (no to be confused with any functions named git) |
| 29 | +$gitApp = $ExecutionContext.SessionState.InvokeCommand.GetCommand('git', 'application') |
| 30 | + |
| 31 | +# Find our repository url. |
| 32 | +$repositoryUrl = @(& $gitApp remote)[0] | |
| 33 | + ForEach-Object { |
| 34 | + & $gitApp remote get-url $_ |
| 35 | + } |
| 36 | + |
| 37 | +# If we could not, throw. |
| 38 | +if (-not $repositoryUrl) { |
| 39 | + throw "No Repository" |
| 40 | +} |
| 41 | + |
| 42 | +# Make sure this package is related to our repository. |
| 43 | +$packageGitRepo = $package.GetRelationship('repository').TargetUri |
| 44 | +if ($packageGitRepo -ne $repositoryUrl) { |
| 45 | + throw "Package unrelated to '$repositoryUrl'" |
| 46 | + return |
| 47 | +} |
| 48 | + |
| 49 | +# Try to get a github event. |
| 50 | +$gitHubEvent = if ($env:GITHUB_EVENT_PATH) { |
| 51 | + [IO.File]::ReadAllText($env:GITHUB_EVENT_PATH) | ConvertFrom-Json |
| 52 | +} else { $null } |
| 53 | + |
| 54 | +# If there was no event, we are going to prompt. |
| 55 | +if (-not $gitHubEvent) { |
| 56 | + Write-Warning "No github event found, prompting for confirmation" |
| 57 | +} else { |
| 58 | + # If there was an event, and it is not merging a pull request, we do not want to potentially tag. |
| 59 | + if (-not ($gitHubEvent.head_commit.message -match "Merge Pull Request #(?<PRNumber>\d+)") -and |
| 60 | + (-not $gitHubEvent.psobject.properties['inputs'])) { |
| 61 | + "::warning::Pull Request has not merged, skipping Tagging" | Out-Host |
| 62 | + return |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +# Find the existing tags |
| 67 | +$existingTags = & $gitApp tag --list |
| 68 | +if (-not $existingTags) { |
| 69 | + # warn if none exist. |
| 70 | + Write-Warning "No tags found" |
| 71 | +} |
| 72 | + |
| 73 | +# Get the target version |
| 74 | +$targetVersion = "v$($package.Version)" |
| 75 | + |
| 76 | +# And find out if it already exists |
| 77 | +$versionTagExists = $existingTags -contains $targetVersion |
| 78 | + |
| 79 | +# If it does |
| 80 | +if ($versionTagExists) { |
| 81 | + # warn them |
| 82 | + "::warning::Version $($versionTagExists)" |
| 83 | + return |
| 84 | +} |
| 85 | + |
| 86 | +# If we have an ACTOR and GITHUB_ACTOR_ID |
| 87 | +if ($env:GITHUB_ACTOR -AND $env:GITHUB_ACTOR_ID) { |
| 88 | + # config git |
| 89 | + & $gitApp config --global user.email "$env:GITHUB_ACTOR_ID+$env:GITHUB_ACTOR@users.noreply.github.com" |
| 90 | + & $gitApp config --global user.name $env:GITHUB_ACTOR |
| 91 | +} |
| 92 | + |
| 93 | +# Prepare our git tag |
| 94 | +$gitTagArgs = @( |
| 95 | + 'tag' |
| 96 | + '-a' |
| 97 | + "v$($package.Version)" |
| 98 | + '-m' |
| 99 | + "$($package.Identifier) $($package.Version)" |
| 100 | +) |
| 101 | + |
| 102 | +# If -WhatIf was passed, |
| 103 | +if ($WhatIfPreference) { |
| 104 | + $gitTagArgs # output the arguments to git. |
| 105 | + return |
| 106 | +} |
| 107 | + |
| 108 | +# If there was no github event |
| 109 | +if (-not $githubEvent) { |
| 110 | + # prompt before we create tags |
| 111 | + if ($PSCmdlet.ShouldProcess("git tag $gitTagArgs")) { |
| 112 | + git @gitTagArgs |
| 113 | + } |
| 114 | + # and prompt before we push them. |
| 115 | + if ($PSCmdlet.ShouldProcess("git push origin --tags")) { |
| 116 | + git push origin --tags |
| 117 | + } |
| 118 | +} else { |
| 119 | + # If there was a GitHubEvent |
| 120 | + git @gitTagArgs # tag, |
| 121 | + git push origin --tags # push tags, |
| 122 | + $LASTEXITCODE = 0 # and set the last exit code. |
| 123 | +} |
| 124 | + |
| 125 | + |
0 commit comments