MonikaBarget / distant-reading

teaching materials for distant reading
https://monikabarget.github.io/distant-reading/
0 stars 1 forks source link

Fix deployment problems #3

Closed MonikaBarget closed 1 day ago

MonikaBarget commented 2 days ago

deploy.yml

Reconsider which branch to set: "main" or "gh-pages"?

on:
  push:
    branches:
      - gh-pages

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '20'  # Use Node.js 20

      - name: Install dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Deploy
        run: npm run deploy
MonikaBarget commented 1 day ago

Branch set-up

The branch has to be set to Github Pages, and it does not work well to deploy directly from Github Actions. Publishing manually is recommended. An NPM token is needed for authentication.

Creating an NPM authentication

To publish or install packages from a private npm registry (such as npmjs.org), you need to authenticate via an npm token:

name: Authenticate to npm
      run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc

Other important settings

yaml
1.  - name: Set up Node.js
2.    uses: actions/setup-node@v3
3.    with:
4.      node-version: '20'
/root
  ├── docs
  │   ├── .vitepress
  │   │   └── config.js
  │   └── index.md
  ├── node_modules
  ├── package.json
  ├── package-lock.json
  └── vite.config.js

Commit any changes to your repository and rerun the workflow.

MonikaBarget commented 1 day ago

Manual deployment via Codespace Terminal

Log in to NPM

If Github Actions does not automatically publish for you, you can manually deploy via the terminal in Codespace, but you need to have NPM authentication. Before you can run commands for deployment, you need to log in to NPM within the terminal:

npm adduser

This will prompt you to add your username and password.

If you do not yet have an access token for your project yet, you can create it with the following command: npm token create

Allow VS Code access to browser clipboard

One problem can be that your browser does not allow VS Code to read from the clipboard to copy and paste the token, so perform the following steps if necessary:

Pay attention to correct versioning

npm does not allow publishing a package with the same version number as an existing published version. Update the version number in your package.json file before publishing. For example, change from "version": "1.0.0" to "version": "1.0.1" or a higher version number. If your project or other projects depend on this version, update any references to use the new package name. For example, in dependencies or devDependencies in other package.json files.

Use deploy.sh script to publish

Instead of automating deployment via Github Actions, you can use a deploy.sh script. This script can handle building your VitePress site and deploying it to GitHub Pages. The deploy.sh script for the Distant Reading repository looks like this:

#!/bin/bash

# Exit script on any error
set -e

# Define variables
REPO_URL="https://github.com/monikabarget/DistantReading.git"
BRANCH="gh-pages"
BUILD_DIR="docs/.vitepress/dist"

# Print the current state
echo "Starting deployment script..."

# Checkout the repository
echo "Cloning the repository..."
git clone $REPO_URL --branch $BRANCH --single-branch deploy_repo

cd deploy_repo

# Clean the current deployment
echo "Cleaning previous deployment files..."
rm -rf *

# Copy the new build files
echo "Copying new build files..."
cp -R ../$BUILD_DIR/* .

# Commit and push the new build
echo "Committing and pushing changes..."
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add -A
git commit -m "Deploy updated site"
git push origin $BRANCH

# Go back to the original directory
cd ..

# Clean up
rm -rf deploy_repo

echo "Deployment complete!"

Before running the script, you have to make it executable in the terminal:

chmod +x deploy.sh

Then you can run:

./deploy.sh

If you want to integrate this script with GitHub Actions, you can create a workflow file that runs this script. Here’s an example of how you might set up a GitHub Actions workflow (in your yaml file) to run your deploy.sh script:

name: Deploy VitePress to GitHub Pages

on:
  push:
    branches: main
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install dependencies
        run: npm ci

      - name: Build VitePress site
        run: npm run docs:build

      - name: Run deploy script
        run: |
          chmod +x deploy.sh
          ./deploy.sh
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Trouble-shooting advice

#!/bin/bash

# Exit script on any error
set -e

# Define variables
REPO_URL="https://github.com/monikabarget/DistantReading.git"
BRANCH="gh-pages"
BUILD_DIR="docs/.vitepress/dist"
DEPLOY_DIR="deploy_repo"

# Print the current state
echo "Starting deployment script..."

# Check if the deploy directory exists and remove it if it does
if [ -d "$DEPLOY_DIR" ]; then
  echo "Removing existing deploy directory..."
  rm -rf "$DEPLOY_DIR"
fi

# Clone the repository
echo "Cloning the repository..."
git clone $REPO_URL --branch $BRANCH --single-branch $DEPLOY_DIR

cd $DEPLOY_DIR

# Clean the current deployment
echo "Cleaning previous deployment files..."
rm -rf *

# Copy the new build files
echo "Copying new build files..."
cp -R ../$BUILD_DIR/* .

# Commit and push the new build
echo "Committing and pushing changes..."
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add -A
git commit -m "Deploy updated site"
git push origin $BRANCH

# Go back to the original directory
cd ..

# Clean up
rm -rf $DEPLOY_DIR

echo "Deployment complete!"

Alternative deploy.sh script:

#!/bin/bash

# Exit script on any error
set -e

# Define variables
REPO_URL="https://github.com/monikabarget/DistantReading.git"
BRANCH="gh-pages"
BUILD_DIR="docs/.vitepress/dist"
DEPLOY_DIR="deploy_repo"

# Print the current state
echo "Starting deployment script..."

# Ensure deploy directory is clean
if [ -d "$DEPLOY_DIR" ]; then
  echo "Directory $DEPLOY_DIR exists. Removing..."
  rm -rf "$DEPLOY_DIR"
fi

# Clone the repository
echo "Cloning the repository..."
git clone $REPO_URL --branch $BRANCH --single-branch $DEPLOY_DIR

cd $DEPLOY_DIR

# Clean the current deployment
echo "Cleaning previous deployment files..."
rm -rf *

# Copy the new build files
echo "Copying new build files..."
cp -R ../$BUILD_DIR/* .

# Commit and push the new build
echo "Committing and pushing changes..."
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add -A
git commit -m "Deploy updated site"
git push origin $BRANCH

# Go back to the original directory
cd ..

echo "Deployment complete!"