Skip to content

Deploy Plugin (PR)

Deploy Plugin (PR) #46

name: Deploy Plugin (PR)
on:
workflow_dispatch:
inputs:
prNumber:
description: 'Pull Request Number'
required: true
type: number
jobs:
Deploy_Plugin:
runs-on: ubuntu-latest
steps:
- name: Get Plugin Path and PR Info
run: |
$prNumber = "${{ github.event.inputs.prNumber }}"
$repository = "${{ github.repository }}"
# Get PR info
$url = "https://api.github.com/repos/$repository/pulls/$prNumber"
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "token ${{ secrets.GITHUB_TOKEN }}" }
# Get changed files in the PR
$filesUrl = $response.url + "/files?per_page=100"
$files = Invoke-RestMethod -Uri $filesUrl -Headers @{Authorization = "token ${{ secrets.GITHUB_TOKEN }}" }
Write-Output "Total files changed: $($files.Count)"
Write-Output "All changed files: $($files.filename -join ', ')"
# Find plugin changes
$pluginChanges = $files | Where-Object { $_.filename -like "plugins/*" } | Select-Object -ExpandProperty filename
Write-Output "Plugin files changed: $($pluginChanges -join ', ')"
if ($pluginChanges) {
$pattern = '(?<=\/[v][0-9]\/).*'
$pluginToDeploy = $pluginChanges -replace $pattern | Sort-Object -Unique | Select-Object -Last 1
$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
# Get PR repo and branch info
$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
# Get tenant info from PR body
$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"
Write-Output "TENANT_TO_RESTRICT_TO=$tenant" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
} 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
}
} else {
Write-Output "No plugin changes found - skipping deployment"
Write-Output "SKIP_DEPLOYMENT=true" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
}
shell: pwsh
timeout-minutes: 10
env:
GH_TOKEN: ${{ github.token }}
- run: |
Write-Output "Deploying Plugin..."
$pluginSuffix = "${{ github.event.inputs.prNumber }}"
$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 }}
- name: Add deployment summary
run: |
echo "## 🚀 Plugin Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "- **Plugin:** ${{ env.PLUGIN_PATH }}" >> $GITHUB_STEP_SUMMARY
echo "- **PR:** #${{ github.event.inputs.prNumber }}" >> $GITHUB_STEP_SUMMARY
echo "- **Tenant:** ${{ env.TENANT_TO_RESTRICT_TO }}" >> $GITHUB_STEP_SUMMARY
echo "- **Branch:** ${{ env.PR_BRANCH_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "- **Repository:** ${{ env.PR_REPO_URL }}" >> $GITHUB_STEP_SUMMARY
echo "- **Requested by:** ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY
if: ${{ env.SKIP_DEPLOYMENT != 'true' }}
shell: bash