Skip to content

Remove comments

Remove comments #168

name: Build and Deploy Antora Site
on:
push:
branches:
- main # adjust this if your primary branch is named differently
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install rsync, zopfli and brotli
run: sudo apt-get install -y rsync brotli zopfli
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Antora
run: |
npm install -g antora
- name: Build the Antora site
run: |
antora --fetch antora-playbook.yml
- name: Install PurgeCSS
run: |
npm install -g purgecss
- name: Replace paths in CSS files using awk
run: |
awk '{gsub(/url\(..\//, "url(/antora-assets/"); print}' ./build/site/antora-assets/css/site.css > ./build/site/antora-assets/css/site_temp.css && mv ./build/site/antora-assets/css/site_temp.css ./build/site/antora-assets/css/site.css
awk '{gsub(/url\(..\//, "url(/antora-assets/"); print}' ./build/site/antora-assets/css/site-extra.css > ./build/site/antora-assets/css/site-extra_temp.css && mv ./build/site/antora-assets/css/site-extra_temp.css ./build/site/antora-assets/css/site-extra.css
- name: Optimize and Inline CSS for each HTML file
run: |
# Create the purgecss.config.js file on-the-fly
echo "module.exports = {" > purgecss.config.js
echo " content: []," >> purgecss.config.js
echo " css: []," >> purgecss.config.js
echo " safelist: {" >> purgecss.config.js
echo " standard: ['embedded', /toc.*/, /copy.*/, /source.*/, /hljs.*/, 'language-elixir', 'language-bash', /highlight.*/, /arabic.*/, 'colist', /.*conum.*/, 'table', 'tr', 'td', 'th', 'theader', 'tbody', 'anchor.*', 'h1', 'h2', 'h3', 'h4', 'h5']," >> purgecss.config.js
echo " deep: [/blue_example$/]," >> purgecss.config.js
echo " greedy: [/yellow_example$/]" >> purgecss.config.js
echo " }," >> purgecss.config.js
echo " output: './build/site/antora-assets/css/'" >> purgecss.config.js
echo "};" >> purgecss.config.js
find ./build/site/ -name "*.html" | while read -r file; do
# Optimize CSS for the current HTML file using the configuration file
purgecss --config purgecss.config.js --content "$file" --css ./build/site/antora-assets/css/site.css --output ./build/site/antora-assets/css/site_temp.css
purgecss --config purgecss.config.js --content "$file" --css ./build/site/antora-assets/css/site-extra.css --output ./build/site/antora-assets/css/site-extra_temp.css
# Combine the optimized CSS files
SITE_CSS=$(cat ./build/site/antora-assets/css/site_temp.css)
SITE_EXTRA_CSS=$(cat ./build/site/antora-assets/css/site-extra_temp.css)
COMBINED_CSS="$SITE_CSS $SITE_EXTRA_CSS"
# Inline the combined optimized CSS into the current HTML file
awk -v css="$COMBINED_CSS" '/<head>/ {print; print "<style>" css "</style>"; next} 1' "$file" > "$file".tmp && mv "$file".tmp "$file"
# Remove the original link tags from the current HTML file
sed -i "/antora-assets\/css\/site.css/d" "$file"
sed -i "/antora-assets\/css\/site-extra.css/d" "$file"
# Clean up temporary CSS files
rm ./build/site/antora-assets/css/site_temp.css
rm ./build/site/antora-assets/css/site-extra_temp.css
done
- name: Install html-minifier
run: |
npm install -g html-minifier
- name: Aggressively minify HTML files
run: |
find ./build/site/ -name "*.html" | while read -r file; do
html-minifier --collapse-whitespace --remove-comments --minify-js --minify-css --remove-redundant-attributes --remove-script-type-attributes --remove-style-link-type-attributes --use-short-doctype --collapse-boolean-attributes --sort-attributes --sort-class-name < "$file" > "$file".tmp
mv "$file".tmp "$file"
done
- name: Zopfli compress all HTML and CSS files
run: |
find ./build/site/ \( -name "*.html" -o -name "*.css" \) -exec zopfli --i20 {} \;
- name: Brotli compress all HTML and CSS files
run: |
find ./build/site/ \( -name "*.html" -o -name "*.css" \) -exec brotli {} \;
- name: Deploy to Linux server via rsync
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
SERVER: ${{ secrets.SERVER_ADDRESS }}
USERNAME: ${{ secrets.SERVER_USERNAME }}
TARGET: ${{ secrets.SERVER_TARGET_DIRECTORY }}
KNOWN_HOSTS: ${{ secrets.KNOWN_HOSTS_ENTRY }}
run: |
echo "$DEPLOY_KEY" > deploy_key
chmod 600 deploy_key
echo "$KNOWN_HOSTS" > known_hosts
rsync -avz -e 'ssh -i deploy_key -o UserKnownHostsFile=known_hosts' --delete ./build/site/ $USERNAME@$SERVER:$TARGET
rm -f deploy_key known_hosts