Skip to article frontmatterSkip to article content

0.5GHGL aan elkaar knopen

name: Sync Changed Files to GitLab

on:
  push:
    branches: [main]

jobs:
  sync-changes:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout GitHub repo (source)
        uses: actions/checkout@v4

      - name: Set up GitLab repo (destination)
        env:
          GITLAB_TOKEN: ${{ secrets.GLGH_PAT }}
        run: |
          # Clone GitLab repo
          git clone https://oauth2:${GITLAB_TOKEN}@gitlab.tudelft.nl/opentextbooks/TN_MechaRela.git gitlab-mirror
          
          # Set up Git config
          cd gitlab-mirror
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          cd ..

      - name: Copy only changed files
        run: |
          # Compare and copy only changed files from GitHub to GitLab clone
          rsync -av --delete --exclude='.git' --exclude='gitlab-mirror/' ./ ./gitlab-mirror/

      - name: Commit and push changes
        env:
          GITLAB_TOKEN: ${{ secrets.GLGH_PAT }}
        run: |
          cd gitlab-mirror
          git add .
          
          # Only commit if there are changes
          if ! git diff --cached --quiet; then
            git commit -m "Sync changed files from GitHub"
            git push https://oauth2:${GITLAB_TOKEN}@gitlab.tudelft.nl/opentextbooks/TN_MechaRela.git main
          else
            echo "No changes to commit"
          fi