|
| 1 | +name: Deploy Static Site |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: ['v*'] |
| 7 | + delete: |
| 8 | + |
| 9 | +jobs: |
| 10 | + deploy: |
| 11 | + if: github.event_name == 'push' |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + fetch-depth: 0 |
| 18 | + |
| 19 | + - name: Read deploy version from deploy.json |
| 20 | + id: version |
| 21 | + run: | |
| 22 | + DEPLOY_VERSION=$(jq -r '.deploy' deploy.json) |
| 23 | + echo "Deploying version: $DEPLOY_VERSION" |
| 24 | + echo "version=$DEPLOY_VERSION" >> $GITHUB_OUTPUT |
| 25 | +
|
| 26 | + - name: Copy selected version to deploy/ |
| 27 | + run: | |
| 28 | + REPO="${GITHUB_REPOSITORY##*/}" |
| 29 | + mkdir -p deploy |
| 30 | + DEPLOY_VERSION="${{ steps.version.outputs.version }}" |
| 31 | +
|
| 32 | + if [ "$DEPLOY_VERSION" = "latest" ]; then |
| 33 | + echo "Deploying root (latest) version" |
| 34 | + rsync -av \ |
| 35 | + --exclude=versions \ |
| 36 | + --exclude=.github \ |
| 37 | + --exclude=.git \ |
| 38 | + --exclude=.gitignore \ |
| 39 | + --exclude=README.md \ |
| 40 | + --exclude=deploy.json \ |
| 41 | + ./ ./deploy/ |
| 42 | + else |
| 43 | + VERSION_FOLDER="versions/${REPO}_${DEPLOY_VERSION}" |
| 44 | + if [ -d "$VERSION_FOLDER" ]; then |
| 45 | + echo "Deploying from $VERSION_FOLDER" |
| 46 | + rsync -av "$VERSION_FOLDER/" ./deploy/ |
| 47 | + else |
| 48 | + echo "⚠️ Version folder $VERSION_FOLDER does not exist, falling back to latest" |
| 49 | + rsync -av \ |
| 50 | + --exclude=versions \ |
| 51 | + --exclude=.github \ |
| 52 | + --exclude=.git \ |
| 53 | + --exclude=.gitignore \ |
| 54 | + --exclude=README.md \ |
| 55 | + --exclude=deploy.json \ |
| 56 | + ./ ./deploy/ |
| 57 | + fi |
| 58 | + fi |
| 59 | +
|
| 60 | + - name: Deploy to GitHub Pages |
| 61 | + uses: peaceiris/actions-gh-pages@v4 |
| 62 | + with: |
| 63 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + publish_branch: gh-pages |
| 65 | + publish_dir: ./deploy |
| 66 | + |
| 67 | + add_tag: |
| 68 | + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') |
| 69 | + runs-on: ubuntu-latest |
| 70 | + steps: |
| 71 | + - name: Checkout code |
| 72 | + uses: actions/checkout@v4 |
| 73 | + with: |
| 74 | + fetch-depth: 0 |
| 75 | + - name: Copy to versions/ on tag push |
| 76 | + run: | |
| 77 | + REPO="${GITHUB_REPOSITORY##*/}" |
| 78 | + TAG_NAME="${GITHUB_REF##refs/tags/}" |
| 79 | + mkdir -p "versions/${REPO}_${TAG_NAME}" |
| 80 | + rsync -av \ |
| 81 | + --exclude=versions \ |
| 82 | + --exclude=.github \ |
| 83 | + --exclude=.git \ |
| 84 | + --exclude=.gitignore \ |
| 85 | + --exclude=README.md \ |
| 86 | + --exclude=deploy.json \ |
| 87 | + ./ "versions/${REPO}_${TAG_NAME}/" |
| 88 | +
|
| 89 | +
|
| 90 | + - name: Commit version folder |
| 91 | + run: | |
| 92 | + REPO="${GITHUB_REPOSITORY##*/}" |
| 93 | + TAG_NAME="${GITHUB_REF##refs/tags/}" |
| 94 | + git config user.name github-actions |
| 95 | + git config user.email github-actions@github.com |
| 96 | + git add "versions/${REPO}_${TAG_NAME}" |
| 97 | +
|
| 98 | + if git diff --cached --quiet; then |
| 99 | + echo "Nothing to commit" |
| 100 | + else |
| 101 | + git commit -m "Add ${REPO} version ${TAG_NAME}" |
| 102 | + git push origin HEAD:main |
| 103 | + fi |
| 104 | +
|
| 105 | + cleanup: |
| 106 | + if: github.event_name == 'delete' && github.event.ref_type == 'tag' && startsWith(github.event.ref, 'v') |
| 107 | + runs-on: ubuntu-latest |
| 108 | + steps: |
| 109 | + - name: Delete event info |
| 110 | + run: | |
| 111 | + echo "Event: $GITHUB_EVENT_NAME" |
| 112 | + echo "Ref: $GITHUB_REF" |
| 113 | + echo "Event ref: ${{ github.event.ref }}" |
| 114 | + - name: Checkout code |
| 115 | + uses: actions/checkout@v4 |
| 116 | + with: |
| 117 | + ref: main |
| 118 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 119 | + - name: Delete version folder |
| 120 | + run: | |
| 121 | + REPO="${GITHUB_REPOSITORY##*/}" |
| 122 | + TAG_NAME="${{ github.event.ref }}" |
| 123 | + FOLDER="versions/${REPO}_${TAG_NAME}" |
| 124 | + if [ -d "$FOLDER" ]; then |
| 125 | + git config user.name github-actions |
| 126 | + git config user.email github-actions@github.com |
| 127 | + git rm -r "$FOLDER" |
| 128 | + if git diff --cached --quiet; then |
| 129 | + echo "Nothing to commit" |
| 130 | + else |
| 131 | + git commit -m "Delete version folder for deleted tag ${TAG_NAME}" |
| 132 | + git push origin main |
| 133 | + fi |
| 134 | + else |
| 135 | + echo "Folder $FOLDER does not exist. Skipping." |
| 136 | + fi |
0 commit comments