From d1881fc5d40dbf4fd1607ba896f7ea1db7142529 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 17:38:23 -0500 Subject: [PATCH 1/8] Create preview.yml --- .github/workflows/preview.yml | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 .github/workflows/preview.yml diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 00000000..76807625 --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,68 @@ +name: Jekyll Site Preview +on: + pull_request: + types: [opened, synchronize] + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + pages: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.2' + bundler-cache: true + + - name: Install dependencies + run: | + bundle install + + - name: Build Jekyll site + run: | + bundle exec jekyll build + env: + JEKYLL_ENV: production + + - name: Deploy preview + run: | + # Create a unique branch name for this PR preview + BRANCH_NAME="preview-pr-${{ github.event.pull_request.number }}" + + # Configure git + git config --global user.name "GitHub Actions" + git config --global user.email "actions@github.com" + + # Create and switch to a new branch for the preview + git checkout -b $BRANCH_NAME + + # Move built site to root (GitHub Pages requires this) + mv _site/* ./ + rm -rf _site + + # Commit and push the built site + git add . + git commit -m "Deploy PR preview" + git push origin $BRANCH_NAME --force + + - name: Comment PR + uses: actions/github-script@v7 + with: + script: | + const prNumber = context.issue.number; + const repoName = context.repo.repo; + const ownerName = context.repo.owner; + const previewUrl = `https://${ownerName}.github.io/${repoName}/preview-pr-${prNumber}/`; + + github.rest.issues.createComment({ + owner: ownerName, + repo: repoName, + issue_number: prNumber, + body: `📝 Preview this PR at: ${previewUrl}` + }); From fa39bb2fe126ea86f847172c4fe4255538760a07 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 17:39:41 -0500 Subject: [PATCH 2/8] Update preview.yml --- .github/workflows/preview.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 76807625..1a50eaaf 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -17,11 +17,13 @@ jobs: - name: Setup Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: '3.2' + ruby-version: '2.7' # Changed to 2.7 for better compatibility bundler-cache: true - name: Install dependencies run: | + gem install bundler + bundle config set --local path 'vendor/bundle' bundle install - name: Build Jekyll site @@ -32,21 +34,16 @@ jobs: - name: Deploy preview run: | - # Create a unique branch name for this PR preview BRANCH_NAME="preview-pr-${{ github.event.pull_request.number }}" - # Configure git git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" - # Create and switch to a new branch for the preview git checkout -b $BRANCH_NAME - # Move built site to root (GitHub Pages requires this) mv _site/* ./ rm -rf _site - # Commit and push the built site git add . git commit -m "Deploy PR preview" git push origin $BRANCH_NAME --force From d97de2a75e996b7ee16eac42a112ecb1605f7bfa Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 17:43:04 -0500 Subject: [PATCH 3/8] Update preview.yml --- .github/workflows/preview.yml | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 1a50eaaf..974a3017 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -6,6 +6,7 @@ on: jobs: build-and-deploy: runs-on: ubuntu-latest + container: jekyll/jekyll:latest permissions: contents: write pull-requests: write @@ -14,24 +15,17 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: '2.7' # Changed to 2.7 for better compatibility - bundler-cache: true - - - name: Install dependencies - run: | - gem install bundler - bundle config set --local path 'vendor/bundle' - bundle install - - name: Build Jekyll site run: | - bundle exec jekyll build + jekyll build env: JEKYLL_ENV: production + - name: Setup git + run: | + # Install git (not included in Jekyll image) + apk add --no-cache git + - name: Deploy preview run: | BRANCH_NAME="preview-pr-${{ github.event.pull_request.number }}" From 7c6c20e5ef69b5cc3d7aa2fa4d5cecfc3069ead3 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 17:44:42 -0500 Subject: [PATCH 4/8] Update preview.yml --- .github/workflows/preview.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 974a3017..ecf1c5d4 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -6,7 +6,9 @@ on: jobs: build-and-deploy: runs-on: ubuntu-latest - container: jekyll/jekyll:latest + container: + image: jekyll/jekyll:3.8 + options: --user root permissions: contents: write pull-requests: write @@ -15,16 +17,22 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 + - name: Set up environment + run: | + gem install bundler:2.1.4 + bundle config --global silence_root_warning true + - name: Build Jekyll site run: | - jekyll build + bundle install + bundle exec jekyll build env: JEKYLL_ENV: production - name: Setup git run: | - # Install git (not included in Jekyll image) - apk add --no-cache git + apt-get update + apt-get install -y git - name: Deploy preview run: | From ee915d1af18efd86860d49875ccef6accc8854bd Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 17:46:12 -0500 Subject: [PATCH 5/8] Update preview.yml --- .github/workflows/preview.yml | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index ecf1c5d4..a57e62bc 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -5,10 +5,7 @@ on: jobs: build-and-deploy: - runs-on: ubuntu-latest - container: - image: jekyll/jekyll:3.8 - options: --user root + runs-on: ubuntu-20.04 # Using older Ubuntu for better compatibility permissions: contents: write pull-requests: write @@ -17,23 +14,22 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up environment + - name: Setup Jekyll run: | - gem install bundler:2.1.4 - bundle config --global silence_root_warning true + sudo apt-get update + sudo apt-get install -y ruby-full build-essential zlib1g-dev + sudo gem install bundler:2.1.4 + + # Install jekyll and dependencies + bundle config set --local path 'vendor/bundle' + bundle install - name: Build Jekyll site run: | - bundle install bundle exec jekyll build env: JEKYLL_ENV: production - - name: Setup git - run: | - apt-get update - apt-get install -y git - - name: Deploy preview run: | BRANCH_NAME="preview-pr-${{ github.event.pull_request.number }}" From c33ddbd68bea6bc5bfc9075ea18a2a02da4b4d4d Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 18:02:34 -0500 Subject: [PATCH 6/8] Update preview.yml --- .github/workflows/preview.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index a57e62bc..7c9c0c9a 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -5,7 +5,7 @@ on: jobs: build-and-deploy: - runs-on: ubuntu-20.04 # Using older Ubuntu for better compatibility + runs-on: ubuntu-20.04 permissions: contents: write pull-requests: write @@ -39,7 +39,11 @@ jobs: git checkout -b $BRANCH_NAME - mv _site/* ./ + # Remove existing content except _site and .git + find . -mindepth 1 -maxdepth 1 ! -name '_site' ! -name '.git' -exec rm -rf {} + + + # Move _site contents to root + cp -r _site/* ./ rm -rf _site git add . From d786e1d52e58c21110f0df45a417bd8ffe752235 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Mon, 25 Nov 2024 18:31:34 -0500 Subject: [PATCH 7/8] Update preview.yml --- .github/workflows/preview.yml | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 7c9c0c9a..4d54c14b 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -32,23 +32,35 @@ jobs: - name: Deploy preview run: | - BRANCH_NAME="preview-pr-${{ github.event.pull_request.number }}" + # Create a temporary directory for the preview + PR_DIR="pr-${{ github.event.pull_request.number }}" + # Configure git git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" - git checkout -b $BRANCH_NAME + # Fetch gh-pages branch if it exists, or create it + if git fetch origin gh-pages; then + git checkout gh-pages + else + git checkout --orphan gh-pages + git rm -rf . + git commit --allow-empty -m "Initial gh-pages branch" + fi - # Remove existing content except _site and .git - find . -mindepth 1 -maxdepth 1 ! -name '_site' ! -name '.git' -exec rm -rf {} + + # Remove previous preview if it exists + if [ -d "$PR_DIR" ]; then + rm -rf "$PR_DIR" + fi - # Move _site contents to root - cp -r _site/* ./ - rm -rf _site + # Move built site to PR-specific directory + mkdir -p "$PR_DIR" + cp -r _site/* "$PR_DIR/" + # Commit and push git add . - git commit -m "Deploy PR preview" - git push origin $BRANCH_NAME --force + git commit -m "Deploy preview for PR #${{ github.event.pull_request.number }}" + git push origin gh-pages - name: Comment PR uses: actions/github-script@v7 @@ -57,11 +69,11 @@ jobs: const prNumber = context.issue.number; const repoName = context.repo.repo; const ownerName = context.repo.owner; - const previewUrl = `https://${ownerName}.github.io/${repoName}/preview-pr-${prNumber}/`; + const previewUrl = `https://${ownerName}.github.io/${repoName}/pr-${prNumber}/`; github.rest.issues.createComment({ owner: ownerName, repo: repoName, issue_number: prNumber, - body: `📝 Preview this PR at: ${previewUrl}` + body: `📝 Preview this PR at: ${previewUrl}\n\nNote: It may take a few minutes for GitHub Pages to update.` }); From da5d14420fbec2407fbaef0f5c98ab83ababd0d0 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Tue, 3 Dec 2024 08:38:15 -0500 Subject: [PATCH 8/8] Update preview.yml change to be identical to https://github.com/NeurodataWithoutBorders/nwb_hackathons/blob/main/.github/workflows/test-pages-build.yml --- .github/workflows/preview.yml | 89 ++++++++--------------------------- 1 file changed, 19 insertions(+), 70 deletions(-) diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index 4d54c14b..7dffda1f 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -1,79 +1,28 @@ -name: Jekyll Site Preview +name: Test GitHub Pages Builds + on: pull_request: - types: [opened, synchronize] + types: + - opened + - reopened + - synchronize + +concurrency: preview-${{ github.ref }} jobs: - build-and-deploy: - runs-on: ubuntu-20.04 - permissions: - contents: write - pull-requests: write - pages: write + run: + runs-on: ubuntu-latest + strategy: + fail-fast: false steps: - - name: Checkout repository - uses: actions/checkout@v4 + - name: Checkout + uses: actions/checkout@v3 - - name: Setup Jekyll - run: | - sudo apt-get update - sudo apt-get install -y ruby-full build-essential zlib1g-dev - sudo gem install bundler:2.1.4 - - # Install jekyll and dependencies - bundle config set --local path 'vendor/bundle' - bundle install - - - name: Build Jekyll site - run: | - bundle exec jekyll build - env: - JEKYLL_ENV: production + - name: Install and Build + uses: actions/jekyll-build-pages@v1 - name: Deploy preview - run: | - # Create a temporary directory for the preview - PR_DIR="pr-${{ github.event.pull_request.number }}" - - # Configure git - git config --global user.name "GitHub Actions" - git config --global user.email "actions@github.com" - - # Fetch gh-pages branch if it exists, or create it - if git fetch origin gh-pages; then - git checkout gh-pages - else - git checkout --orphan gh-pages - git rm -rf . - git commit --allow-empty -m "Initial gh-pages branch" - fi - - # Remove previous preview if it exists - if [ -d "$PR_DIR" ]; then - rm -rf "$PR_DIR" - fi - - # Move built site to PR-specific directory - mkdir -p "$PR_DIR" - cp -r _site/* "$PR_DIR/" - - # Commit and push - git add . - git commit -m "Deploy preview for PR #${{ github.event.pull_request.number }}" - git push origin gh-pages - - - name: Comment PR - uses: actions/github-script@v7 + uses: rossjrw/pr-preview-action@v1 with: - script: | - const prNumber = context.issue.number; - const repoName = context.repo.repo; - const ownerName = context.repo.owner; - const previewUrl = `https://${ownerName}.github.io/${repoName}/pr-${prNumber}/`; - - github.rest.issues.createComment({ - owner: ownerName, - repo: repoName, - issue_number: prNumber, - body: `📝 Preview this PR at: ${previewUrl}\n\nNote: It may take a few minutes for GitHub Pages to update.` - }); + source-dir: _site/ + preview-branch: gh-pages