diff --git a/.github/workflows/sync-files.yaml b/.github/workflows/sync-files.yaml new file mode 100644 index 0000000..2d2ef3a --- /dev/null +++ b/.github/workflows/sync-files.yaml @@ -0,0 +1,71 @@ +--- +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 }} + owner: ${{ github.ref == 'refs/heads/main' && 'xebis' || 'xebis-test' }} + repositories: ${{ matrix.repository }} + + - 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 + working-directory: target + run: | + if output=$(git status --porcelain) && [ -n "$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 + working-directory: target + run: | + echo "short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + + - 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