Check Fork for Latest Release #15
Workflow file for this run
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: Check Fork for Latest Release | |
on: | |
schedule: | |
- cron: '0 12 * * *' # Runs daily at 12:00 UTC | |
workflow_dispatch: # Allows manual triggering of the workflow | |
permissions: | |
contents: write | |
pull-requests: write | |
jobs: | |
check-latest-release: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Fetch Latest Release from Upstream | |
id: latest-release | |
run: | | |
# Replace with the owner/repo of the upstream repository | |
UPSTREAM_OWNER="hashicorp" | |
UPSTREAM_REPO="terraform-provider-aws" | |
# Use GitHub API to get the latest release | |
LATEST_RELEASE=$(curl -s https://api.github.com/repos/$UPSTREAM_OWNER/$UPSTREAM_REPO/releases/latest) | |
# Extract the tag name and commit SHA from the release data | |
LATEST_RELEASE_TAG=$(echo "$LATEST_RELEASE" | jq -r .tag_name) | |
LATEST_RELEASE_COMMIT=$(curl -s https://api.github.com/repos/$UPSTREAM_OWNER/$UPSTREAM_REPO/commits/$LATEST_RELEASE_TAG | jq -r .sha) | |
echo "Latest release tag: $LATEST_RELEASE_TAG" | |
echo "Latest release commit: $LATEST_RELEASE_COMMIT" | |
echo "LATEST_RELEASE_TAG=$LATEST_RELEASE_TAG" >> $GITHUB_ENV | |
echo "LATEST_RELEASE_COMMIT=$LATEST_RELEASE_COMMIT" >> $GITHUB_ENV | |
echo "UPSTREAM_OWNER=$UPSTREAM_OWNER" >> $GITHUB_ENV | |
echo "UPSTREAM_REPO=$UPSTREAM_REPO" >> $GITHUB_ENV | |
- name: Check if Fork Contains Latest Release Commit | |
run: | | |
if git merge-base --is-ancestor $LATEST_RELEASE_COMMIT HEAD; then | |
echo "Your fork contains the latest release commit from the upstream repository ($LATEST_RELEASE_COMMIT)." | |
echo "UP_TO_DATE=true" >> $GITHUB_ENV | |
else | |
echo "Your fork does NOT contain the latest release commit from the upstream repository ($LATEST_RELEASE_COMMIT)." | |
echo "UP_TO_DATE=false" >> $GITHUB_ENV | |
fi | |
- name: Add upstream repository | |
if: env.UP_TO_DATE == 'false' | |
run: | | |
git remote add upstream https://github.com/$UPSTREAM_OWNER/$UPSTREAM_REPO.git | |
git fetch upstream --tags | |
- name: Create branch from upstream tag | |
if: env.UP_TO_DATE == 'false' | |
run: | | |
git fetch upstream $LATEST_RELEASE_TAG | |
git checkout $LATEST_RELEASE_TAG | |
new_branch="pr-from-$LATEST_RELEASE_TAG" | |
git checkout -b "$new_branch" | |
echo "NEW_BRANCH=$new_branch" >> $GITHUB_ENV | |
- name: Exclude workflow files from the branch | |
if: env.UP_TO_DATE == 'false' | |
run: | | |
git checkout $COMMIT_SHA | |
git switch --create $BRANCH_NAME | |
git checkout origin/main -- .github/workflows | |
git add .github/workflows | |
git commit -m "chore: checkout .github/workflows files from main branch" | |
- name: Push branch to fork | |
if: env.UP_TO_DATE == 'false' | |
run: | | |
git push origin $NEW_BRANCH | |
- name: Create PR | |
if: env.UP_TO_DATE == 'false' | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh pr create \ | |
--title "Update from Upstream ($LATEST_RELEASE_TAG)" \ | |
--body "This PR updates the fork with the latest changes from upstream, including the latest release tag: $LATEST_RELEASE_TAG." \ | |
--head $NEW_BRANCH \ | |
--base main | |