Skip to content

Bump actions/deploy-pages from 3 to 4 #289

Bump actions/deploy-pages from 3 to 4

Bump actions/deploy-pages from 3 to 4 #289

Workflow file for this run

name: Build, Deploy, and Index site
on:
push:
branches:
- 'staging'
- 'main'
pull_request:
branches:
- 'staging'
- 'main'
concurrency:
group: ${{ format('{0}-{1}', github.job, github.ref) }}
cancel-in-progress: true
jobs:
build:
name: Build the site
runs-on: ubuntu-latest
steps:
- name: Token configuration (PAT available)
if: github.ref_name == 'main' || github.ref_name == 'staging'
run: |
echo "PAT=${{ secrets.IMAGE_BOT_PAT }}" >> $GITHUB_ENV
- name: Token configuration (PAT unavailable)
if: github.ref_name != 'main' && github.ref_name != 'staging'
run: |
echo "PAT=${{ github.token }}" >> $GITHUB_ENV
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ env.PAT }}
- name: Staging site configuration
if: github.ref_name != 'main'
run: |
echo "XARGS=--drafts" >> $GITHUB_ENV
echo 'User-agent: * Disallow: /' > ${{ github.workspace }}/robots.txt
sed -i 's_blog.sebsscholarship.org_test.blog.sebsscholarship.org_g' ${{ github.workspace }}/_config.yml
- name: Load Gem Cache
uses: actions/cache/restore@v3
id: gem-cache
with:
key: gem-cache-${{ hashFiles('Gemfile.lock') }}
path: _vendor/bundle
restore-keys: gem-cache-
- name: Build site
run: |
docker run -v ${{ github.workspace }}:/srv/jekyll -e JEKYLL_UID=1001 -e JEKYLL_GID=1001 \
jekyll/builder:4.2.2 /bin/bash -c \
"apk add --no-cache imagemagick wget \
&& wget 'https://fonts.google.com/download?family=Montserrat' -O font.zip \
&& unzip font.zip -d /usr/share/fonts/googlefonts/ \
&& rm font.zip \
&& bundle config --local path '_vendor/bundle' \
&& bundle install \
&& chmod 777 /srv/jekyll \
&& bundle exec jekyll build --future ${{ env.XARGS }}"
- name: Save Gems to Cache
uses: actions/cache/save@v3
if: steps.gem-cache.outputs.cache-hit != 'true'
with:
key: ${{ steps.gem-cache.outputs.cache-primary-key }}
path: _vendor/bundle
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: Built Site
path: ${{ github.workspace }}/_site/
- name: GitHub Pages Artifact
uses: actions/upload-pages-artifact@v3
if: github.ref_name == 'main'
- name: Commit images
if: github.ref_name == 'main' || github.ref_name == 'staging'
run: |
git config core.fileMode false
git config --global user.name 'SSF Image Bot'
git config --global user.email '[email protected]'
git add assets/images/
((git commit -m "bot: Add newly generated images" && git push) || true)
deploy-staging:
name: Deploy the staging site
runs-on: ubuntu-latest
needs:
- build
if: github.ref_name == 'staging'
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: Built Site
path: _site/
- name: Install SSH key
if: github.ref_name == 'staging'
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Trust SSH key
if: github.ref_name == 'staging'
run: ssh-keyscan sebsscholarship.org > ~/.ssh/known_hosts
- name: Deploy Staging
if: github.ref_name == 'staging'
run: |
scp -r ${{ github.workspace }}/_site/* ${{ secrets.USERNAME }}@sebsscholarship.org:~/staging/
ssh ${{ secrets.USERNAME }}@sebsscholarship.org 'rm -rf ~/test.blog.sebsscholarship.org/* \
&& mv ~/staging/* ~/test.blog.sebsscholarship.org/'
deploy-prod:
name: Deploy the production site
runs-on: ubuntu-latest
needs:
- build
if: github.ref_name == 'main'
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
index:
name: Index the site
runs-on: ubuntu-latest
needs:
- deploy-staging
- deploy-prod
# Only index if deploy completes successfully
if: always() && (needs.deploy-staging.result == 'success' || needs.deploy-prod.result == 'success')
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Staging site configuration
if: github.ref_name != 'main'
run: |
echo "XARGS=--drafts" >> $GITHUB_ENV
echo 'User-agent: * Disallow: /' > ${{ github.workspace }}/robots.txt
sed -i 's_blog.sebsscholarship.org_test.blog.sebsscholarship.org_g' ${{ github.workspace }}/_config.yml
- name: Load Gem Cache
uses: actions/cache/restore@v3
id: gem-cache
with:
key: gem-cache-${{ hashFiles('Gemfile.lock') }}
path: _vendor/bundle
restore-keys: gem-cache-
- name: Index site
run: |
docker run -v ${{ github.workspace }}:/srv/jekyll -e JEKYLL_UID=1001 -e JEKYLL_GID=1001 \
jekyll/builder:4.2.2 /bin/bash -c \
"bundle config --local path '_vendor/bundle' \
&& bundle install \
&& chmod 777 /srv/jekyll \
&& ALGOLIA_API_KEY='${{ secrets.ALGOLIA_KEY }}' bundle exec jekyll algolia --future ${{ env.XARGS }}"
notify:
name: Send job complete notification
runs-on: ubuntu-latest
needs:
- build
- deploy-staging
- deploy-prod
- index
# Run if on staging or main
if: always() && (github.ref_name == 'staging' || github.ref_name == 'main')
steps:
- name: Set test environment
if: github.ref_name == 'staging'
run: |
echo "SITE_NAME=test" >> $GITHUB_ENV
echo "SITE_ADDR=https://test.blog.sebsscholarship.org" >> $GITHUB_ENV
- name: Set main environment
if: github.ref_name == 'main'
run: |
echo "SITE_NAME=main" >> $GITHUB_ENV
echo "SITE_ADDR=https://blog.sebsscholarship.org" >> $GITHUB_ENV
- name: Notify on success
# If the site was successfully built and indexed and deployed if staging
if: needs.build.result == 'success' && (needs.deploy-staging.result == 'success' || needs.deploy-prod.result == 'success') && needs.index.result == 'success'
uses: appleboy/[email protected]
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#800000"
username: "SSF Blog Status Bot"
message: >
Updates were successfully pushed to ${{ env.SITE_NAME }} blog:
${{ env.SITE_ADDR }}
- name: Notify on failure
# If the site failed at any point
if: needs.build.result == 'failure' || needs.deploy-staging.result == 'failure' || needs.deploy-prod.result == 'failure' || needs.index.result == 'failure'
uses: appleboy/[email protected]
with:
webhook_id: ${{ secrets.DISCORD_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
color: "#800000"
username: "SSF Blog Status Bot"
message: >
Aborting ${{ env.SITE_NAME }} blog deployment due to unexpected error:
https://github.com/sebs-scholarship/Blog/actions/runs/${{ github.run_id }}