Closed MonikaBarget closed 1 day ago
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.
To publish or install packages from a private npm registry (such as npmjs.org), you need to authenticate via an npm token:
yaml
file:name: Authenticate to npm
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
yaml
file:yaml
1. - name: Set up Node.js
2. uses: actions/setup-node@v3
3. with:
4. node-version: '20'
index
file exits in the docs
folder:/root
├── docs
│ ├── .vitepress
│ │ └── config.js
│ └── index.md
├── node_modules
├── package.json
├── package-lock.json
└── vite.config.js
github/workflows/deploy.yml
file.docs/.vitepress/config.js
is set up with your site’s settings and navigation.build,
dev
and deploy
scripts are correctly defined in package.json
.Commit any changes to your repository and rerun the workflow.
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
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:
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.
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 }}
name: Run deploy script run: | chmod +x scripts/deploy.sh
scripts/deploy.sh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
fatal: destination path 'deploy_repo' already exists and is not an empty directory
notification when running npm run deploy
. This error occurs because the deploy_repo
directory was already created. This can happen when the script is run multiple times without cleaning up the previous clone. Modify the deploy.sh
script to remove the deploy_repo
directory if it exists before cloning the repository. This ensures that the directory is clean each time the script runs:#!/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!"
deploy.yml
Reconsider which branch to set: "main" or "gh-pages"?