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" | |
branches: main | |
jobs: | |
validate-codeowner: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Validate Code Owner | |
run: | | |
echo "Validating if ${{ github.actor }} is a Code Owner..." | |
# Lista de Code Owners | |
CODEOWNERS=("paula-encinar" "antonio-ailabs" "marcos-rodriguez-insud") | |
# Convertir el actor actual a minúsculas para comparación | |
GITHUB_ACTOR_LOWER=$(echo "${{ github.actor }}" | tr '[:upper:]' '[:lower:]') | |
# Verificar si el actor está en la lista de Code Owners | |
if [[ ! " ${CODEOWNERS[@]} " =~ " ${GITHUB_ACTOR_LOWER} " ]]; 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: | |
runs-on: ubuntu-latest | |
needs: validate-codeowner | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
# Paso 1: Checkout del repositorio | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Asegúrate de clonar todo el historial necesario | |
# Paso 2: Autenticar GitHub CLI con GITHUB_TOKEN | |
- name: Authenticate GitHub CLI | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
# Paso 3: 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 | |
steps: | |
# Paso 4: Seleccionar el Token del Code Owner basado en github.actor | |
- name: Select Code Owner Token | |
id: select-token | |
run: | | |
echo "Selecting token for ${{ github.actor }}..." | |
TOKEN_ENV_NAME=$(echo "${{ github.actor }}" | tr '[:lower:]' '[:upper:]' | tr '-' '_' )_TOKEN | |
echo "TOKEN_ENV_NAME=${TOKEN_ENV_NAME}" >> $GITHUB_ENV | |
echo "Selected token environment variable: $TOKEN_ENV_NAME" | |
# Paso 5: Autenticar GitHub CLI con el Token seleccionado | |
- name: Authenticate GitHub CLI with Selected Token | |
run: | | |
echo "Authenticating GitHub CLI with the token for ${{ github.actor }}..." | |
gh auth logout || true | |
echo "${{ secrets[env.TOKEN_ENV_NAME] }}" | gh auth login --with-token | |
# Paso 6: Aprobar la PR con el Token del Code Owner | |
- name: Approve Pull Request with Selected Token | |
run: | | |
echo "Approving the PR #${{ needs.create-pr.outputs.pr_number }} with the token for ${{ github.actor }}..." | |
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 }} |