Update publish-krewplugin.yaml #36
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Plugin to Krew | |
on: | |
push: | |
branches: | |
- '*' # Trigger on all branches | |
release: | |
types: | |
- published | |
workflow_dispatch: # Allows manual triggering of the workflow | |
jobs: | |
windows: | |
runs-on: windows-latest | |
steps: | |
# Checkout the repository | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.ref }} # Check out the branch that triggered the workflow | |
# Ensure we're not on the main branch | |
- name: Check branch | |
run: | | |
if [ "${{ github.ref_name }}" = "main" ]; then | |
echo "This workflow does not run on the main branch." | |
exit 1 | |
fi | |
# Update the KubeTidy.psm1 file for Krew Plugin | |
- name: Update KubeTidy.psm1 for Krew Plugin | |
run: | | |
# Create a copy of KubeTidy.psm1 directly in the root | |
$sourceFile = "KubeTidy.psm1" | |
$destinationFile = "kubectl-KubeTidy" | |
Copy-Item -Path $sourceFile -Destination $destinationFile -Force | |
# Read the content of the destination file | |
$content = Get-Content $destinationFile -Raw | |
# Create a regex pattern to capture the entire parameter block | |
$pattern = '# START PARAM BLOCK[\s\S]*?# END PARAM BLOCK' # Capture the entire block, including newlines | |
# Check if the parameter block exists | |
if ($content -match $pattern) { | |
# Capture the parameter block for verification | |
$paramBlock = $matches[0] | |
# Display the captured block (optional for debugging) | |
Write-Host "Captured Block:`n$paramBlock`n" | |
# Remove the captured parameter block from the original content | |
$content = $content -replace $pattern, '' # Remove the entire block | |
# Replace the MARKER: NEW PARAM BLOCK line with the new parameter block | |
$content = $content -replace '# MARKER: NEW PARAM BLOCK', $paramBlock | |
# Prepare the new function call to be inserted | |
$functionCall = @" | |
# Call the function, passing parameters manually for cross-platform compatibility | |
Invoke-KubeTidy \$PSBoundParameters | |
"@ | |
# Replace the MARKER: FUNCTION CALL line with the new function call | |
$content = $content -replace '# MARKER: FUNCTION CALL', $functionCall | |
# Save the modified content back to the original script file | |
Set-Content -Path $destinationFile -Value $content | |
Write-Host "$destinationFile has been modified successfully." | |
} else { | |
Write-Host "No parameter block found to remove." | |
} | |
# Convert line endings from Windows (CRLF) to Unix (LF) | |
- name: Convert Line Endings | |
run: | | |
(Get-Content "kubectl-KubeTidy" -Raw) -replace "`r`n", "`n" | Set-Content "kubectl-KubeTidy" | |
linux: | |
runs-on: ubuntu-latest | |
steps: | |
# Checkout the repository | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ github.ref }} # Check out the branch that triggered the workflow | |
# Ensure we're not on the main branch | |
- name: Check branch | |
run: | | |
if [ "${{ github.ref_name }}" = "main" ]; then | |
echo "This workflow does not run on the main branch." | |
exit 1 | |
fi | |
# Create tar.gz files for Linux and Darwin | |
- name: Create Tar Files | |
run: | | |
# Get the version from the previous step | |
$version = "${{ github.ref_name }}" | |
# Create output directory for the tar files | |
$outputDir = "krewplugin" | |
mkdir -p $outputDir | |
# Create platform-specific directories | |
$linuxDir = "$outputDir/linux" | |
$darwinDir = "$outputDir/darwin" | |
mkdir -p $linuxDir | |
mkdir -p $darwinDir | |
# Move the updated script to the appropriate platform directories | |
cp kubectl-KubeTidy "$linuxDir/kubectl-KubeTidy" | |
cp kubectl-KubeTidy "$darwinDir/kubectl-KubeTidy" | |
# Copy the Private folder into each platform directory | |
cp -r Private "$linuxDir/Private" | |
cp -r Private "$darwinDir/Private" | |
# Create tar.gz files for Linux and Darwin using the tar command | |
tar -czf "$outputDir/KubeTidy-linux-amd64-$version.tar.gz" -C "$linuxDir" . | |
tar -czf "$outputDir/KubeTidy-darwin-amd64-$version.tar.gz" -C "$darwinDir" . | |
# Create a GitHub release and upload the tar.gz files | |
- name: Create GitHub Release | |
id: create_release | |
uses: actions/create-release@v1 | |
with: | |
tag_name: ${{ github.ref_name }} # Use the version from the output | |
release_name: KubeTidy Release ${{ github.ref_name }} | |
draft: false | |
prerelease: false | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Upload the Linux tar.gz Release Asset | |
- name: Upload Linux Release Asset | |
uses: actions/upload-release-asset@v1 | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ${{ github.workspace }}/krewplugin/KubeTidy-linux-amd64-${{ github.ref_name }}.tar.gz | |
asset_name: KubeTidy-linux-amd64-${{ github.ref_name }}.tar.gz | |
asset_content_type: application/gzip | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Upload the Darwin tar.gz Release Asset | |
- name: Upload Darwin Release Asset | |
uses: actions/upload-release-asset@v1 | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ${{ github.workspace }}/krewplugin/KubeTidy-darwin-amd64-${{ github.ref_name }}.tar.gz | |
asset_name: KubeTidy-darwin-amd64-${{ github.ref_name }}.tar.gz | |
asset_content_type: application/gzip | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Publish the plugin to Krew | |
- name: Update new version in krew-index | |
uses: rajatjindal/krew-release-bot@v0.0.46 |