Create PR, Approve with User PAT, and Auto-Merge #59
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-24.04 | |
steps: | |
- name: Validate Code Owner | |
run: | | |
echo "Validating if ${{ github.actor }} is a Code Owner..." | |
# Lista de Code Owners | |
CODEOWNERS=("paula-encinar") | |
# 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-24.04 | |
needs: validate-codeowner | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Authenticate GitHub CLI | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
- 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" | |
PR_URL=$(echo "$PR_OUTPUT" | grep -Eo 'https://[^ ]+') | |
PR_NUMBER="${PR_URL##*/}" | |
# Write outputs to environment file | |
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
echo "PR_URL=$PR_URL" >> $GITHUB_ENV | |
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-24.04 | |
steps: | |
- 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" | |
- 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 | |
- 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-24.04 | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- 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 }} |