|
$repository = "$ENV:REPOSITORY" |
|
|
|
$ghUser = "jetersen" |
|
|
|
$filePath = Get-ChildItem .github/workflows -Recurse -ErrorAction Ignore | Select-String "uses: jenkins-infra/jenkins-maven-cd-action@" | Select-Object -ExpandProperty Path -First 1 |
|
if ([string]::IsNullOrEmpty($filePath) -eq $true) { |
|
$reusableWorkflowFound = Get-ChildItem .github/workflows -Recurse -ErrorAction Ignore | Select-String "uses: jenkins-infra/github-reusable-workflows/.github/workflows/maven-cd.yml@v1" | Select-Object -ExpandProperty Path -First 1 |
|
if ([string]::IsNullOrEmpty($reusableWorkflowFound) -eq $false) { |
|
Write-Host "$repository already uses the reusable workflow" |
|
} else { |
|
Write-Host "No jenkins-infra/jenkins-maven-cd-action found, skipping $repository" |
|
} |
|
exit 0 |
|
} |
|
|
|
& git config remote.origin.gh-resolved base |
|
|
|
$checkPullRequest = & gh pr list --author $ghUser --limit 1 --search "use jenkins infra maven cd reusable workflow" --state all |
|
|
|
if ($checkPullRequest | Select-String $prTitle) { |
|
Write-Host "Pull request already exists for $repository" |
|
exit 0 |
|
} |
|
|
|
$noteString = "# Note: additional setup is required, see https://www.jenkins.io/redirect/continuous-delivery-of-plugins" |
|
$prTitle = "chore: use jenkins infra maven cd reusable workflow" |
|
$prBody = @" |
|
🤖 Beep boop! |
|
|
|
This is an automatic pull request that updates your GitHub action used for continous delivery workflow for this repository. |
|
|
|
Switching to using a reusable workflow created to simplify maintance of this file in your repository. |
|
|
|
In case of questions, please ping `@jetersen`. |
|
|
|
Additional informations: |
|
[You can find the workflow here](https://github.com/jenkins-infra/github-reusable-workflows/blob/main/.github/workflows/maven-cd.yml) |
|
[Full explaination](https://github.com/jenkinsci/configurationslicing-plugin/pull/117#issuecomment-1156234429) |
|
"@ |
|
|
|
$desiredFileContent = @( |
|
'# Note: additional setup is required, see https://www.jenkins.io/redirect/continuous-delivery-of-plugins' |
|
'' |
|
'name: cd' |
|
'on:' |
|
' workflow_dispatch:' |
|
' check_run:' |
|
' types:' |
|
' - completed' |
|
'' |
|
'jobs:' |
|
' maven-cd:' |
|
' uses: jenkins-infra/github-reusable-workflows/.github/workflows/maven-cd.yml@v1' |
|
' secrets:' |
|
' MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}' |
|
' MAVEN_TOKEN: ${{ secrets.MAVEN_TOKEN }}' |
|
'' |
|
) |
|
|
|
[string] $desiredFileContentString = $desiredFileContent | Out-String |
|
$desiredFileLength = $desiredFileContent.Length - 1 |
|
[int] $desiredJobsIndex = $desiredFileContent | Select-String "jobs:" | Select-Object -First 1 -ExpandProperty LineNumber |
|
$desiredJobs = $desiredFileContent[$desiredJobsIndex..$desiredFileLength] |
|
|
|
$content = Get-Content "$filePath" |
|
[string] $contentString = $content | Out-String |
|
|
|
if ($contentString -eq $desiredFileContentString) { |
|
Write-Host "Content already up to date for $repository" |
|
exit 0 |
|
} |
|
|
|
$checkWorkflowValidationSteps = & yq '.jobs.validate.steps' "$filePath" |
|
$checkWorkflowValidationSize = $checkWorkflowValidationSteps | wc -l |
|
$checkWorkflowReleaseSteps = & yq '.jobs.release.steps' "$filePath" |
|
$checkWorkflowReleaseSize = $checkWorkflowReleaseSteps | wc -l |
|
if ($checkWorkflowValidationSize -eq 1 -and $checkWorkflowReleaseSize -eq 1) { |
|
Write-Host "Workflow $filePath is not valid in $repository" |
|
exit 0 |
|
} |
|
if (($checkWorkflowValidationSize -ne 21 -or $checkWorkflowReleaseSize -ne 15)) { |
|
Write-Host "Workflow $filePath is not valid in $repository" |
|
exit 0 |
|
} |
|
|
|
if ($content[0] -ne $noteString) { |
|
if ($content[0] -eq "name: cd") { |
|
$content = @($noteString, "") + $content |
|
} elseif ($content[0] -ilike "# Note:*") { |
|
$content[0] = $noteString |
|
} else { |
|
Write-Host "$filePath :" $content[0] |
|
} |
|
} |
|
|
|
[int] $contentJobsIndex = $content | Select-String "jobs:" | Select-Object -First 1 -ExpandProperty LineNumber |
|
if ($contentJobsIndex -eq 0) { |
|
continue |
|
} |
|
|
|
$contentJobsIndex -= 1 |
|
|
|
[string[]] $contentNoJobs = $content[0..$contentJobsIndex] |
|
|
|
[string[]] $content = $contentNoJobs + $desiredJobs |
|
|
|
Set-Content "$filePath" $content |
|
|
|
$checkGitStatus = & git status --porcelain |
|
if ([string]::IsNullOrEmpty($checkGitStatus)) { |
|
Write-Host "No changes to commit for $repository" |
|
exit 0 |
|
} |
|
|
|
& gh repo fork --org "jetersen-cloud" --remote-name fork --remote |
|
|
|
& git fetch --all |
|
|
|
$branch = "chore/maven-cd-reusable-workflow" |
|
|
|
& git checkout . |
|
|
|
$checkBranchExist = & git show-ref $branch |
|
if ([string]::IsNullOrEmpty("$checkBranchExist")) { |
|
& git checkout -b $branch |
|
} else { |
|
$checkBranchUsed = & git rev-parse --abbrev-ref HEAD |
|
if ("$checkBranchUsed" -ne "chore/maven-cd-reusable-workflow") { |
|
& git checkout $branch |
|
} |
|
} |
|
|
|
Set-Content "$filePath" $content |
|
|
|
$checkGitStatus = & git status --porcelain |
|
if ([string]::IsNullOrEmpty($checkGitStatus) -eq $false) { |
|
& git add . |
|
& git commit -S -s -m $prTitle |
|
& git push -u fork $branch |
|
} else { |
|
Write-Host "No changes to commit for $repository" |
|
} |
|
|
|
# # create pull request |
|
|
|
$checkPullRequest = & gh pr list --author $ghUser --limit 1 --search "use jenkins infra maven cd reusable workflow" --state all |
|
|
|
if ($checkPullRequest | Select-String $prTitle) { |
|
Write-Host "Pull request already exists for $repository" |
|
exit 0 |
|
} |
|
|
|
$pr = & gh pr create --title $prTitle --body $prBody |
|
|
|
if ($pr -ilike "https://github.com*") { |
|
Write-Host "Pull request created for $repository" |
|
Start-Sleep -Seconds 5 |
|
} else { |
|
Write-Host "Pull request creation failed for $repository" |
|
} |