-
Notifications
You must be signed in to change notification settings - Fork 4
149 lines (137 loc) · 7.23 KB
/
deploy-plugin-pr.yml
File metadata and controls
149 lines (137 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Deploy Plugin (PR)
on:
pull_request:
types: [opened, edited, synchronize]
workflow_dispatch:
inputs:
prNumber:
description: 'Pull Request Number'
required: true
type: number
jobs:
Deploy_Plugin:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- run: |
$changes = git diff --name-only origin/main... ./plugins
if ( $null -eq $changes ) {
Write-Output "Nothing to Deploy as no changes were found in plugins folder..."
} else {
Write-Output "Changes found in plugins folder: $changes"
}
$pattern = '(?<=\/[v][0-9]\/).*'
$pluginToDeploy = $changes -replace $pattern | Sort-Object -Unique | Select-Object -Last 1
if ( $null -eq $pluginToDeploy ) {
Write-Output "No plugin to deploy"
Write-Output "SKIP_DEPLOYMENT=true" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
exit 0
}
$pluginToDeploy = $pluginToDeploy.TrimStart("plugins/").TrimEnd("/")
Write-Output "Plugin to Deploy: $pluginToDeploy"
Write-Output "PLUGIN_PATH=$pluginToDeploy" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
shell: pwsh
name: Get Plugin Path for Deployment
if: github.event_name != 'workflow_dispatch'
timeout-minutes: 5
- run: |
Write-Output "Getting PR Number based on trigger event..."
if ("${{ github.event_name }}" -eq "pull_request") {
$prNumber = "${{ github.event.pull_request.number }}"
} elseif ("${{ github.event_name }}" -eq "workflow_dispatch") {
$prNumber = "${{ github.event.inputs.prNumber }}"
} else {
Write-Output "Unsupported event: ${{ github.event_name }}"
exit -1
}
if (-not $prNumber) {
Write-Output "PR Number is null or empty"
exit -1
}
Write-Output "PR Number: $prNumber"
Write-Output "PR_NUMBER=$prNumber" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
shell: pwsh
name: Get PR Number
if: ${{ env. SKIP_DEPLOYMENT != 'true' }}
timeout-minutes: 5
- run: |
Write-Output "Fetching PR Info..."
$prNumber = "${{ env.PR_NUMBER }}"
$repository = "${{ github.repository_owner }}/${{ github.event.repository.name }}"
$url = "https://api.github.com/repos/$repository/pulls/$prNumber"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "token ${{ secrets.GITHUB_TOKEN }}" }
$repoUrl = $response.head.repo.html_url + ".git"
$branchName = $response.head.ref
Write-Output "Repo URL: $repoUrl | Branch Name: $branchName"
Write-Output "PR_REPO_URL=$repoUrl" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
Write-Output "PR_BRANCH_NAME=$branchName" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
Write-Output "Getting tenant to restrict deployment to..."
$repoBody = gh pr view $prNumber --json body --jq '.body' --repo $repository
Write-Output "Repo Body: $repoBody"
$tenantLine = $repoBody | Select-String -Pattern "^Tenant to Deploy to:.*"
Write-Output "Tenant Line: $tenantLine"
if ($tenantLine) {
$tenant = $tenantLine -replace "Tenant to Deploy to:\s*", "" -replace "\s*$", ""
if (-not [string]::IsNullOrWhiteSpace($tenant)) {
Write-Output "Tenant to Deploy to: $tenant"
}
else {
Write-Error "Error: 'Tenant to Deploy to' is blank."
exit -1
}
}
else {
Write-Error "Error: 'Tenant to Deploy to' field not found."
exit -1
}
Write-Output "TENANT_TO_RESTRICT_TO=$tenant" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
shell: pwsh
name: Fetch PR Info
if: ${{ env. SKIP_DEPLOYMENT != 'true' }}
timeout-minutes: 10
env:
GH_TOKEN: ${{ github.token }}
- run: |
Write-Output "Deploying Plugin..."
$pluginSuffix = "${{ env.PR_NUMBER }}"
$pluginPath = "${{ env.PLUGIN_PATH }}"
$tenantToRestrictTo = "${{ env.TENANT_TO_RESTRICT_TO }}"
$repoUrl = "${{ env.PR_REPO_URL }}"
$branchName = "${{ env.PR_BRANCH_NAME }}"
Write-Output "Deploying Plugin with suffix $pluginSuffix from $pluginPath to $tenantToRestrictTo tenant..."
Write-Output "Repo URL: $repoUrl | Branch Name: $branchName"
try {
Write-Output "Queueing deployment..."
$requestBody = @{
branchName = $branchName
customerRelease = $false
pluginPath = $pluginPath
pluginSuffix = $pluginSuffix
repositoryURL = $repoUrl
tenantToDeployTo = $tenantToRestrictTo
deploymentRequestedBy = "${{ github.actor }}"
} | ConvertTo-Json
Write-Output "Request Body: $requestBody"
$deployerUrl = "${{ secrets.DEPLOYER_BASE_URL }}/queuedeployment"
Write-Output "Deployer URL: $deployerUrl"
$authHeader = "Bearer ${{ secrets.DEPLOYER_API_KEY }}"
$response = Invoke-RestMethod -Uri $deployerUrl -Method Post -Headers @{Authorization=$authHeader} -Body $requestBody -ContentType "application/json"
Write-Output "Deployment request sent successfully"
Write-Output "Deployment ID: $($response.buildId)"
} catch {
Write-Output "Failed to deploy plugin"
Write-Output "Error: $($_.Exception.Message)"
$errMessage = $_.ErrorDetails.Message | ConvertFrom-Json
Write-Output "Error Message: $($errMessage.message)"
exit 1
}
shell: pwsh
name: Deploy Plugin
if: ${{ env. SKIP_DEPLOYMENT != 'true' }}
timeout-minutes: 30
env:
DEPLOYER_BASE_URL: ${{ secrets.DEPLOYER_BASE_URL }}
DEPLOYER_API_KEY: ${{ secrets.DEPLOYER_API_KEY }}