From f28813912166f347901b7dadf93fb2e03df103c1 Mon Sep 17 00:00:00 2001 From: Martin Bruzina Date: Fri, 3 Oct 2025 18:43:30 +0200 Subject: [PATCH 1/4] feat: add sync files workflow --- .github/workflows/sync-files.yaml | 69 +++++++++++++++++++++++++++++++ README.md | 17 ++++++++ 2 files changed, 86 insertions(+) create mode 100644 .github/workflows/sync-files.yaml diff --git a/.github/workflows/sync-files.yaml b/.github/workflows/sync-files.yaml new file mode 100644 index 0000000..0c2da89 --- /dev/null +++ b/.github/workflows/sync-files.yaml @@ -0,0 +1,69 @@ +--- +name: Sync Files + +on: + push: + +jobs: + sync: + runs-on: ubuntu-latest + strategy: + matrix: + repository: + - .github + steps: + - name: Checkout source repository + uses: actions/checkout@v5 + with: + path: source + + - name: Create GitHub App token + uses: actions/create-github-app-token@v2 + id: gh-app-token + with: + app-id: ${{ vars.GH_APP_SEM_REL_ID }} + private-key: ${{ secrets.GH_APP_SEM_REL_PEM_FILE }} + + - name: Checkout target repository + uses: actions/checkout@v5 + with: + repository: ${{ format('{0}/{1}', github.ref == 'refs/heads/main' && 'xebis' || 'xebis-test', matrix.repository) }} + path: target + token: ${{ steps.gh-app-token.outputs.token }} + + - name: Copy files + run: | + yq -r '.copy[]' target/.github/sync.yaml | while IFS= read -r file; do + source_file="source/$file" + target_file="target/$file" + mkdir -p "$(dirname "$target_file")" + cp "$source_file" "$target_file" + done + + - name: Detect sync updates + id: updates + run: | + if output=$(git status --porcelain) && [ -z "$output" ]; then + echo "updated=true" >> "$GITHUB_OUTPUT" + else + echo "updated=false" >> "$GITHUB_OUTPUT" + fi + + - name: Output short commit SHA + if: steps.updates.outputs.updated == 'true' + id: sha + run: | + pushd target + echo "short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + popd + + - name: Create Pull Request with applied automatic fixes + if: steps.updates.outputs.updated == 'true' + uses: peter-evans/create-pull-request@v7 + with: + path: target + token: ${{ steps.gh-app-token.outputs.token }} + branch: "feature/sync-files-for-${{ steps.sha.outputs.short }}" + commit-message: "chore: [sync-files] sync files" + title: "[Sync Files] Sync Files" + labels: bot diff --git a/README.md b/README.md index 7ef39d4..70cfdc9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ A collection of GitHub composite actions and reusable workflows. - [Reusable Megalinter Workflow](#reusable-megalinter-workflow) - [Reusable Semantic Release Workflow](#reusable-semantic-release-workflow) +- [Sync Files Workflow](#sync-files-workflow) ### Reusable Megalinter Workflow @@ -92,6 +93,22 @@ plugins: - semantic-release-major-tag ``` +### Sync Files Workflow + +[Sync Files Workflow](.github/workflows/sync-files.yaml) synchronizes the repository contents by creating a pull request to the target repositories. + +Configure target repositories: + +- Target repositories are specified at the source repository [Sync Files Workflow](.github/workflows/sync-files.yaml) in the matrix. +- Files to sync are specified in the target repository file `.github/sync.yaml`: + + ```yaml + --- + copy: + - .github/workflows/semantic-release.yaml + - .releaserc.yaml + ``` + ## Credits and Acknowledgments - Martin Bružina - Author From fab4907993957ee402eeb266a1fa45cf90cd8e98 Mon Sep 17 00:00:00 2001 From: Martin Bruzina Date: Sat, 4 Oct 2025 08:31:16 +0200 Subject: [PATCH 2/4] fix: wrong sync files working directory --- .github/workflows/sync-files.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sync-files.yaml b/.github/workflows/sync-files.yaml index 0c2da89..0df8d88 100644 --- a/.github/workflows/sync-files.yaml +++ b/.github/workflows/sync-files.yaml @@ -42,6 +42,7 @@ jobs: - name: Detect sync updates id: updates + working-directory: target run: | if output=$(git status --porcelain) && [ -z "$output" ]; then echo "updated=true" >> "$GITHUB_OUTPUT" @@ -52,10 +53,9 @@ jobs: - name: Output short commit SHA if: steps.updates.outputs.updated == 'true' id: sha + working-directory: target run: | - pushd target echo "short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - popd - name: Create Pull Request with applied automatic fixes if: steps.updates.outputs.updated == 'true' From 726fcff018f93a7001f36bcacde381062ca84e15 Mon Sep 17 00:00:00 2001 From: Martin Bruzina Date: Sat, 4 Oct 2025 08:45:15 +0200 Subject: [PATCH 3/4] fix: sync files don't detect changed files correctly --- .github/workflows/sync-files.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sync-files.yaml b/.github/workflows/sync-files.yaml index 0df8d88..af136a5 100644 --- a/.github/workflows/sync-files.yaml +++ b/.github/workflows/sync-files.yaml @@ -44,7 +44,7 @@ jobs: id: updates working-directory: target run: | - if output=$(git status --porcelain) && [ -z "$output" ]; then + if output=$(git status --porcelain) && [ -n "$output" ]; then echo "updated=true" >> "$GITHUB_OUTPUT" else echo "updated=false" >> "$GITHUB_OUTPUT" From 82026c4a6eee135d637b698b1a9912e6cab36b90 Mon Sep 17 00:00:00 2001 From: Martin Bruzina Date: Sat, 4 Oct 2025 23:06:53 +0200 Subject: [PATCH 4/4] fix: missing parameters to create github app token --- .github/workflows/sync-files.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/sync-files.yaml b/.github/workflows/sync-files.yaml index af136a5..2d2ef3a 100644 --- a/.github/workflows/sync-files.yaml +++ b/.github/workflows/sync-files.yaml @@ -23,6 +23,8 @@ jobs: with: app-id: ${{ vars.GH_APP_SEM_REL_ID }} private-key: ${{ secrets.GH_APP_SEM_REL_PEM_FILE }} + owner: ${{ github.ref == 'refs/heads/main' && 'xebis' || 'xebis-test' }} + repositories: ${{ matrix.repository }} - name: Checkout target repository uses: actions/checkout@v5