diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml new file mode 100644 index 0000000000..7441ef4b72 --- /dev/null +++ b/.github/workflows/deployment.yml @@ -0,0 +1,59 @@ +name: Deploy to GitHub Pages + +on: + # Trigger only on push to master branch + push: + branches: + - master + + # Allow manual trigger if needed + workflow_dispatch: + +jobs: + deploy: + runs-on: ubuntu-latest + + # Run only when the repository is the main repository + if: ${{ github.repository == 'sugarlabs/musicblocks'}} + + steps: + # 1. Checkout the repository + - name: Checkout repository + uses: actions/checkout@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + # 2. Configure Git for GitHub Actions + - name: Configure Git + run: | + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git + git config --global user.name "GitHub Actions" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + + # 3. Prepare the deployment directory + - name: Prepare deployment directory + run: | + mkdir -p ../deploy + cp -r * ../deploy/ + + # 4. Switch to the 'gh-pages' branch and pull latest changes + - name: Switch to gh-pages and pull latest changes + run: | + git checkout gh-pages || git checkout --orphan gh-pages + git pull origin gh-pages || true + git rm -rf . || true + git clean -fxd + + # 5. Copy deployment files to the 'gh-pages' branch + - name: Copy files to gh-pages + run: | + cp -r ../deploy/* . + rm -rf ../deploy + + # 6. Commit and push changes to GitHub Pages + - name: Commit and push changes + run: | + git add . + git commit -m "Deploy updates from $(date)" || echo "No changes to commit" + git pull origin gh-pages || true + git push origin gh-pages || true