Create PR, Approve with User PAT, and Auto-Merge #34
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, Merge, and Deploy | |
on: | |
workflow_dispatch: | |
inputs: | |
target_branch: | |
description: "Rama desde la cual quieres crear el PR (ej. dev)" | |
required: true | |
default: "dev" | |
jobs: | |
create-merge-deploy: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
# Paso 1: Checkout del repositorio | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Necesario para operaciones completas de Git | |
# Paso 2: Instalar GitHub CLI (gh) | |
- name: Install GitHub CLI | |
run: | | |
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg | |
sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg | |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null | |
sudo apt update | |
sudo apt install gh | |
# Paso 3: Autenticar GitHub CLI usando GITHUB_TOKEN | |
- name: Authenticate GitHub CLI | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
# Paso 4: Crear el Pull Request | |
- name: Create Pull Request | |
id: create_pr | |
run: | | |
PR_OUTPUT=$(gh pr create \ | |
--base main \ | |
--head ${{ github.event.inputs.target_branch }} \ | |
--title "Fusionar ${{ github.event.inputs.target_branch }} a main" \ | |
--body "Este PR se crea y fusionará automáticamente mediante el workflow." \ | |
--label automerge) | |
# Extraer la URL del PR creado | |
PR_URL=$(echo "$PR_OUTPUT" | grep -Eo 'https://[^ ]+') | |
echo "PR_URL=$PR_URL" >> $GITHUB_ENV | |
# Extraer el número del PR de la URL | |
PR_NUMBER="${PR_URL##*/}" | |
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
# # Paso 5: Esperar a que el PR esté listo para fusionar | |
# - name: Wait for PR to be Mergeable | |
# run: | | |
# PR_NUMBER=${{ env.PR_NUMBER }} | |
# echo "Esperando que el PR #$PR_NUMBER sea fusionable..." | |
# # Intentar hasta 10 veces (aprox. 10 minutos con espera de 60 segundos) | |
# max_attempts=10 | |
# sleep_time=60 | |
# for ((i=1;i<=max_attempts;i++)); do | |
# echo "Intento $i de $max_attempts..." | |
# # Obtener el estado del mergeable | |
# PR_INFO=$(gh pr view $PR_NUMBER --json mergeable,mergeStateStatus --jq '{mergeable: .mergeable, mergeStateStatus: .mergeStateStatus}') | |
# MERGEABLE=$(echo $PR_INFO | jq -r '.mergeable') | |
# MERGE_STATE_STATUS=$(echo $PR_INFO | jq -r '.mergeStateStatus') | |
# echo "mergeable: $MERGEABLE" | |
# echo "mergeStateStatus: $MERGE_STATE_STATUS" | |
# if [[ "$MERGEABLE" == "true" && "$MERGE_STATE_STATUS" == "CLEAN" ]]; then | |
# echo "El PR está listo para fusionar." | |
# break | |
# elif [[ "$MERGE_STATE_STATUS" == "CONFLICTING" ]]; then | |
# echo "El PR tiene conflictos de merge. Abortando." | |
# exit 1 | |
# else | |
# echo "El PR aún no está listo. Esperando $sleep_time segundos..." | |
# sleep $sleep_time | |
# fi | |
# if [[ $i -eq $max_attempts ]]; then | |
# echo "Timeout: El PR no está listo para fusionar después de $((max_attempts * sleep_time)) segundos." | |
# exit 1 | |
# fi | |
# done | |
# Paso 6: Fusionar el Pull Request | |
- name: Merge Pull Request | |
run: | | |
PR_NUMBER=${{ env.PR_NUMBER }} | |
echo "Fusionando el PR #$PR_NUMBER..." | |
gh pr merge $PR_NUMBER --merge --auto |