This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
Sync theme basefiles #126
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: Sync theme basefiles | |
# this workflow is manually triggered | |
# and every day at 02:22 CET | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '22 2 * * *' | |
env: | |
UPSTREAM_REPO: ${{ vars.UPSTREAM_REPO || 'nextcloud/server' }} | |
UPSTREAM_BRANCH: ${{ vars.UPSTREAM_BRANCH || 'master' }} | |
SYNC_DIR: ${{ vars.SYNC_DIR || 'magentacloud25' }} | |
# SYNC_FILES: (${{ join(fromJSON(vars.SYNC_FILES), ' ') }}) | |
SYNC_FILES: ${{ vars.SYNC_FILES || 'README.md LICENSE' }} | |
SYNC_REPO: ${{ github.repository }} | |
SYNC_BRANCH: ${{ github.ref }} | |
jobs: | |
sync-from-upstream: | |
runs-on: ubuntu-latest | |
env: | |
upstream_path: source | |
downstream_path: syncrepo | |
steps: | |
- name: Checkout upstream code | |
uses: actions/checkout@v3 | |
with: | |
repository: ${{ env.UPSTREAM_REPO }} | |
ref: ${{ env.UPSTREAM_BRANCH }} | |
fetch-depth: 0 | |
path: ${{ env.upstream_path }} | |
token: ${{ secrets.BUILD_TOKEN }} | |
- name: Checkout repo to sync | |
uses: actions/checkout@v3 | |
with: | |
repository: ${{ env.SYNC_REPO }} | |
ref: ${{ env.SYNC_BRANCH }} | |
fetch-depth: 0 | |
path: ${{ env.downstream_path }} | |
token: ${{ secrets.BUILD_TOKEN }} | |
- name: Sync upstream files since last checkout | |
env: | |
BUILD_USER: ${{ vars.BUILD_USER }} | |
BUILD_EMAIL: ${{ vars.BUILD_EMAIL }} | |
run: | | |
echo "::notice:: Executed by $BUILD_USER, $BUILD_EMAIL" | |
UPSTREAM="$GITHUB_WORKSPACE/$upstream_path" | |
DOWNSTREAM="$GITHUB_WORKSPACE/$downstream_path/$SYNC_DIR" | |
mkdir -p $DOWNSTREAM | |
cd $DOWNSTREAM | |
git config user.name $BUILD_USER | |
git config user.email $BUILD_EMAIL | |
# sanitize linebreaks | |
SYNC_FILES="${SYNC_FILES//$'\n'/' '}" | |
SYNC_FILES="${SYNC_FILES//$'\r'/' '}" | |
#iterate over each file separately | |
for syncfile in $SYNC_FILES | |
do | |
cd $UPSTREAM | |
echo "::group::$syncfile" | |
if [ ! -f "$UPSTREAM/$syncfile" ] | |
then | |
echo "::error file=$syncfile::No upstream file [$UPSTREAM/$syncfile](https://github.com/$THEME_SYNC_OWNER/$THEME_SYNC_REPO/$syncfile)" | |
else | |
cd $DOWNSTREAM | |
# check whether the file with commits exists already downstream | |
if [ -f $DOWNSTREAM/$syncfile ] && [ "$(git log -i --format=%H -- $DOWNSTREAM/$syncfile)" ] | |
then | |
# copy commits since last downstream sync of existing file | |
lastCommitDate=$(git log -1 --format=%ci $DOWNSTREAM/$syncfile) | |
cd $UPSTREAM | |
for commitId in $(git log --since="$lastCommitDate" --format=%H --reverse $UPSTREAM/$syncfile) | |
do | |
cd $UPSTREAM | |
commitMessages=$(git log -1 --format="-m \"%s\" -m \"Upstream commit: %H\" -m \"%b\"" $commitId -- $UPSTREAM/$syncfile) | |
git show --patch $commitId > /tmp/transfer.patch | |
cd $DOWNSTREAM | |
git apply --allow-empty --include $syncfile /tmp/transfer.patch | |
eval git commit $commitMessages $DOWNSTREAM/$syncfile | |
cd $UPSTREAM | |
echo "::notice file=$syncfile::Synced commit $(git log -1 --oneline $commitId -- $UPSTREAM/$syncfile)" | |
done | |
else | |
# create initial filecopy with latest commit | |
cd $UPSTREAM | |
latestCommitMessages=$(git log -1 --format="-m \"%s\" -m \"Init upstream commit: %H\" -m \"%b\"" -- $UPSTREAM/$syncfile) | |
cd $DOWNSTREAM | |
mkdir -p $(dirname $DOWNSTREAM/$syncfile) | |
cp $UPSTREAM/$syncfile $DOWNSTREAM/$syncfile | |
git add $DOWNSTREAM/$syncfile | |
eval git commit $latestCommitMessages $DOWNSTREAM/$syncfile | |
cd $UPSTREAM | |
echo "::notice file=$syncfile::Copied $(git log -1 --oneline -- $UPSTREAM/$syncfile)" | |
fi | |
fi | |
echo "::endgroup::" | |
done | |
# cleanup | |
rm -f /tmp/transfer.patch | |
- name: Push sync update | |
run: | | |
DOWNSTREAM="$GITHUB_WORKSPACE/$downstream_path/$SYNC_DIR" | |
cd $DOWNSTREAM | |
git push |