Skip to main content
All CollectionsShifter Webhooks
Converting artifact into subdirectory and publishing it to the external server
Converting artifact into subdirectory and publishing it to the external server
Tomohyco Tsunoda avatar
Written by Tomohyco Tsunoda
Updated over 4 years ago

Shifter starts to support sub directory publish, but you still need to do some works with it if you're publishing artifacts into the external server like AWS S3 or Netlify. In this document, we want to introduce and describe our script and how to do it. The webhook for sub directory publishing to the Netlify is available now.

 

Steps

Step 1. Download an artifact from Shifter dashboard

Step 2. Get the converting script

Copy, paste the script in the script section to your notepad and save it.

Step 3. Run the script for downloaded aritfact

Step 4. Publish converted artifact to external server like AWS S3 or Netlify.

 

Script

Copy and save as you like.

ARTIFACT_ID=YOUR_ARTIFACT_ID
tar xvzf "${ARTIFACT_ID}".tgz

# Set the name of sub directory, source URL and target URL.
# In the below sample, set sub directory to "blog", published URL (source URL) to "example.on.getshifter.io" and host of the target URL to "example.com"

export SUBDIR=blog
export BASE_NAME=example.on.getshifter.io
export NEW_NAME=example.com

# Rewrite `/`` to `/blog/``
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@<a( [^\/>]*)href="/@<a$1href="/$ENV{SUBDIR}/@gsi' {} \;
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e "s@<a( [^\/>]*)href='/@<a $1href='/\$ENV{SUBDIR}/@gsi" {} \;

find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e "s@'/wp-content@/\$ENV{SUBDIR}/wp-content@gsi" {} \;
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e "s@'/wp-includes@'/\$ENV{SUBDIR}/wp-includes@gsi" {} \;

find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@"/wp-content@"/$ENV{SUBDIR}/wp-content@gsi' {} \;
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@"/wp-includes@"/$ENV{SUBDIR}/wp-includes@gsi' {} \;

find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@\s/wp-content@ /$ENV{SUBDIR}/wp-content@gsi' {} \;
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@\s/wp-includes@ /$ENV{SUBDIR}/wp-includes@gsi' {} \;

# update amphtml
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@<link\srel="amphtml"\shref="https://$ENV{BASE_NAME}/@<link rel="amphtml" href="https://$ENV{NEW_NAME}/$ENV{SUBDIR}/@gsi' {} \;
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e "s@<link\srel='amphtml'\shref='https://\$ENV{BASE_NAME}/@<link rel='amphtml' href='https://\$ENV{NEW_NAME}/\$ENV{SUBDIR}/@gsi" {} \;
find "${ARTIFACT_ID}/*/amp/" -name 'index.html' -exec perl -pi -e 's@<link\srel="canonical"\shref="https://$ENV{BASE_NAME}/@<link rel="canonical" href="https://$ENV{NEW_NAME}/$ENV{SUBDIR}/@gsi' {} \;
find "${ARTIFACT_ID}/*/amp/" -name 'index.html' -exec perl -pi -e "s@<link\srel='canonical'\shref='https://\$ENV{BASE_NAME}/@<link rel='canonical' href='https://\$ENV{NEW_NAME}/\$ENV{SUBDIR}/@gsi" {} \;

# update meta
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e "s@<meta( [^\/>]*)content='https://\$ENV{BASE_NAME}/@<meta $1content='https://\$ENV{NEW_NAME}/\$ENV{SUBDIR}/@gsi" {} \;
find "${ARTIFACT_ID}" -name 'index.html' -exec perl -pi -e 's@<meta( [^\/>]*)content="https://$ENV{BASE_NAME}/@<meta $1content="https://$ENV{NEW_NAME}/$ENV{SUBDIR}/@gsi' {} \;

# update feed
perl -pi -e 's@<link>https://$ENV{BASE_NAME}@<link>https://$ENV{NEW_NAME}/$ENV{SUBDIR}@gsi' "${ARTIFACT_ID}"/feed/index.html
perl -pi -e 's@<atom:link( [^\/>]*)href="https://$ENV{BASE_NAME}@<atom:link$1href="https://$ENV{NEW_NAME}/$ENV{SUBDIR}@gsi' "${ARTIFACT_ID}"/feed/index.html

Since the version 2, compatible below:

  • Converting URLs in XML RSS feed

  • Converting URLs of OGP

  • Converting  URLs for srcset   in img  elements 

 

Sample script for sub directory publishing to Netlify 

Did this answer your question?