Merge branch 'main' of github.com:wintermeyer/elixir-phoenix-ash #177
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '16' | |
- 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 Server | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
SERVER_IP: ${{ secrets.SERVER_IP }} | |
SERVER_USER: ${{ secrets.SERVER_USER }} | |
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} | |
run: | | |
mkdir -p ~/.ssh | |
echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan $SERVER_IP >> ~/.ssh/known_hosts | |
rsync -avz --delete ./build/site/ $SERVER_USER@$SERVER_IP:$DEPLOY_PATH | |
- name: Log deployment success | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
SERVER_IP: ${{ secrets.SERVER_IP }} | |
SERVER_USER: ${{ secrets.SERVER_USER }} | |
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} | |
run: | | |
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "logger -t deployment 'Successfully deployed to ${DEPLOY_PATH} from the GitHub repository.'" |