From d0d1e02763ec3ae3bb2a21ace596fbb0a4088cd1 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Thu, 17 Sep 2020 01:28:44 +0800 Subject: [PATCH] Improve the CD workflow and enhance the related tools. --- .github/workflows/pages-deploy.yml.hook | 71 +++++++------------------ tools/build.sh | 37 ++++++------- tools/test.sh | 6 ++- 3 files changed, 39 insertions(+), 75 deletions(-) diff --git a/.github/workflows/pages-deploy.yml.hook b/.github/workflows/pages-deploy.yml.hook index 1733a17..45595d4 100644 --- a/.github/workflows/pages-deploy.yml.hook +++ b/.github/workflows/pages-deploy.yml.hook @@ -9,7 +9,7 @@ on: - 'LICENSE' jobs: - build-n-test: + continuous-delivery: runs-on: ubuntu-latest steps: @@ -17,15 +17,15 @@ jobs: with: ruby-version: '2.6.x' - - name: install tooling - run: | - brew install yq - - name: Checkout uses: actions/checkout@v2 with: fetch-depth: 0 + - name: Install yq + run: | + brew install yq + - name: Bundle Caching id: bundle-cache uses: actions/cache@v1 @@ -49,58 +49,25 @@ jobs: run: | bundle install --local + - name: Check baseurl + run: | + baseurl="$(grep '^baseurl:' _config.yml | yq r - baseurl)" + if [[ -n $baseurl ]]; then + echo "::set-env name=SPEC_TEST::_site_no_baseurl" + fi + - name: Build Site run: | - bash tools/build.sh -b "" + bash tools/build.sh + + if [[ -n $SPEC_TEST ]]; then + # Bypass the defects of htmlproofer + bash tools/build.sh -b "" -d "$SPEC_TEST" + fi - name: Test Site run: | - bash tools/test.sh - - deploy: - needs: build-n-test - runs-on: ubuntu-latest - - steps: - - uses: actions/setup-ruby@v1 - with: - ruby-version: '2.6.x' - - - name: install tooling - run: | - brew install yq - - - name: Checkout - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Bundle Caching - id: bundle-cache - uses: actions/cache@v1 - with: - path: vendor/bundle - key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile') }} - restore-keys: | - ${{ runner.os }}-gems- - - - name: Bundle config - run: | - bundle config path vendor/bundle - - - name: Bundle Install - if: steps.bundle-cache.outputs.cache-hit != 'true' - run: | - bundle install - - - name: Bundle Install locally - if: steps.bundle-cache.outputs.cache-hit == 'true' - run: | - bundle install --local - - - name: Build site - run: | - bash tools/build.sh + bash tools/test.sh "$SPEC_TEST" - name: Deploy run: | diff --git a/tools/build.sh b/tools/build.sh index a05712c..4d1c58a 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -16,28 +16,26 @@ CONTAINER="${WORK_DIR}/.container" DEST="${WORK_DIR}/_site" - _help() { echo "Usage:" echo echo " bash build.sh [options]" echo echo "Options:" - echo " -b, --baseurl The site relative url that start with slash, e.g. '/project'" - echo " -h, --help Print the help information" - echo " -d, --destination Destination directory (defaults to ./_site)" + echo " -b, --baseurl The site relative url that start with slash, e.g. '/project'" + echo " -h, --help Print the help information" + echo " -d, --destination Destination directory (defaults to ./_site)" } - _init() { cd "$WORK_DIR" - if [[ -d "$CONTAINER" ]]; then + if [[ -d $CONTAINER ]]; then rm -rf "$CONTAINER" fi - if [[ -d "_site" ]]; then - jekyll clean + if [[ -d $DEST ]]; then + bundle exec jekyll clean fi local _temp="$(mktemp -d)" @@ -46,7 +44,6 @@ _init() { mv "$_temp" "$CONTAINER" } - _build() { cd "$CONTAINER" echo "$ cd $(pwd)" @@ -60,7 +57,7 @@ _build() { echo -e "\nBuild success, the site files have been placed in '${DEST}'." if [[ -d "${DEST}/.git" ]]; then - if [[ ! -z $(git -C "$DEST" status -s) ]]; then + if [[ -n $(git -C "$DEST" status -s) ]]; then git -C "$DEST" add . git -C "$DEST" commit -m "[Automation] Update site files." -q echo -e "\nPlease push the changes of $DEST to remote master branch.\n" @@ -70,37 +67,33 @@ _build() { cd .. && rm -rf "$CONTAINER" } - _check_unset() { - if [[ -z ${1:+unset} ]] - then + if [[ -z ${1:+unset} ]]; then _help exit 1 fi } - main() { - while [[ $# -gt 0 ]] - do + while [[ $# -gt 0 ]]; do opt="$1" case $opt in - -b|--baseurl) + -b | --baseurl) local _baseurl="$2" - if [[ -z "$_baseurl" ]]; then + if [[ -z $_baseurl ]]; then _baseurl='""' fi CMD+=" -b $_baseurl" shift shift ;; - -d|--destination) + -d | --destination) _check_unset "$2" DEST="$(realpath "$2")" - shift; - shift; + shift + shift ;; - -h|--help) + -h | --help) _help exit 0 ;; diff --git a/tools/test.sh b/tools/test.sh index b556ff2..90dd701 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -4,7 +4,7 @@ # # Requirement: https://github.com/gjtorikian/html-proofer # -# Usage: bash /path/to/test.sh +# Usage: bash /path/to/test.sh [indicated path] # # v2.0 # https://github.com/cotes2020/jekyll-theme-chirpy @@ -14,6 +14,10 @@ DEST=_site URL_IGNORE=cdn.jsdelivr.net +if [[ -n $1 && -d $1 ]]; then + DEST=$1 +fi + bundle exec htmlproofer $DEST \ --disable-external \ --check-html \