Skip to content

aep-repo-trigger

aep-repo-trigger #272

name: Update AEP Specs
on:
workflow_dispatch:
repository_dispatch:
types: [aep-repo-trigger]
# Sets permissions of the GITHUB_TOKEN
permissions:
contents: write # Required to push commits and create branches
pull-requests: write # Required to create and label PRs
# Make sure to:
# GH Repo Settings -> Actions -> General -> Workflow permissions -> [x] Allow GitHub Actions to create and approve pull requests
# GH Repo Settings -> Branches -> Add branch ruleset -> call it "default-main" or however you like, leave defaults and click "Save changes"
jobs:
update-specs:
runs-on: ubuntu-latest
outputs:
merge_complete: ${{ steps.create-merge-pr.outputs.merge_complete }}
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0 # Fetch all history for all branches
- name: Check for existing PR
id: check-pr
run: |
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
PR_EXIST=$(gh pr list --search "Update AEP Specs" --state open --json number --jq 'length')
echo "pr_exists=$PR_EXIST" >> $GITHUB_OUTPUT
if [ "$PR_EXIST" -gt "0" ]; then
PR_INFO=$(gh pr list --search "Update AEP Specs" --state open --json number,headRefName --jq '.[0]')
PR_NUMBER=$(echo $PR_INFO | jq -r '.number')
PR_BRANCH=$(echo $PR_INFO | jq -r '.headRefName')
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
echo "pr_branch=$PR_BRANCH" >> $GITHUB_OUTPUT
fi
- name: Clone and Update Specs
id: update-specs
run: |
# Save current state
if [ -d "src/content/aeps" ]; then
mkdir -p ../old_specs
cp -r src/content/aeps/* ../old_specs/
fi
# Clone the AEP repository
rm -rf temp_aep
git clone --depth 1 https://github.com/akash-network/AEP.git temp_aep
# Clear existing specs and copy new ones
rm -rf src/content/aeps
mkdir -p src/content/aeps
cp -r temp_aep/spec/* src/content/aeps/
rm -rf temp_aep
# Check for changes
if [ -d "../old_specs" ]; then
if diff -r ../old_specs src/content/aeps > /dev/null 2>&1; then
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "No changes detected in specs"
else
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Changes detected in specs"
diff -r ../old_specs src/content/aeps || true
fi
else
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Initial specs setup"
fi
# Cleanup old specs
rm -rf ../old_specs
- name: Update Existing PR
if: steps.update-specs.outputs.has_changes == 'true' && steps.check-pr.outputs.pr_exists != '0'
run: |
# Configure git
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
# Stash any uncommitted changes
git stash --include-untracked
# Fetch and checkout PR branch
git fetch origin ${{ steps.check-pr.outputs.pr_branch }}
git checkout ${{ steps.check-pr.outputs.pr_branch }}
# Apply stashed changes (if any)
git stash pop || echo "No stashed changes to apply"
# Stage and check for changes
git add src/content/aeps
if git diff --staged --quiet; then
echo "No changes to commit after staging"
exit 0
fi
# Commit and push changes
git commit -m "chore: update AEP specs [skip ci]"
git push origin ${{ steps.check-pr.outputs.pr_branch }}
echo "Updated existing PR #${{ steps.check-pr.outputs.pr_number }}"
- name: Create &/ Merge PR if Needed
id: create-merge-pr
if: steps.update-specs.outputs.has_changes == 'true'
run: |
# Configure git
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
if [[ "${{ steps.check-pr.outputs.pr_exists }}" -gt "0" ]]; then
echo "Merging existing PR #${{ steps.check-pr.outputs.pr_number }}..."
# If fails with "GraphQL: Pull request Protected branch rules not configured for this branch (enablePullRequestAutoMerge)", then either create Classic Branch protection rule OR use: gh pr merge --admin --merge --delete-branch ${{ steps.check-pr.outputs.pr_number }}
# Ref. https://github.com/orgs/community/discussions/129063#discussioncomment-10689765
gh pr merge --auto --squash --delete-branch ${{ steps.check-pr.outputs.pr_number }}
else
echo "No existing PR found. Creating a new one..."
branch_name="update-aep-specs-$(date +%Y%m%d-%H%M%S)"
git checkout -b $branch_name
# Stage changes and check if there's anything to commit
git add src/content/aeps
if git diff --staged --quiet; then
echo "No changes to commit after staging"
exit 0
fi
git commit -m "chore: update AEP specs"
git push origin $branch_name
# Create PR and capture the PR URL
PR_URL=$(gh pr create \
--title "Update AEP Specs $(date +%Y-%m-%d)" \
--body "Automated PR to update AEP specifications" \
--base main \
--head $branch_name)
# Extract PR number from PR URL
PR_NUMBER=$(echo $PR_URL | grep -oE '[0-9]+$')
echo "Merging newly created PR #$PR_NUMBER..."
# If fails with "GraphQL: Pull request Protected branch rules not configured for this branch (enablePullRequestAutoMerge)", then either create Classic Branch protection rule OR use: gh pr merge --admin --merge --delete-branch $PR_NUMBER
# Ref. https://github.com/orgs/community/discussions/129063#discussioncomment-10689765
# Make sure freshy created PR is MERGEABLE before proceeding!
# Otherwise it will fail with either:
# - "GraphQL: Pull request Auto merge is not allowed for this repository (enablePullRequestAutoMerge)"
# - "GraphQL: Pull request Pull request is in clean status (enablePullRequestAutoMerge)"
echo "Waiting until PR is mergeable ..."
for i in {1..10}; do
MERGEABLE=$(gh pr view "$PR_URL" --json mergeable --jq '.mergeable')
echo "Mergeable status: $MERGEABLE"
if [ "$MERGEABLE" = "MERGEABLE" ]; then
break
fi
echo "Waiting for PR to become mergeable..."
sleep 3
done
gh pr merge --auto --squash --delete-branch $PR_NUMBER
fi
echo "merge_complete=true" >> $GITHUB_OUTPUT
# to trigger the Astro Build workflow (for repo that use Classic Branch protection)
trigger-astro:
name: Trigger Astro Build
needs: update-specs
if: needs.update-specs.outputs.merge_complete == 'true'
runs-on: ubuntu-latest
steps:
- name: Dispatch event to trigger Astro build
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
event-type: aep-spec-update