Modified from Eleventy website to GitHub Pages with GitHub Actions by Andres Lopez
-
Create GitHub account
-
Open your browser, log in to GitHub and navigate to github.new
-
Name repository
eleventy-github -
Add a README file
-
Click green "Create Repository" button
-
Click the green "<>Code" dropdown and select the right "Codespaces" tab and the green "Create codespace on main" button
-
Your codespace will open in a new browser tab with Explorer to the left, Preview to the right, and Terminal at the bottom. You can toggle this view as needed with the icons in the top right
-
In your Terminal, copy and paste
npm init -yand hit enter. This will create apackage.jsonfile -
Next copy and paste
npm install @11ty/eleventy@latest sass@latest --save-exactand hit enter. This will install Eleventy, create anode_modulesfolder, and apackage-lock.jsonfile. (Note: Explain what the three views are doing and the files in the explorer so far) -
In the Explorer, select the new
package.jsonfile. -
Highlight line 7, the "test" script and replace with the following (check indenting)
`"watch:sass": "sass src/static/scss:public/static/css --watch", "build:sass": "sass src/static/scss:public/static/css", "watch:eleventy": "eleventy --serve", "build:eleventy": "ELEVENTY_ENV=development eleventy", "start": "npm run watch:eleventy & npm run watch:sass", "build": "npm run build:eleventy & npm run build:sass"` -
Create a new
srcfolder by clicking on the folder+ button in Explorer -
Inside your new
srcfolder create two subfolders:_includesandstatic -
Inside your new
staticfolder, create a newscssfolder -
Next create a new file
.eleventy.jsin the root directory with the following code:
`module.exports = function (eleventyConfig) { eleventyConfig.setBrowserSyncConfig({ files: './public/static/**/*.css', });
return { dir: { input: 'src', output: 'public', }, }; };`
- Create an empty file named
main.scssin thestatic/scssfolder - Create a template file named
base.njkin the_includesfolder - Paste the following code in
base.njk
`
<title>{{ title }}</title> {% block content %} {{ content | safe }} {% endblock %} `- Create an
index.mdfile in yoursrcfolder - Paste the following code in your new
index.mdfile
--- layout: 'base.njk' permalink: / title: 'Our Eleventy page' ---
# Hello Price Lab
- Permanently delete
README.md - Open the Terminal and paste
npm run start - Hit Enter and it will run the start script
- Open the http://localhost:8080/ in a new browser tab
- Go to line 6 in the of
base.njk, press enter, and paste the line of code
<link rel="stylesheet" type="text/css" href="{{ 'static/css/main.css' | url }}" media="screen" />
- Add this code to
main.scssh1 { color: green; background-color: black; text-align: center; padding: 50px; } - Refresh Local host page and see CSS changes
- Create new
.gitignorefile in root directory and paste the following code:
# Dependencies /node_modules
# Misc /public
# Intellij /.idea
# Sass .sass-cache/
*.css.map .sass.map
`.scss.map
# Visual Studio Code /.vscode
.history
- Then go to GitHub repository Settings>Pages in left sidebar
- Click the Branch none dropdown menu and switch to "main" and then hit "Save"
- By default, GitHub pages utilizes Jekyll to generate your website. However, in our case, we’re using Eleventy, so we need to notify GitHub Pages about this. To achieve this, make an empty file named
.nojekylland place it in the root directory. - Then, create a
.githubfolder in the root directory and within that folder, create another folder namedworkflows. - Create a
build.ymlfile in workflows and paste this code `name: Build PR
on: pull_request: branches: ['main']
jobs: build: runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20']
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install packages
run: npm ci
- name: Run npm build
run: npm run build`
- Create a
build-and-deploy.ymlfile in workflows and paste this code `name: Build and Deploy
on: push: branches: ['main']
jobs: build: runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20']
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install packages
run: npm ci
- name: Run npm build
run: npm run build:prod
- name: Deploy to gh-pages
uses: peaceiris/actions-gh-pages@v4
with:
deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}`
- Control+C to stop current terminal command.
- Generate your deploy key with the following command in the Terminal
ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages -N ""You will get 2 files:gh-pages.pubis a public key andgh-pagesis a private key - Now go to Repository Settings>Deploy Keys
- Select the green button "add deploy key"
- Give it the title “Public key of ACTIONS_DEPLOY_KEY”, copy and paste your public key (gh-pages.pub) with the Allow write access checked
- Then go to Secrets and Variables>Actions> and select the green button "New repository secret"
- Name your private key ACTIONS_DEPLOY_KEY and copy and paste your private key (gh-pages) and hit the green "Add secret" button
- Then delete
gh-pagesandgh-pages.pubfrom your Explorer - In your Terminal, run clear to clear the public key image
- Add more scripts to
package.jsonfile (be sure to add a comma after every script and check indentation)"build:sass:prod": "sass src/static/scss:public/static/css --style compressed", "build:eleventy:prod": "ELEVENTY_ENV=production eleventy", "build:prod": "npm run build:eleventy:prod & npm run build:sass:prod" - Then paste the following commands to Terminal (This is a shortcut to add the current directory, then commit with a message, then push)
git add . git commit -m "build and deploy" git push - Go to your GitHub repository and click on the Actions tab at the top. You’ll usually find the Build & Deploy workflow running, followed by the pages-build-deployment workflow which deploys the website to GitHub Pages once the first one is completed
- Once the Build & Deploy workflow runs, a new branch named "gh-pages" is automatically generated. This branch will contain the output of your Eleventy site. To ensure the website can be hosted correctly, we must notify GitHub Pages.
- Go to the Settings tab, choose Pages from the menu on the left, switch the branch for your GitHub Pages site to "gh-pages", and click save.
- Once the pages-build-deployment workflow is completed, it will automatically be triggered again. After it’s done, you can check out the final result by visiting the URL where your website is published.
- Make changes in Codespaces to
index.mdand then redo git add/commit/push