diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml index 65c8123..d760eeb 100644 --- a/.github/workflows/build-deploy.yml +++ b/.github/workflows/build-deploy.yml @@ -21,7 +21,7 @@ jobs: runs-on: ubuntu-latest container: - image: ghcr.io/atas/ssg-builder:1.0.0 + image: ghcr.io/atas/ssg-builder:202310131126 steps: - name: Checkout repository diff --git a/Makefile b/Makefile index 480cac2..1acc73d 100644 --- a/Makefile +++ b/Makefile @@ -2,17 +2,22 @@ # You would only need to use these if you want to build your own custom builder image. # Otherwise, by default the workflow uses the pre-built image from ghcr.io/atas/ssg-builder:latest +.PHONY: build-local-image update-workflow-image run-local-image + build-local-image: docker build system/workflow-image -t atas-ssg-builder:latest +run-local-image: + docker run --rm -it -v $(shell pwd):/github/workspace atas-ssg-builder:latest + +# Updates the GHCR:latest image update-workflow-image: docker login ghcr.io docker buildx build --platform linux/amd64 system/workflow-image -t ghcr.io/atas/ssg-builder:latest docker push ghcr.io/atas/ssg-builder:latest - -.PHONY: run-local-image - -run-local-image: - docker run --rm -it -v $(shell pwd):/github/workspace atas-ssg-builder:latest +# Creates a new tag for GHCR:latest image with the current date and time +tag-workflow-image: + docker tag ghcr.io/atas/ssg-builder:latest ghcr.io/atas/ssg-builder:$(shell date +%Y%m%d%H%M) + docker push ghcr.io/atas/ssg-builder:$(shell date +%Y%m%d%H%M) diff --git a/README.md b/README.md index 95d3441..fe63871 100644 --- a/README.md +++ b/README.md @@ -102,3 +102,12 @@ Ensure local hostname matches the `local_hostname` in `config.json` file. Add it necessary with IP `127.0.0.1` Locally, http works fine, https is used in build process with self-signed cert to generate URLs correctly. + + +## Architectural Considerations + +### Caching ./pages and ./posts for performance in local env + +If you have a lot of pages and posts, local development environment will be likely slow. We can introduce caching to +speed it up, but if you have over a hundred pages and posts, you are probably better off with one of the more +advanced static site generators instead of this little tool. \ No newline at end of file diff --git a/system/bin/generate_html.sh b/system/bin/generate_html.sh index 3063ccb..902fb55 100755 --- a/system/bin/generate_html.sh +++ b/system/bin/generate_html.sh @@ -1,25 +1,34 @@ #!/bin/bash -echo "Creating symbolic link /workspace to $(pwd)" -ln -s "$(pwd)" /workspace - -php-fpm -D -service nginx start +# Create a symbolic link to the current directory +# if it doesn't exist +if [[ ! -L "/workspace" ]]; then + echo "Creating symbolic link /workspace to $(pwd)" + ln -s "$(pwd)" /workspace +fi -host=$(jq -r '.hostname' config.json) +# Check if php-fpm is running +if ! pgrep "php-fpm" > /dev/null; then + php-fpm -D +fi -if ! grep -qF "$host" "/etc/hosts"; then - echo "$host" >> "/etc/hosts" +# Check if nginx is running +if ! pgrep "nginx" > /dev/null; then + service nginx start fi -echo 127.0.0.1 "$host" >> /etc/hosts +hostname=$(jq -r '.hostname' config.json) -rm -rf html -mkdir -p html +if ! grep -qF "$hostname" "/etc/hosts"; then + echo "$hostname" >> "/etc/hosts" +fi + +echo 127.0.0.1 "$hostname" >> /etc/hosts +# Curl the URL $1 and save it to $2 function curl_and_save() { - echo Hitting URL "$1" - curl -k "$1" > "$2" + echo "curl_and_save https://$hostname$1 > $2" + curl -ks --fail "https://$hostname$1" > "$2" # Check if the file exists and is not zero bytes if [[ ! -s $2 ]]; then @@ -27,40 +36,62 @@ function curl_and_save() { exit 1 fi - # Check if the file is missing the specific text string - if ! grep -q "" "$2"; then - echo "Error: The file does not contain the attribution HTML comment OR something else is wrong in the output. As the MIT license requirement, please do not remove it to help other fellow developers discover this neat tool." + # Check if file exists + if [[ ! -e "$2" ]]; then + echo "Error: File $2 does not exist." + exit 1 + fi + + # Check if file size is zero + if [[ ! -s "$2" ]]; then + echo "Error: File $2 is empty." exit 1 fi } -curl_and_save "https://$host/" "html/index.html" +#region Building HTML files +rm -rf html +mkdir -p html -# generate html from all md files at pages directory +# pages/*.md HTML BUilding for file in pages/*.md; do filename=$(basename "$file" .md) - curl_and_save "https://$host/$filename" "html/$filename.html" + curl_and_save "/$filename" "html/$filename.html" done -# generate html from all php files at home directory except index.php, page.php and post.php -for file in *.php; do - if [[ "$file" != "index.php" && "$file" != "page.php" && "$file" != "post.php" ]]; then - filename=$(basename "$file" .php) - curl_and_save "https://$host/$filename" "html/$filename.html" - fi -done - -# generate html from all md files at posts directory +# posts/*.md HTML Building mkdir -p html/p for file in posts/*.md; do filename=$(basename "$file" .md) slug=$(awk -F': ' '/^slug:|^[ \t]*"slug":/ {gsub(/["\r]/, "", $2); print $2}' $file) - curl_and_save "https://$host/p/$slug" "html/p/$slug.html" + curl_and_save "/p/$slug" "html/p/$slug.html" +done + +# **/*.php Any php file HTML Building +find . -name "*.php" ! -path "./system/*" ! -path "./vendor/*" ! -path "./layout/*" | while read -r file; do + file="${file#./}" #Remove the leading ./ + if [[ "$file" == "page.php" || "$file" == "post.php" ]]; then #pages and posts are handled above separately + continue; + fi + + without_extension="${file%.*}" + # Replace the .php extension with .txt + html_file="html/${file%.php}.html" + + # Create the directory structure if it doesn't exist + mkdir -p "$(dirname "$html_file")" + + filename=$(basename "$file" .php) + curl_and_save "/$without_extension" "$html_file" done +#endregion + +# ./assets/* copying to ./html cp -R assets html/assets +# Build CSS lessc html/assets/styles/style.less html/assets/styles/style.css - +# Remove .less as .css file is built rm -f html/assets/styles/*.less - +# Robots.txt is good to have cp robots.txt html/ diff --git a/system/workflow-image/Dockerfile b/system/workflow-image/Dockerfile index 2b66f01..b9129f0 100644 --- a/system/workflow-image/Dockerfile +++ b/system/workflow-image/Dockerfile @@ -10,8 +10,9 @@ RUN apt-get update && apt-get install -y \ node-less \ git \ jq \ - && docker-php-ext-configure gd --with-freetype --with-jpeg \ - && docker-php-ext-install gd pdo_mysql + procps \ + vim \ + && docker-php-ext-configure gd --with-freetype --with-jpeg # Remove default Nginx configuration RUN rm -rf /etc/nginx/sites-available/default \ @@ -28,6 +29,8 @@ RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/nginx RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +RUN echo "alias ll='ls -lha'" >> /root/.bashrc + LABEL org.opencontainers.image.source = "https://github.com/atas/ssg" ENTRYPOINT ["/entrypoint.sh"] diff --git a/system/workflow-image/nginx.conf b/system/workflow-image/nginx.conf index 676b7bc..6f8452f 100644 --- a/system/workflow-image/nginx.conf +++ b/system/workflow-image/nginx.conf @@ -24,7 +24,7 @@ server { location / { # If matching .php file found, rewrite to that, e.g. custom-page.php for /custom-page if (-f $request_filename.php) { - rewrite /$1.php last; + rewrite ^(.*)$ $1.php last; } try_files $uri $uri.html $uri/ @try-render-page;