Skip to content

Sync Upstream on New Upstream Release #13

Sync Upstream on New Upstream Release

Sync Upstream on New Upstream Release #13

Workflow file for this run

name: Sync Upstream on New Upstream Release
on:
schedule:
# Run on the 1st day of every month at midnight
- cron: "0 0 1 * *"
workflow_dispatch:
# Allow manual triggering of the workflow
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout Fork Repository
uses: actions/checkout@v4
with:
repository: ${{ github.repository }} # Your fork's repository
fetch-depth: 0
- name: Set Git Config
run: |
git config --global user.name "GitHub Action"
git config --global user.email "[email protected]"
- name: Set Upstream Repository
run: git remote add upstream https://github.com/nielsfaber/alarmo.git
- name: Fetch Upstream Changes
run: git fetch upstream --tags
- name: Check Latest Release from Upstream
id: get-upstream-release
uses: actions/github-script@v6
with:
script: |
const upstreamRepo = 'nielsfaber/alarmo';
const upstreamRelease = await github.rest.repos.getLatestRelease({
owner: upstreamRepo.split('/')[0],
repo: upstreamRepo.split('/')[1],
});
core.setOutput('upstream_tag', upstreamRelease.data.tag_name);
core.setOutput('release_body', upstreamRelease.data.body);
core.setOutput('html_url', upstreamRelease.data.html_url);
- name: Check Latest Release in Fork
id: get-fork-release
uses: actions/github-script@v6
with:
script: |
const forkRepo = process.env.GITHUB_REPOSITORY;
try {
const forkRelease = await github.rest.repos.getLatestRelease({
owner: forkRepo.split('/')[0],
repo: forkRepo.split('/')[1],
});
core.setOutput('fork_tag', forkRelease.data.tag_name);
} catch (error) {
// No release in the fork yet
core.setOutput('fork_tag', '');
}
- name: Compare Releases
id: compare-releases
run: |
echo "Upstream tag: ${{ steps.get-upstream-release.outputs.upstream_tag }}"
echo "Fork tag: ${{ steps.get-fork-release.outputs.fork_tag }}"
if [ "${{ steps.get-upstream-release.outputs.upstream_tag }}" != "${{ steps.get-fork-release.outputs.fork_tag }}" ]; then
echo "New upstream release found."
echo "new_release=true" >> $GITHUB_ENV
else
echo "No new release found."
echo "new_release=false" >> $GITHUB_ENV
fi
- name: Rebase Upstream (if new release)
if: env.new_release == 'true'
run: |
git checkout main
git rebase upstream/main
- name: Push Changes to Fork (if new release)
if: env.new_release == 'true'
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
git push -f https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }} main
- name: Create New Release in Fork (if new release)
if: env.new_release == 'true'
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GH_TOKEN }}
tag: ${{ steps.get-upstream-release.outputs.upstream_tag }}
name: ${{ steps.get-upstream-release.outputs.upstream_tag }} (with blake3)
body: "This release syncs with the latest upstream release. [Original Release](${{ steps.get-upstream-release.outputs.html_url }})"
draft: false
prerelease: false