Create PR, Approve with User PAT, and Auto-Merge #50
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: Create PR, Approve with User PAT, and Auto-Merge | |
on: | |
workflow_dispatch: | |
inputs: | |
target_branch: | |
description: "Branch from which you want to create the PR (e.g., dev)" | |
required: true | |
default: "dev" | |
jobs: | |
validate-codeowner: | |
runs-on: ubuntu-latest | |
steps: | |
# Paso 1: Validar que el usuario que ejecuta el workflow sea un Code Owner | |
- name: Validate Code Owner | |
run: | | |
echo "Validating if ${{ github.actor }} is a Code Owner..." | |
CODEOWNERS=("paula-encinar") # Define aquí los Code Owners | |
if [[ ! " ${CODEOWNERS[@]} " =~ " ${GITHUB_ACTOR} " ]]; then | |
echo "Error: User ${{ github.actor }} is not a Code Owner. Workflow execution is not allowed." | |
exit 1 | |
fi | |
echo "$GITHUB_ACTOR is a valid Code Owner. Proceeding with the workflow..." | |
create-pr: | |
needs: validate-codeowner | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
# Paso 2: Checkout del repositorio | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
# Paso 3: Autenticar GitHub CLI con el GITHUB_TOKEN | |
- name: Authenticate GitHub CLI | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
# Paso 4: Crear la Pull Request | |
- name: Create Pull Request | |
id: create_pr | |
run: | | |
PR_OUTPUT=$(gh pr create \ | |
--base main \ | |
--head ${{ github.event.inputs.target_branch }} \ | |
--title "Merge ${{ github.event.inputs.target_branch }} into main" \ | |
--body "This PR was created automatically by the workflow." \ | |
--label automerge) | |
echo "PR created: $PR_OUTPUT" | |
# Extraer el número del PR | |
PR_URL=$(echo "$PR_OUTPUT" | grep -Eo 'https://[^ ]+') | |
PR_NUMBER="${PR_URL##*/}" | |
echo "::set-output name=pr_number::$PR_NUMBER" | |
echo "::set-output name=pr_url::$PR_URL" | |
outputs: | |
pr_number: ${{ steps.create_pr.outputs.pr_number }} | |
pr_url: ${{ steps.create_pr.outputs.pr_url }} | |
approve-pr: | |
needs: create-pr | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
# Paso 5: Aprueba la PR con tu usuario (PAULA_DEPLOYMENT) | |
- name: Approve Pull Request with Personal Account | |
run: | | |
echo "Approving the PR #${{ needs.create-pr.outputs.pr_number }} with PAULA_DEPLOYMENT..." | |
gh auth logout || true | |
echo "${{ secrets.PAULA_DEPLOYMENT }}" | gh auth login --with-token | |
gh pr review --approve "https://github.com/${{ github.repository }}/pull/${{ needs.create-pr.outputs.pr_number }}" | |
auto-merge: | |
needs: [create-pr, approve-pr] | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
# Habilitar Auto-Merge para la PR con etiqueta 'automerge' | |
- name: Enable Auto-Merge | |
run: | | |
echo "Merging PR #${{ needs.create-pr.outputs.pr_number }}..." | |
gh pr merge --auto --merge "https://github.com/${{ github.repository }}/pull/${{ needs.create-pr.outputs.pr_number }}" | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |