Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate checkout, deploy, and --site + --base config #29

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout your repository using git
uses: actions/checkout@v4
- name: Install, build, and upload your site output
uses: withastro/action@v1
# with:
# path: . # The root location of your Astro project inside the repository. (optional)
# node-version: 18 # The specific version of Node that should be used to build your site. Defaults to 18. (optional)
# package-manager: pnpm@latest # The Node package manager that should be used to install dependencies and build your site. Automatically detected based on your lockfile. (optional)
# path: . # The root location of your Astro project inside the repository. (optional)
# node-version: 18 # The specific version of Node that should be used to build your site. Defaults to 18. (optional)
# package-manager: pnpm@latest # The Node package manager that should be used to install dependencies and build your site. Automatically detected based on your lockfile. (optional)

deploy:
needs: build
Expand Down
108 changes: 77 additions & 31 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: "Astro Deploy"
description: "A composite action that prepares your Astro site to be deployed to GitHub Pages"
description: "A composite action that deploys your Astro site to GitHub Pages"
branding:
icon: "box"
color: "orange"
color: "purple"
inputs:
node-version:
description: "The node version to use"
required: false
default: "18"
default: "20"
package-manager:
description: "You may specify your preferred package manager (one of `npm | yarn | pnpm | bun` and an optional `@<version>` tag). Otherwise, the package manager will be automatically detected."
required: false
Expand All @@ -16,45 +16,82 @@ inputs:
description: "Path of the directory containing your site"
required: false
default: "."
outputs:
url:
description: "The URL of the deployed Pages site"
value: ${{ steps.deploy.outputs.page_url }}

runs:
using: composite
steps:
- name: Check lockfiles
- name: Checkout repository
uses: actions/checkout@v4

- name: Check tools
shell: "bash"
working-directory: ${{ inputs.path }}
env:
INPUT_PM: ${{ inputs.package-manager }}
run: |
len=`echo $INPUT_PM | wc -c`
# Check tools
# Sets $PACKAGE_MANAGER based on input or detected lockfile
len=$(echo $INPUT_PM | wc -c)
if [ $len -gt 1 ]; then
PACKAGE_MANAGER=$(echo "$INPUT_PM" | grep -o '^[^@]*')
VERSION=$(echo "$INPUT_PM" | grep -o '@.*' | sed 's/^@//')
# Set default VERSION if not provided
if [ -z "$VERSION" ]; then
VERSION="latest"
fi
echo "PACKAGE_MANAGER=$PACKAGE_MANAGER" >> $GITHUB_ENV
PACKAGE_MANAGER=$(echo "$INPUT_PM" | grep -o '^[^@]*')
VERSION=$(echo "$INPUT_PM" | (grep -o '@.*' || [ "$?" == "1" ]) | sed 's/^@//')
elif [ $(find "." -name "pnpm-lock.yaml") ]; then
echo "PACKAGE_MANAGER=pnpm" >> $GITHUB_ENV
echo "LOCKFILE=pnpm-lock.yaml" >> $GITHUB_ENV
elif [ $(find "." -name "yarn.lock") ]; then
echo "PACKAGE_MANAGER=yarn" >> $GITHUB_ENV
echo "LOCKFILE=yarn.lock" >> $GITHUB_ENV
elif [ $(find "." -name "package-lock.json") ]; then
VERSION="latest"
echo "PACKAGE_MANAGER=npm" >> $GITHUB_ENV
echo "LOCKFILE=package-lock.json" >> $GITHUB_ENV
elif [ $(find "." -name "bun.lockb") ]; then
VERSION="latest"
echo "PACKAGE_MANAGER=bun" >> $GITHUB_ENV
echo "LOCKFILE=bun.lockb" >> $GITHUB_ENV
PACKAGE_MANAGER=pnpm
elif [ $(find "." -name "yarn.lock") ]; then
PACKAGE_MANAGER=yarn
elif [ $(find "." -name "package-lock.json") ]; then
PACKAGE_MANAGER=npm
elif [ $(find "." -name "bun.lockb") ]; then
PACKAGE_MANAGER=bun
else
echo "No lockfile found.
Please specify your preferred \"package-manager\" in the action configuration."
Please specify your preferred \"package-manager\" in the action configuration."
exit 1
fi
echo "VERSION=$VERSION" >> $GITHUB_ENV

# If no $VERSION has been provided, default to latest
if [ -z "$VERSION" ]; then
VERSION="latest"
fi

RUNNER_FLAGS=""
case $PACKAGE_MANAGER in
npm)
COMMAND=ci
RUNNER=npm
RUNNER_FLAGS="exec --no-install --"
LOCKFILE=package-lock.json
;;
pnpm)
COMMAND=install
RUNNER=pnpm
RUNNER_FLAGS=exec
LOCKFILE=pnpm-lock.yaml
;;
yarn)
COMMAND=install
RUNNER=yarn
LOCKFILE=yarn.lock
;;
bun)
COMMAND=install
RUNNER=bun
RUNNER_FLAGS="run --bun"
LOCKFILE=bun.lockb
;;
esac

echo "PACKAGE_MANAGER=$PACKAGE_MANAGER
VERSION=$VERSION
COMMAND=$COMMAND
RUNNER=$RUNNER
RUNNER_FLAGS=$RUNNER_FLAGS
LOCKFILE=$LOCKFILE" >>$GITHUB_ENV

- name: Setup PNPM
if: ${{ env.PACKAGE_MANAGER == 'pnpm' }}
uses: pnpm/action-setup@v2
Expand All @@ -81,18 +118,27 @@ runs:
with:
node-version: ${{ inputs.node-version }}

- name: Setup Pages
id: pages
uses: actions/configure-pages@v3
with:
enablement: true

- name: Install
shell: "bash"
working-directory: ${{ inputs.path }}
run: $PACKAGE_MANAGER install
run: $PACKAGE_MANAGER $COMMAND

- name: Build
shell: "bash"
working-directory: ${{ inputs.path }}
run: $PACKAGE_MANAGER run build
run: eval "$RUNNER $RUNNER_FLAGS astro build --site \"${{ steps.pages.outputs.origin }}\" --base \"${{ steps.pages.outputs.base_path }}\""

- name: Upload Pages Artifact
# Must use v2 to avoid requiring `actions/deploy-pages@v4` or newer
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
path: "${{ inputs.path }}/dist/"

- name: Deploy to GitHub Pages
id: deploy
uses: actions/deploy-pages@v4