How Strapi triggers Nuxt static site generation for S3 bucket

I have been working on ecommerce website, we just suppose to build the website as operational.

I choose Strapi and Nuxt, Algolia for search engine

Strapi is headless CMS, open-source, provides the large-scale of APIs without hard-coding.
Nu…


This content originally appeared on DEV Community and was authored by Liu Yu Zhou

I have been working on ecommerce website, we just suppose to build the website as operational.

I choose Strapi and Nuxt, Algolia for search engine

Strapi is headless CMS, open-source, provides the large-scale of APIs without hard-coding.
Nuxt is SSR vue.js based framework, useful for pre-rendered pages for ecommerce.
Algolia is client-side search engine, very impressive tool to make high traffic in business.

The infrastructure I use

  1. Source repositories on Github
  2. Strapi on AWS EC2 instance
  3. Nuxt on AWS S3 bucket

The things I need

The static website should be generated again when

  1. The developer commit the code updates.
  2. The author upload own product.

How did I tweak the problems

I solved all problems with GitHub Actions, it is the great tool for CI/CD.

> SSG : The developer commit the code updates.

It involves several steps:

  • Specify when the github action should be trigger
  • Configure AWS Credentials
  • Create ENV files
  • Cache Packages
  • Install Packages
  • Generate Static Pages
  • Deploy website on S3 bucket
  • Cloudfront Invalidation

> SSG: The author upload own product.

This is the tricky, I was stuck on this for couple of days.
There should be something to trigger S3 updates from Strapi,
I had researched the solutions and found that Netlify is the best solution because it provides build hook for CI/CD.
However, I wanted to take the challenge and decided not to use Netlify.

How can I achieve the same behavior without Netlify, keep S3 bucket?

1. Make the custom node app for webhook

After a while, I noticed that Github has REST APIs to control resources. I can dispatches APIs to certain repository.

https://api.github.com/repos/{ORG_NAME}/{REPO_NAME}/dispatches

This is the key.
In this node app, we just set headers to call github APIs.
I named the event as ssg.

const postData = Buffer.from(
  JSON.stringify({
    event_type: "ssg",
  })
);
headers: {
    Authorization: "token *****************",
    Accept: "application/vnd.github.everest-preview+json",
    "Content-Type": "application/json",
    "Content-Length": Buffer.byteLength(postData),
    "User-Agent": "nodejs request",
  },

Finally, upload nodejs app on EC2 instance. Here is the completed code.

2. Configure CI/CD script

Configure script to listen dispatch events from node app.
Github action listens the nodejs app request and regenerate the static website, deploy updates on S3.

name: CI/CD
on:
  repository_dispatch:
    types: ssg
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

3. Register webhook in Strapi dashboard

Register webhook on EC2 in Strapi dashboard

image

That's all, long journey. Thanks for joining me. :)


This content originally appeared on DEV Community and was authored by Liu Yu Zhou


Print Share Comment Cite Upload Translate Updates
APA

Liu Yu Zhou | Sciencx (2021-04-26T15:10:56+00:00) How Strapi triggers Nuxt static site generation for S3 bucket. Retrieved from https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/

MLA
" » How Strapi triggers Nuxt static site generation for S3 bucket." Liu Yu Zhou | Sciencx - Monday April 26, 2021, https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/
HARVARD
Liu Yu Zhou | Sciencx Monday April 26, 2021 » How Strapi triggers Nuxt static site generation for S3 bucket., viewed ,<https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/>
VANCOUVER
Liu Yu Zhou | Sciencx - » How Strapi triggers Nuxt static site generation for S3 bucket. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/
CHICAGO
" » How Strapi triggers Nuxt static site generation for S3 bucket." Liu Yu Zhou | Sciencx - Accessed . https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/
IEEE
" » How Strapi triggers Nuxt static site generation for S3 bucket." Liu Yu Zhou | Sciencx [Online]. Available: https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/. [Accessed: ]
rf:citation
» How Strapi triggers Nuxt static site generation for S3 bucket | Liu Yu Zhou | Sciencx | https://www.scien.cx/2021/04/26/how-strapi-triggers-nuxt-static-site-generation-for-s3-bucket/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.