Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSA: I made this tool work using some scripts #61

Closed
ItsIgnacioPortal opened this issue Mar 31, 2022 · 1 comment
Closed

PSA: I made this tool work using some scripts #61

ItsIgnacioPortal opened this issue Mar 31, 2022 · 1 comment

Comments

@ItsIgnacioPortal
Copy link
Contributor

ItsIgnacioPortal commented Mar 31, 2022

I've created a setup so that when I want to deploy ghost to github pages, I just run have to run ghostdeploy in cmd, then git commit -m "some message" and git push. This is the result: https://itsignacioportal.github.io/. No broken links, no broken images, no errors :)

Environment:
Windows 10

  • node v16.14.2
  • Ghost-CLI version: 1.19.2
  • Ghost version: 4.41.3 (at ~\AppData\Local\Ghost_Server)
  • ghostdeploy.bat in a folder that's part of %PATH% //This is the script we launch
  • python 3.9
  • replacer3.py //Used mainly to fix links
  • ImageMagick (Used to resize images, for more performance on github pages)

WSL:

  • Debian
  • node v16.14.2 installed via NVM
  • gssg installed. //gssg doesn't work on Windows with Cygwin, but it does work on WSL, idk why
  • gssgsummoner.sh at $HOME //A wrapper for gssg
  • gitcleaner.sh at $HOME //deletes all files except ".git" folder, and my google search console ownership file
  • /etc/hosts modified to redirect localhost to 192.168.50.21 (beware of the warning on the first line of the file) //modification done because otherwise you would need to have Ghost running directly on WSL, and that's just bothersome for many reasons

The /etc/hosts redirection was made persistant using a scheduled task that runs "with the highest privileges" on user log-in (See: MicrosoftDocs/WSL#418):

@echo off
wsl -d Debian -u root ip addr add 192.168.50.11/24 broadcast 192.168.50.255 dev eth0 label eth0:1

netsh interface ip add address "vEthernet (WSL)" 192.168.50.21 255.255.255.0
@echo on
exit

You'll obviously have to modify some paths to make this work for you, but hey, it's better than starting at nothing. I spent three days making all of these scripts :p

ghostdeploy.bat:

@echo off

REM Summon the Ghost Static Page Generator
wsl.exe -d Debian cd /mnt/c/Users/Sherman/Documents/GitHub/ItsIgnacioPortal.github.io/ ^&^& /home/pinkdev1/gitcleaner.sh ^&^& echo "[+] Running the Ghost Static Site Generator (gssg)..." ^&^& /home/pinkdev1/gssgsummoner.sh

REM Fix picture links
echo [+] Fixing picture links and dangling hrefs...
py -3.9 "C:\Program Files\HackingSoftware\ghost\replacer3.py"

REM Fix profile picture
echo [+] Fixing profile picture...
cd C:\Users\Sherman\Documents\GitHub\ItsIgnacioPortal.github.io
mkdir content\images\size\w100\2022\03
REM Make sure to modify the Circle-2.png filename to match the actual filename of your uploaded profile picture
magick convert content/images/2022/03/Circle-2.png -resize 100x100! content/images/size/w100/2022/03/Circle-2.png

echo [+] Commiting files...
git config --global core.safecrlf false
git add .

echo [+] The blog is now ready for deployment!
echo [+] Just pick a commit message, and push it to the repo.

cd C:\Users\Sherman\Documents\GitHub\ItsIgnacioPortal.github.io

(file on WSL) gitcleaner.sh

#!/bin/bash

shopt -s extglob
cd /mnt/c/Users/Sherman/Documents/GitHub/ItsIgnacioPortal.github.io/
echo "[+] (WSL): Deleting everything except .git folder on \"/mnt/c/Users/Sherman/Documents/GitHub/ItsIgnacioPortal.github.io/\"..."
rm -rf !(.git|googleeef7c96830aeb56d.html)

replacer3.py

import os

root_dir = 'C:\\Users\\Sherman\\Documents\\GitHub\\ItsIgnacioPortal.github.io'
githubPagesDomain = "itsignacioportal.github.io"
fourLetterAllowedExtensions = ["html"]
threeLetterAllowedExtensions = ["xml", "css", "txt", "xsl"]
twoLetterAllowedExtensions = ["js"]

#There's probably a better way of doing this, but works well enough
def hasAllowedExtension(file):
	if file[-4:] in fourLetterAllowedExtensions:
		return True

	if file[-3:] in threeLetterAllowedExtensions:
		return True

	if file[-2:] in twoLetterAllowedExtensions:
		return True

	return False

def replaceStrings(searchText, replaceText, file):
	print("[+] Replacing \"" + searchText +"\" in \"" + file + "\" ...")
	#https://stackoverflow.com/a/17141572/11490425
	# Read in the file
	with open(file, 'r') as iofile :
		filedata = iofile.read()

	# Replace the target string
	filedata = filedata.replace(searchText, replaceText)

	# Write the file out again
	with open(file, 'w') as iofile:
		iofile.write(filedata)

def replaceStringsInDir(searchText, replaceText, folder):
	for file in os.listdir(folder):
		#print(folder + "\\" + file)
		if os.path.isdir(folder + "\\" +file) and file != ".git":
			file = folder + "\\" + file
			print("[+][DEBUG] Recursing to: " + file)
			replaceStringsInDir(searchText, replaceText, file)
		elif hasAllowedExtension(file):
			replaceStrings(searchText, replaceText, folder + "\\" + file)


def replaceRecursively(searchText, replaceText, mainfolder):
	for file in os.listdir(root_dir):
		if file != ".git":
			if os.path.isdir(mainfolder + file):
				replaceStringsInDir(searchText, replaceText, mainfolder + "\\" + file)
			else:
				replaceStringsInDir(searchText, replaceText, mainfolder)

os.chdir(root_dir)
replaceRecursively("http://localhost:2368", "https://" + githubPagesDomain, root_dir)

#Fix sitemaps
replaceStrings("href=\"//localhost:2368", "href=\"//" + githubPagesDomain, root_dir + "\\" + "sitemap-authors.xml")
replaceStrings("href=\"//localhost:2368", "href=\"//" + githubPagesDomain, root_dir + "\\" + "sitemap-pages.xml")
replaceStrings("href=\"//localhost:2368", "href=\"//" + githubPagesDomain, root_dir + "\\" + "sitemap-posts.xml")
replaceStrings("href=\"//localhost:2368", "href=\"//" + githubPagesDomain, root_dir + "\\" + "sitemap-tags.xml")
replaceStrings("href=\"//localhost:2368", "href=\"//" + githubPagesDomain, root_dir + "\\" + "sitemap.xml")

(file on WSL) gssgsummoner.sh

#!/bin/bash
cd /mnt/c/Users/Sherman/Documents/GitHub/ItsIgnacioPortal.github.io/
gssg --productionDomain "https://itsignacioportal.github.io" --dest "/mnt/c/Users/Sherman/Documents/GitHub/ItsIgnacioPortal.github.io/"
@ItsIgnacioPortal
Copy link
Contributor Author

These scripts are no longer needed :D

https://github.com/itsignacioportal/ghost-static-site-generator/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant