July 2024 post - GitHub profile - Fix #79 #697
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
workflow_dispatch: | |
pull_request: | |
branches: | |
- main | |
push: | |
branches: | |
- main | |
name: Update website | |
jobs: | |
build: | |
name: Build site | |
runs-on: ubuntu-latest | |
env: | |
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
submodules: true | |
- name: Install cURL Headers | |
run: | | |
sudo apt-get update | |
sudo apt-get install libcurl4-openssl-dev | |
- name: Get Hugo version | |
id: hugo_vr | |
run: | | |
ver=$(cat .Rprofile | grep hugo.version | cut -d'"' -f2 ) | |
echo ".Rprofile sets hugo version to $ver" | |
echo "hugo_v=${ver}" >> $GITHUB_ENV | |
nalias=$(echo ${{ github.head_ref }} | sed 's/_/-/g') | |
echo "nalias=${nalias}" >> $GITHUB_ENV | |
- name: Setup Hugo | |
uses: peaceiris/actions-hugo@v2 | |
with: | |
hugo-version: ${{ env.hugo_v }} | |
extended: true | |
- name: Build | |
run: | | |
if [[ '${{github.ref}}' == 'refs/heads/main' ]]; then | |
hugo -e production | |
else | |
hugo \ | |
--buildDrafts \ | |
--buildFuture \ | |
-b https://${{ env.nalias }}--drmowinckels.netlify.app/ | |
fi | |
- name: Deploy production 🚀 | |
if: github.ref == 'refs/heads/main' | |
uses: JamesIves/github-pages-deploy-action@v4 | |
with: | |
branch: gh-pages | |
folder: public | |
- name: Deploy to Netlify | |
id: netlify | |
if: github.event_name == 'pull_request' | |
env: | |
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} | |
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} | |
run: | | |
npm install netlify-cli -g | |
netlify deploy --alias ${{ env.nalias }} | |
shell: sh | |
- uses: actions/github-script@v7 | |
if: steps.netlify.outcome == 'success' | |
name: "Notify about build preview" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
await github.rest.issues.createComment({ | |
issue_number: ${{ github.event.pull_request.number }}, | |
owner: 'drmowinckels', | |
repo: 'drmowinckels.github.io', | |
body: ':tada: The preview is built! [Check it out :eyes:](https://${{ env.nalias }}--drmowinckels.netlify.app/)' | |
}) | |
check-post: | |
name: Check post date | |
if: github.ref == 'refs/heads/main' | |
runs-on: ubuntu-latest | |
needs: build | |
outputs: | |
TOOT: ${{ steps.check_date.outputs.TOOT }} | |
POST: ${{ steps.check_post.outputs.POST }} | |
env: | |
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
- name: 📝 Get latest blog post 📝 | |
id: check_post | |
run: | | |
BLOG_PATH="content/blog" | |
# Find the latest blog post | |
latest_post=$(find "${BLOG_PATH}" | grep /index.md$ | grep -v "XX-XX" |sort | tail -n1) | |
echo "POST=${latest_post}" > $GITHUB_OUTPUT | |
# extract date | |
date=$(grep "^date:" "${latest_post}" | sed 's/^date: //' | sed 's/["'\'']//g') | |
echo "POST_DATE=${date}" >> $GITHUB_ENV | |
- name: Check post date | |
id: check_date | |
run: | | |
post_date=$(date -d "${{ env.POST_DATE }}" +%Y%m%d) | |
two_days_ago=$(date -d "-2 days" +%Y%m%d) | |
if (( post_date > two_days_ago )); then | |
echo "Post date is within the last 2 days" | |
echo "TOOT=true" > $GITHUB_OUTPUT | |
else | |
echo "Post date is older than 2 days" | |
echo "TOOT=false" > $GITHUB_OUTPUT | |
fi | |
announce: | |
name: Announce new blog post | |
runs-on: ubuntu-latest | |
needs: check-post | |
if: needs.check-post.outputs.TOOT == 'true' | |
env: | |
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
latest_post: ${{ needs.check-post.outputs.POST }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
- name: Get curl and jq | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y jq curl | |
- name: Get yaml information | |
run: | | |
# Extract the URL from the slug | |
slug=$(grep "^slug:" "${latest_post}" | sed 's/^slug: //' | sed 's/["'\'']//g') | |
year=$(echo ${latest_post} | cut -d"/" -f3) | |
url="https://drmowinckels.io/blog/${year}/${slug}" | |
echo "URL=${url}" >> $GITHUB_ENV | |
# Extract image | |
image=$(grep "^image:" "${latest_post}" | sed 's/^image: //' | sed 's/["'\'']//g') | |
image=$(dirname ${latest_post})/${image} | |
echo "IMAGE=${image}" >> $GITHUB_ENV | |
# Extract the title from yaml | |
title=$(grep "^title:" "${latest_post}" | sed 's/^title: //' | sed 's/["'\'']//g') | |
echo "TITLE=${title}" >> $GITHUB_ENV | |
# Extract summary using yml | |
summary=$(awk '/summary:/ {getline; while ($0 !~ /^$/) {gsub(/^[[:blank:]]+/,"",$0); summary = summary $0 " "; getline}} END {gsub(/[[:blank:]]+$/,"",summary); print summary}' "${latest_post}") | |
# escape backtics | |
summary=$(echo $summary | sed -e 's/`/\\`/g') | |
echo "SUMMARY=${summary}" >> $GITHUB_ENV | |
# Extract tags | |
tags=$(awk '/tags:/ {getline; while ($0 ~ /^ - /) {printf "#%s ", $2; getline}}' ${latest_post}) | |
hastags=$(echo $tags| sed -e 's/#r /#rstats /') | |
echo "TAGS=${hastags}" >> $GITHUB_ENV | |
- name: 🦹 Insert random fun emoji 🦲 | |
run: | | |
emojis=("🦄" "🦜" "🦣" "🦥" "🦦" "🦧" "🦨" "🦩" "🦪" \ | |
"🦫" "🦬" "🦭" "🦮" "🦯" "🦰" "🦱" "🦲" "🦳" "🦴" \ | |
"🦵" "🦶" "🦷" "🦸" "🦹" "🦺" "🦻" "🦼" "🦽" "🦾" \ | |
"🦿" "🧀" "🧁" "🧂" "🧃" "🧄" "🧅" "🧆" "🧇" "🧈" \ | |
"🧉" "🧊" "🧋" "🧌" "🧍" "🧎" "🧏" "🧐" "🧑" "🧒" \ | |
"🧓" "🧔" "🧕" "🧖" "🧗" "🧘" "🧙" "🧚" "🧛" "🧜" \ | |
"🧝" "🧞" "🧟" "🧠" "🧡" "🧢" "🧣" "🧤" "🧥" "🧦" \ | |
"🧧" "🧨" "🧩" "🧪" "🧫" "🧬" "🧭" "🧮" "🧯" "🧰" \ | |
"🧱" "🧲" "🧳" "🧴" "🧵" "🧶" "🧷" "🧸" "🧹" "🧺" \ | |
"🧻" "🧼" "🧽" "🧾" "🧿") | |
random_emoji=${emojis[$RANDOM % ${#emojis[@]}]} | |
echo "EMOJI=${random_emoji}" >> $GITHUB_ENV | |
- name: 🦣 Toot it! 🦣 | |
env: | |
MASTODON_INSTANCE: fosstodon.org | |
MASTO_KEY: ${{ secrets.MASTO_KEY }} | |
run: | | |
# First, upload the media and get the media ID | |
MEDIA_ID=$(curl -X POST \ | |
-H "Authorization: Bearer ${MASTO_KEY}" \ | |
-F "file=@./${{ env.IMAGE }}" \ | |
https://${MASTODON_INSTANCE}/api/v1/media | jq -r '.id') | |
message=$(echo -e "📝 New blog post 📝 | |
\n '${{ env.TITLE }}' \ | |
\n\n ${{ env.EMOJI }} ${{ env.SUMMARY }} \ | |
\n\n 👀 Read more at ${{ env.URL }} \ | |
\n\n ${{ env.TAGS }}") | |
# Then, create a new status with the media attached | |
toot=$(curl -X POST \ | |
-H "Authorization: Bearer ${MASTO_KEY}" \ | |
-F "status=${message}" \ | |
-F "media_ids[]=${MEDIA_ID}" \ | |
https://${MASTODON_INSTANCE}/api/v1/statuses | jq -r '.id') | |
echo $toot | |
# Display clickable url | |
echo "🦣 Tooted! 🦣" | |
echo "https://${MASTODON_INSTANCE}/web/statuses/${toot}" | |