From e0ebd224d3b28e9a53057267d25d966fbdd525a1 Mon Sep 17 00:00:00 2001 From: Glenn 'devalias' Grant Date: Wed, 3 Jul 2024 13:31:23 +1000 Subject: [PATCH] improve build/deploy scripts/instructions (#113) * make bin/build more robust * make bin/deploy more robust * exclude Makefile from build/deploy * consolidate build/deploy/etc instructions into README * update exclude config to remove old files --- Makefile | 30 ------------------------- PUBLISHING.md | 15 ------------- README.md | 53 ++++++++++++++++++++++++++++++++----------- _config.yml | 2 -- bin/build | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++- bin/deploy | 49 +++++++++++++++++++++++++++++++++++++++- 6 files changed, 149 insertions(+), 62 deletions(-) delete mode 100644 Makefile delete mode 100644 PUBLISHING.md diff --git a/Makefile b/Makefile deleted file mode 100644 index 3c09a5216..000000000 --- a/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -.PHONY: list serve serve-drafts build deploy publish outdated - -list: - @echo "Usage (Quick):" - @echo " make build" - @echo " make deploy" - @echo - @echo "Usage (Makefile):" - @grep '^[^#[:space:]].*:' Makefile - @echo - @echo "Usage (./bin):" - @ls ./bin - -serve: - ./bin/serve - -serve-drafts: - ./bin/serve-drafts - -build: - ./bin/build - -deploy: - ./bin/deploy - -publish: - bundle exec jekyll publish "$1" - -outdated: - bundle outdated diff --git a/PUBLISHING.md b/PUBLISHING.md deleted file mode 100644 index 33443cfb8..000000000 --- a/PUBLISHING.md +++ /dev/null @@ -1,15 +0,0 @@ -# Publishing - -Some useful commands to remember: - -## Posts - -* `jekyll draft "Name of Post"` -* `jekyll serve --incremental --drafts` -* `jekyll publish ./_drafts/post-name.md` -* `jekyll unpublish ./_posts/post-name.md` - -## Build & Deploy - -* `bin/build` -* `bin/deploy` diff --git a/README.md b/README.md index 6f428d931..4535faa67 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,43 @@ This is the [Jekyll](https://jekyllrb.com/) source for http://www.devalias.net/ Feel free to create an issue/pull request for any typo's/bugs you might find :) <3 -## Cheatsheet - -* Link to other posts - * `[other post]({% post_url 2017-01-01-other-post %})` -* Embed an image with [jekyll-postfiles](https://github.com/nhoizey/jekyll-postfiles#how-does-it-work) - * create a folder under `_posts` named the same as your post's markdown file - * add any images for your post there, along with the post's markdown file - * use a relative markdown image tag to reference and embed the images - * eg. `![a title or something](foo.jpg)` -* Embed a gist with [jekyll-gist](https://github.com/jekyll/jekyll-gist) - * `{% gist foo/12345678901234567890 %} ` -* Embed a tweet with [jekyll-twitter-plugin](https://github.com/rob-murray/jekyll-twitter-plugin) - * `{% twitter https://twitter.com/rubygems/status/518821243320287232 %}` + +- [Quick Start](#quick-start) +- [Managing Posts and Drafts](#managing-posts-and-drafts) + - [Drafts](#drafts) + - [Tips and Tricks](#tips-and-tricks) +- [Additional Commands](#additional-commands) + + +## Quick Start + +To get started quickly, use the following commands: + +- Serve the site locally: `./bin/serve` or `./bin/serve-drafts` (includes drafts) +- Build the site: `./bin/build` +- Deploy the site: `./bin/deploy` + +## Managing Posts and Drafts + +### Drafts + +- Create a new draft: `jekyll draft "Name of Post"` +- Serve drafts locally: `./bin/serve-drafts` or `jekyll serve --incremental --drafts` +- Promote a draft to a published post: `./bin/publish ./_drafts/post-name.md` or `jekyll publish ./_drafts/post-name.md` + - This will move the specified draft post to the `_posts` directory, making it a published post (though it won't be built or deployed automatically). +- Unpublish a post: `jekyll unpublish ./_posts/post-name.md` + - This will move the specified post from the `_posts` directory back to the `_drafts` directory, making it a draft again. + +### Tips and Tricks + +- Link to other posts: `[other post]({% post_url 2017-01-01-other-post %})` +- Embed an image with [jekyll-postfiles](https://github.com/nhoizey/jekyll-postfiles#how-does-it-work) + - Create a folder under `_posts` named the same as your post's markdown file + - Add images to that folder and use a relative markdown image tag: `![a title or something](foo.jpg)` +- Embed a gist with [jekyll-gist](https://github.com/jekyll/jekyll-gist): `{% gist foo/12345678901234567890 %}` +- Embed a tweet with [jekyll-twitter-plugin](https://github.com/rob-murray/jekyll-twitter-plugin): `{% twitter https://twitter.com/rubygems/status/518821243320287232 %}` + +## Additional Commands + +- Check outdated dependencies: `./bin/outdated` + - This will list all outdated dependencies for your project. diff --git a/_config.yml b/_config.yml index 439ed11af..dd62d9d62 100644 --- a/_config.yml +++ b/_config.yml @@ -91,10 +91,8 @@ keep_files: - .nojekyll # To prevent GitHub re-processing exclude: - - "PUBLISHING.md" - "README.md" - "TODO.txt" - - "ojekyll.txt" - "bin/" # TODO: Enable these checks + fix any errors associated with them diff --git a/bin/build b/bin/build index c0faaa732..476ef2018 100755 --- a/bin/build +++ b/bin/build @@ -1,3 +1,63 @@ -#!/bin/sh +#!/usr/bin/env zsh +set -e + +# Variables +SITE_DIR="_site" +REPO_URL="https://github.com/0xdevalias/devalias.net" +BRANCH="gh-pages" + +# Function to prompt the user for yes/no input with a default response +prompt_yes_no() { + local prompt=$1 + local default=${2:-Y} + + local yn + while true; do + if [[ $default == [Yy]* ]]; then + echo -n "$prompt [Y/n]: " + read yn + yn=${yn:-Y} + else + echo -n "$prompt [y/N]: " + read yn + yn=${yn:-N} + fi + + case $yn in + [Yy]* ) return 0;; + [Nn]* ) return 1;; + * ) echo "Please answer yes or no.";; + esac + done +} + +# Function to check if the site directory is a valid git repository with the correct branch +check_site_setup() { + if [ -d "$SITE_DIR" ] && [ -d "$SITE_DIR/.git" ]; then + if (cd "$SITE_DIR" && git rev-parse --verify "$BRANCH" > /dev/null 2>&1); then + return 0 + fi + fi + return 1 +} + +# Function to clone the branch into the site directory +setup_site_clone() { + rm -rf "$SITE_DIR" + git clone -b "$BRANCH" "$REPO_URL" "$SITE_DIR" +} + +# Main script logic +if ! check_site_setup; then + echo "WARNING: $SITE_DIR directory does not exist or is not a proper git repository." + if prompt_yes_no "Would you like to set up the $SITE_DIR directory as a clone of the $BRANCH branch?" Y; then + setup_site_clone + else + echo "Error: $SITE_DIR directory is not set up. Aborting build." + exit 1 + fi +fi + +# Build the Jekyll site JEKYLL_ENV=production bundle exec jekyll build --lsi --profile $@ diff --git a/bin/deploy b/bin/deploy index b563996f9..4e693d853 100755 --- a/bin/deploy +++ b/bin/deploy @@ -1,3 +1,50 @@ -#!/bin/sh +#!/usr/bin/env zsh +set -e + +# Variables +SITE_DIR="_site" +REPO_URL="https://github.com/0xdevalias/devalias.net" +BRANCH="gh-pages" + +# Function to check if the site directory is a valid git repository with the correct branch +check_site_setup() { + if [ -d "$SITE_DIR" ] && [ -d "$SITE_DIR/.git" ]; then + if git -C "$SITE_DIR" rev-parse --verify "$BRANCH" > /dev/null 2>&1; then + return 0 + fi + fi + return 1 +} + +# Function to check for changes in the site directory +check_for_changes() { + if git -C "$SITE_DIR" diff --quiet && git -C "$SITE_DIR" diff --cached --quiet; then + return 1 + fi + return 0 +} + +# Main script logic +if ! check_site_setup; then + echo "ERROR: $SITE_DIR directory does not exist or is not a proper git repository." + echo + echo "Please set up the $SITE_DIR directory as a clone of the $BRANCH branch." + echo + echo "You can do this automatically by running:" + echo " bin/build" + echo + echo "Or manually with the following commands:" + echo " rm -rf $SITE_DIR" + echo " git clone -b $BRANCH $REPO_URL $SITE_DIR" + exit 1 +fi + +# Check for changes before deployment +if ! check_for_changes; then + echo "No changes to deploy." + exit 0 +fi + +# Deploy the Jekyll site bundle exec jekyll deploy $@