From e74fccfe1300802603b137459de5e28845a53445 Mon Sep 17 00:00:00 2001 From: Maksim Shilov <89912354+shilovmaksim@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:14:27 +0300 Subject: [PATCH] Adding python-versions-runner workflow (#137) --- .github/workflows/python-versions-runner.yml | 31 ++++++++++++ builders/invoke-workflow.psm1 | 52 ++++++++++++++++++++ builders/python-versions-runner.ps1 | 31 ++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .github/workflows/python-versions-runner.yml create mode 100644 builders/invoke-workflow.psm1 create mode 100644 builders/python-versions-runner.ps1 diff --git a/.github/workflows/python-versions-runner.yml b/.github/workflows/python-versions-runner.yml new file mode 100644 index 0000000..4ac53b8 --- /dev/null +++ b/.github/workflows/python-versions-runner.yml @@ -0,0 +1,31 @@ +name: Python versions runner +on: + workflow_dispatch: + inputs: + versions: + description: 'Versions to build' + required: true + default: '","' + publish-releases: + description: 'Whether to publish releases' + required: true + default: 'false' + +defaults: + run: + shell: pwsh + +jobs: + trigger_builds: + name: Trigger python build + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v2 + + - name: Trigger python workflow + run: | + $versions = ${{ github.event.inputs.versions }} + ./builders/python-versions-runner.ps1 -Versions $versions.Split(",") -PublishRelease ${{ github.event.inputs.publish-releases }} + env: + PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }} \ No newline at end of file diff --git a/builders/invoke-workflow.psm1 b/builders/invoke-workflow.psm1 new file mode 100644 index 0000000..2b2ffb3 --- /dev/null +++ b/builders/invoke-workflow.psm1 @@ -0,0 +1,52 @@ +function Invoke-Workflow { + param ( + [string] $Version, + [string] $PublishRelease + ) + + $payload = @{ + "ref" = "main" + "inputs" = @{ + "VERSION" = "$Version" + "PUBLISH_RELEASES" = "$PublishRelease" + } + } | ConvertTo-Json + $headers = @{ + Authorization="Bearer $env:PERSONAL_TOKEN" + } + $actionsRepoUri = "$env:GITHUB_API_URL/repos/$env:GITHUB_REPOSITORY/actions" + Invoke-RestMethod -uri "$actionsRepoUri/workflows/python-builder.yml/dispatches" -method POST -headers $headers -body $payload + + $result = [PSCustomObject]@{ + Version = $Version + Conclusion = "failure" + Url = "Not run" + } + # Triggering workflow and verifying that it has been triggered with retries + while (-not $workflowToCheck) { + Start-Sleep -seconds 40 + $workflowRuns = (Invoke-RestMethod "$actionsRepoUri/runs").workflow_runs | Where-Object {$_.status -like "*progress*" -and $_.id -ne $env:GITHUB_RUN_ID} + $workflowToCheck = $workflowRuns | Where-Object { + (Invoke-RestMethod "$actionsRepoUri/runs/$($_.id)/jobs").jobs.steps.name -like "*$Version" + } + $retries++ + if ($retries -gt 10) { + Write-Host "Workflow triggered for version '$Version' not found or something went wrong with fetching the workflow status" + return $result + } + } + # Waiting for workflow to complete + while ($workflowToCheck.status -ne "completed") { + Start-Sleep -Seconds 120 + $workflowToCheck = Invoke-RestMethod "$actionsRepoUri/runs/$($workflowToCheck.id)" + Write-Host "Workflow run with Id: $($workflowToCheck.id) for version '$Version' - status '$($workflowToCheck.status)'" + } + $result.Conclusion = $workflowToCheck.conclusion + $result.Url = $workflowToCheck.html_url + if ($workflowToCheck.conclusion -ne "success") { + Write-Host "Triggered workflow for version '$Version' completed unsuccessfully with result '$($workflowToCheck.conclusion)'. Check the logs: $($workflowToCheck.html_url)" + return $result + } + Write-Host "Triggered workflow for version '$Version' succeeded; Url: $($workflowToCheck.html_url)" + return $result +} \ No newline at end of file diff --git a/builders/python-versions-runner.ps1 b/builders/python-versions-runner.ps1 new file mode 100644 index 0000000..d84dd1a --- /dev/null +++ b/builders/python-versions-runner.ps1 @@ -0,0 +1,31 @@ +<# +.SYNOPSIS +Generate Python artifact. + +.DESCRIPTION +Script that triggering and fetching the result of the "Build python package" workflows with provided python versions + +.PARAMETER Version +Required parameter. Python versions to trigger builds for. + +.PARAMETER PublishRelease +Switch parameter. Whether to publish release for built version. + +#> + +param( + [Parameter (Mandatory=$true, HelpMessage="Python version to trigger build for")] + [array] $Versions, + [Parameter (Mandatory=$false, HelpMessage="Whether to publish release for built version")] + [string] $PublishRelease +) + +$summary = $Versions | ForEach-Object -Parallel { + Import-Module "./builders/invoke-workflow.psm1" + Invoke-Workflow -Version $_ -PublishRelease $Using:PublishRelease +} +Write-Host "Results of triggered workflows:" +$summary | Out-String +if ($summary.Conclusion -contains "failure" -or $summary.Conclusion -contains "cancelled") { + exit 1 +} \ No newline at end of file