diff --git a/.circleci/config.yml b/.circleci/config.yml index 12c647e..b812c35 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,5 +1,10 @@ version: 2.1 +setup: true + +orbs: + path-filtering: circleci/path-filtering@0.1.3 + executors: build-environment: docker: @@ -22,7 +27,10 @@ jobs: - run: name: Verify Environment - command: sh ./scripts/ci/verify.sh build + command: | + apk update + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/ci/verify.sh build verify-deploy-environment: executor: deploy-environment @@ -32,7 +40,10 @@ jobs: - run: name: Verify Environment - command: sh ./scripts/ci/verify.sh deploy + command: | + apk update + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/ci/verify.sh deploy verify-publish-environment: executor: publish-environment @@ -42,7 +53,10 @@ jobs: - run: name: Verify Environment - command: sh ./scripts/ci/verify.sh publish + command: | + apk update + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/ci/verify.sh publish build: executor: build-environment @@ -55,7 +69,8 @@ jobs: command: | apk update - sh ./scripts/install/make.sh + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/install/make.sh make install-git env=$BLOG_BUILD_ENV make install-hugo @@ -92,7 +107,8 @@ jobs: command: | apk update - sh ./scripts/install/make.sh + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/install/make.sh make install-npm make install-netlify-cli @@ -114,7 +130,8 @@ jobs: name: Install Packages command: | apk update - sh ./scripts/install/make.sh + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/install/make.sh make install-docker-cli - run: @@ -122,20 +139,33 @@ jobs: command: make publish-ci tag=$CIRCLE_TAG username=$DOCKER_USERNAME token=$DOCKER_ACCESS_TOKEN workflows: - verify: + main: jobs: - verify-build-environment - - verify-deploy-environment - - verify-publish-environment - build: - jobs: - - build + - verify-deploy-environment: + filters: + branches: + only: + - main + + - verify-publish-environment: + filters: + branches: + only: + - main - deploy: - jobs: - build - + + - path-filtering/filter: + name: watchtower + + mapping: | + tests/.* run-tests-jobs true + + base-revision: main + config-path: .circleci/watchtower.yml + - deploy: requires: - build @@ -144,8 +174,7 @@ workflows: branches: only: - main - publish: - jobs: + - publish: filters: tags: diff --git a/.circleci/watchtower.yml b/.circleci/watchtower.yml new file mode 100644 index 0000000..38e5bab --- /dev/null +++ b/.circleci/watchtower.yml @@ -0,0 +1,72 @@ +version: 2.1 + +parameters: + run-tests-jobs: + type: boolean + default: false + +executors: + tests-node-environment: + docker: + - image: node:18.7.0-alpine3.16 + + tests-bats-environment: + docker: + - image: bats/bats:1.7.0 + +jobs: + tests-bats: + executor: tests-bats-environment + + steps: + - checkout + + - run: + name: Install Packages + + command: | + apk update + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/install/make.sh + make install-git env=$BLOG_BUILD_ENV + + - run: + name: Add Git Submodules + command: make git-submodules env=$BLOG_BUILD_ENV + + - run: + name: Run Tests + command: bats ./tests/_.bats ./tests/install.bats ./tests/shared.bats + + tests-node: + executor: tests-node-environment + + steps: + - checkout + + - run: + name: Install Packages + + command: | + apk update + sh scripts/install/bash.sh $BLOG_BUILD_ENV + bash scripts/install/make.sh + make install-git env=$BLOG_BUILD_ENV + npm install -g bats + + - run: + name: Add Git Submodules + command: make git-submodules env=$BLOG_BUILD_ENV + + - run: + name: Run Node Tests + command: bats tests/_.bats tests/install-node.bats + + +workflows: + test: + when: << pipeline.parameters.run-tests-jobs >> + + jobs: + - tests-bats + - tests-node diff --git a/.docker/bats.alpine.Dockerfile b/.docker/bats.alpine.Dockerfile new file mode 100644 index 0000000..8337475 --- /dev/null +++ b/.docker/bats.alpine.Dockerfile @@ -0,0 +1,5 @@ +FROM bats/bats:1.7.0 + +LABEL maintainer="Airscript " + +COPY . . diff --git a/.docker/bats.node.Dockerfile b/.docker/bats.node.Dockerfile new file mode 100644 index 0000000..acc7087 --- /dev/null +++ b/.docker/bats.node.Dockerfile @@ -0,0 +1,17 @@ +FROM node:18.7.0-alpine3.16 + +LABEL maintainer="Airscript " + +ARG APP_DIR=app + +RUN \ + apk update && \ + apk add bash && \ + npm install -g bats \ + mkdir ${APP_DIR} + +WORKDIR /${APP_DIR} + +COPY . . + +ENTRYPOINT ["bash", "bats"] \ No newline at end of file diff --git a/.docker/blog.Dockerfile b/.docker/blog.Dockerfile index 6b5a1d5..eba5d47 100644 --- a/.docker/blog.Dockerfile +++ b/.docker/blog.Dockerfile @@ -11,6 +11,7 @@ ARG BLOG_BUILD_ENV=docker RUN \ apk update && \ apk add hugo && \ +apk add bash && \ mkdir ${BLOG_DIR} WORKDIR /${BLOG_DIR} @@ -25,4 +26,4 @@ ENTRYPOINT ["./scripts/docker-entrypoint.sh"] CMD ["hugo"] -EXPOSE ${BLOG_PORT} \ No newline at end of file +EXPOSE ${BLOG_PORT} diff --git a/.gitmodules b/.gitmodules index 89af1b0..998dc03 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,15 @@ [submodule "themes/PaperMod"] path = themes/PaperMod url = https://github.com/adityatelange/hugo-PaperMod.git + +[submodule "tests/bats"] + path = tests/bats + url = https://github.com/bats-core/bats-core.git + +[submodule "tests/libraries/bats-support"] + path = tests/libraries/bats-support + url = https://github.com/bats-core/bats-support.git + +[submodule "tests/libraries/bats-assert"] + path = tests/libraries/bats-assert + url = https://github.com/bats-core/bats-assert.git diff --git a/Makefile b/Makefile index 756a4c2..68cbd7a 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ all: build run clean: docker compose down cd ./themes/PaperMod && find . -name "*" -delete + cd ./tests/bats && find . -name "*" -delete + cd ./tests/libraries/bats-assert && find . -name "*" -delete + cd ./tests/libraries/bats-support && find . -name "*" -delete .PHONY: build build: git-submodules @@ -19,6 +22,38 @@ build: git-submodules run: docker compose up -d +.PHONY: all-test +all-test: build-test run-test + +.PHONY: clean-test +clean-test: + docker rm worker-bats-node; \ + docker rm worker-bats-alpine; \ + docker rmi airscript/bats:node; \ + docker rmi airscript/bats:alpine + +.PHONY: clean-bats +clean-bats: + docker rmi bats/bats:1.7.0 + docker rmi node:18.7.0-alpine3.16 + +.PHONY: build-test +build-test: + docker build -f .docker/bats.alpine.Dockerfile -t airscript/bats:alpine . + docker build -f .docker/bats.node.Dockerfile -t airscript/bats:node . + +.PHONY: run-test +run-test: + docker run -it --rm --name="worker-bats-alpine" airscript/bats:alpine \ + ./tests/_.bats ./tests/install.bats ./tests/shared.bats + + docker run -it --rm --name="worker-bats-node" airscript/bats:node \ + ./tests/_.bats ./tests/install-node.bats + +.PHONY: install-bash +install-bash: + sh ./scripts/install/bash.sh + .PHONY: install-npm install-npm: sh ./scripts/install/npm.sh diff --git a/init.sh b/init.sh deleted file mode 100644 index 2a2da9f..0000000 --- a/init.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -sh ./scripts/submodules.sh "local" \ No newline at end of file diff --git a/scripts/bats.sh b/scripts/bats.sh new file mode 100755 index 0000000..92f5ac5 --- /dev/null +++ b/scripts/bats.sh @@ -0,0 +1,2 @@ +#!/bin/bash +echo "Bats is working correctly." diff --git a/scripts/ci/build.sh b/scripts/ci/build.sh old mode 100644 new mode 100755 index 86dde72..3475e88 --- a/scripts/ci/build.sh +++ b/scripts/ci/build.sh @@ -1,2 +1,2 @@ -#!/bin/sh +#!/bin/bash hugo --gc --minify diff --git a/scripts/ci/deploy.sh b/scripts/ci/deploy.sh old mode 100644 new mode 100755 index 9e5b17c..d8eff16 --- a/scripts/ci/deploy.sh +++ b/scripts/ci/deploy.sh @@ -1,2 +1,2 @@ -#!/bin/sh +#!/bin/bash netlify deploy --site $1 --auth $2 --prod --dir=public diff --git a/scripts/ci/verify.sh b/scripts/ci/verify.sh old mode 100644 new mode 100755 index ccdcad8..47d2a79 --- a/scripts/ci/verify.sh +++ b/scripts/ci/verify.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash if [ "$1" = "build" ]; then apk --version diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh index 32c73f6..ac2a287 100755 --- a/scripts/docker-entrypoint.sh +++ b/scripts/docker-entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e if [ "$1" = 'hugo' ]; then diff --git a/scripts/docker/build.sh b/scripts/docker/build.sh old mode 100644 new mode 100755 index 0c122e6..4b2e088 --- a/scripts/docker/build.sh +++ b/scripts/docker/build.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/bin/bash echo "Building Docker image..." docker build -f .docker/blog.Dockerfile -t airscript/blog:$1 . \ No newline at end of file diff --git a/scripts/docker/login.sh b/scripts/docker/login.sh old mode 100644 new mode 100755 index 3b75764..66e460b --- a/scripts/docker/login.sh +++ b/scripts/docker/login.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/bin/bash echo "Logging into Docker Hub..." docker login --username $1 --password $2 diff --git a/scripts/docker/push.sh b/scripts/docker/push.sh old mode 100644 new mode 100755 index 1985431..ca63886 --- a/scripts/docker/push.sh +++ b/scripts/docker/push.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/bin/bash echo "Pushing image to Docker Hub..." docker push airscript/blog:$1 \ No newline at end of file diff --git a/scripts/install/bash.sh b/scripts/install/bash.sh new file mode 100755 index 0000000..8dddc5f --- /dev/null +++ b/scripts/install/bash.sh @@ -0,0 +1,4 @@ +#!/bin/sh +apk add bash +bash --version +echo "Bash installed successfully." \ No newline at end of file diff --git a/scripts/install/docker-cli.sh b/scripts/install/docker-cli.sh old mode 100644 new mode 100755 index 9ec1f86..7daa701 --- a/scripts/install/docker-cli.sh +++ b/scripts/install/docker-cli.sh @@ -1,2 +1,4 @@ -#!/bin/sh +#!/bin/bash apk add docker-cli +docker --version +echo "Docker CLI installed successfully." \ No newline at end of file diff --git a/scripts/install/git.sh b/scripts/install/git.sh old mode 100644 new mode 100755 index b18e5f6..3b7666e --- a/scripts/install/git.sh +++ b/scripts/install/git.sh @@ -1,4 +1,8 @@ -#!/bin/sh +#!/bin/bash if [ ! "$1" = "compose" ]; then apk add git + echo "Git installed successfully." + +else + echo "Git installation has been skipped." fi diff --git a/scripts/install/hugo.sh b/scripts/install/hugo.sh old mode 100644 new mode 100755 index 92ce713..71d346a --- a/scripts/install/hugo.sh +++ b/scripts/install/hugo.sh @@ -1,2 +1,3 @@ -#!/bin/sh +#!/bin/bash apk add hugo +echo "Hugo installed successfully." diff --git a/scripts/install/make.sh b/scripts/install/make.sh old mode 100644 new mode 100755 index bd3a72d..3e2977c --- a/scripts/install/make.sh +++ b/scripts/install/make.sh @@ -1,2 +1,3 @@ -#!/bin/sh +#!/bin/bash apk add make +echo "Make installed successfully." \ No newline at end of file diff --git a/scripts/install/netlify-cli.sh b/scripts/install/netlify-cli.sh old mode 100644 new mode 100755 index 1acd046..50b7768 --- a/scripts/install/netlify-cli.sh +++ b/scripts/install/netlify-cli.sh @@ -1,2 +1,3 @@ -#!/bin/sh +#!/bin/bash npm install -g netlify-cli +echo "Netlify CLI installed successfully." \ No newline at end of file diff --git a/scripts/install/npm.sh b/scripts/install/npm.sh old mode 100644 new mode 100755 index d0a1eee..a3a5faa --- a/scripts/install/npm.sh +++ b/scripts/install/npm.sh @@ -1,2 +1,3 @@ -#!/bin/sh +#!/bin/bash npm install -g npm +echo "npm installed successfully." diff --git a/scripts/shared/git-submodules.sh b/scripts/shared/git-submodules.sh index 58b2fde..2899e74 100755 --- a/scripts/shared/git-submodules.sh +++ b/scripts/shared/git-submodules.sh @@ -1,4 +1,8 @@ -#!/bin/sh +#!/bin/bash if [ ! "$1" = "compose" ]; then git submodule update --init --recursive + echo "Git submodule update executed." + +else + echo "Git submodule update skipped." fi \ No newline at end of file diff --git a/tests/_.bats b/tests/_.bats new file mode 100644 index 0000000..8e790bb --- /dev/null +++ b/tests/_.bats @@ -0,0 +1,17 @@ +#!./tests/bats/bin/bats + +setup() { + load 'libraries/bats-support/load' + load 'libraries/bats-assert/load' + source ./tests/mocks.sh +} + +@test "Run Script" { + run ./scripts/bats.sh +} + +@test "Install Bash" { + run ./scripts/install/bash.sh + assert_output --partial "$INSTALL_BASH" + refute_output --partial "ERROR" +} \ No newline at end of file diff --git a/tests/bats b/tests/bats new file mode 160000 index 0000000..690a3a9 --- /dev/null +++ b/tests/bats @@ -0,0 +1 @@ +Subproject commit 690a3a9779f59e971e4cf4ee6a106d351a362763 diff --git a/tests/install-node.bats b/tests/install-node.bats new file mode 100644 index 0000000..e95108b --- /dev/null +++ b/tests/install-node.bats @@ -0,0 +1,19 @@ +#!./tests/bats/bin/bats + +setup() { + load 'libraries/bats-support/load' + load 'libraries/bats-assert/load' + source ./tests/mocks.sh +} + +@test "Install npm" { + run ./scripts/install/npm.sh + assert_output --partial "$INSTALL_NPM" + refute_output --partial "ERR!" +} + +@test "Install Netlify CLI" { + run ./scripts/install/netlify-cli.sh + assert_output --partial "$INSTALL_NETLIFY_CLI" + refute_output --partial "ERR!" +} \ No newline at end of file diff --git a/tests/install.bats b/tests/install.bats new file mode 100644 index 0000000..dd8d3d8 --- /dev/null +++ b/tests/install.bats @@ -0,0 +1,39 @@ +#!./tests/bats/bin/bats + +setup() { + load 'libraries/bats-support/load' + load 'libraries/bats-assert/load' + source ./tests/mocks.sh +} + +@test "Install Docker CLI" { + run ./scripts/install/docker-cli.sh + assert_output --partial "$INSTALL_DOCKER_CLI" + refute_output --partial "ERROR" +} + +@test "Install Git" { + run ./scripts/install/git.sh + assert_output --partial "$INSTALL_GIT" + refute_output --partial "ERROR" + refute_output --partial "$SKIP_INSTALL_GIT" +} + +@test "Skip install Git" { + run ./scripts/install/git.sh compose + assert_output --partial "$SKIP_INSTALL_GIT" + refute_output --partial "ERROR" + refute_output --partial "$INSTALL_GIT" +} + +@test "Install Hugo" { + run ./scripts/install/hugo.sh + assert_output --partial "$INSTALL_HUGO" + refute_output --partial "ERROR" +} + +@test "Install Make" { + run ./scripts/install/make.sh + assert_output --partial "$INSTALL_MAKE" + refute_output --partial "ERROR" +} diff --git a/tests/libraries/bats-assert b/tests/libraries/bats-assert new file mode 160000 index 0000000..ffe84ea --- /dev/null +++ b/tests/libraries/bats-assert @@ -0,0 +1 @@ +Subproject commit ffe84ea5dd43b568851549b3e241db150c12929c diff --git a/tests/libraries/bats-support b/tests/libraries/bats-support new file mode 160000 index 0000000..3c8fadc --- /dev/null +++ b/tests/libraries/bats-support @@ -0,0 +1 @@ +Subproject commit 3c8fadc5097c9acfc96d836dced2bb598e48b009 diff --git a/tests/mocks.sh b/tests/mocks.sh new file mode 100755 index 0000000..6095409 --- /dev/null +++ b/tests/mocks.sh @@ -0,0 +1,11 @@ +#!/bin/bash +readonly INSTALL_NPM="npm installed successfully." +readonly INSTALL_GIT="Git installed successfully." +readonly INSTALL_HUGO="Hugo installed successfully." +readonly INSTALL_MAKE="Make installed successfully." +readonly INSTALL_BASH="Bash installed successfully." +readonly SKIP_INSTALL_GIT="Git installation has been skipped." +readonly INSTALL_DOCKER_CLI="Docker CLI installed successfully." +readonly INSTALL_NETLIFY_CLI="Netlify CLI installed successfully." +readonly ADD_GIT_SUBMODULE_SKIPPED="Git submodule update skipped." +readonly ADD_GIT_SUBMODULE_EXECUTED="Git submodule update executed." diff --git a/tests/shared.bats b/tests/shared.bats new file mode 100644 index 0000000..1cd074f --- /dev/null +++ b/tests/shared.bats @@ -0,0 +1,18 @@ +#!./tests/bats/bin/bats + +setup() { + load 'libraries/bats-support/load' + load 'libraries/bats-assert/load' + source ./tests/mocks.sh + apk add git +} + +@test "Add Git Submodules" { + run ./scripts/shared/git-submodules.sh + assert_output "$ADD_GIT_SUBMODULE_EXECUTED" +} + +@test "Skip Git Submodules" { + run ./scripts/shared/git-submodules.sh compose + assert_output "$ADD_GIT_SUBMODULE_SKIPPED" +} \ No newline at end of file