Skip to content

Switch Deployment to Use New Function #5

Switch Deployment to Use New Function

Switch Deployment to Use New Function #5

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@v3
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
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
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
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
$tenantLine = $repoBody | Select-String -Pattern "^Tenant to Deploy to:.*"
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
timeout-minutes: 10
- 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
} | ConvertTo-Json
$deployerUrl = "${{ secrets.DEPLOYER_BASE_URL }}/queuedeployment"
$authHeader = "Bearer ${{ secrets.DEPLOYER_API_KEY }}"
$response = Invoke-RestMethod -Uri $deployerUrl -Method Post -Headers @{Authorization=$authHeader} -Body $requestBody
Write-Output "Deployment request sent successfully"
Write-Output "Deployment ID: $($response.id)"
} catch {
Write-Output "Failed to deploy plugin"
Write-Output "Error: $($_.Exception.Message)"
exit 1
}
shell: pwsh
name: Deploy Plugin
timeout-minutes: 30