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

wip: deploy preview site for PRs, to make contributing easier #196

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4f138b7
wip: deploy preview site for PRs, to make contributing easier
allison-truhlar Apr 17, 2024
db43771
fix: correct folder where astro build is located
allison-truhlar Apr 17, 2024
9187549
fix: increase permissions for pull requests
allison-truhlar Apr 17, 2024
9818f72
wip: trying a different base github action - previous was deprecated
allison-truhlar Apr 17, 2024
50cd9ca
wip: adds additional configuration to deploy preview action
allison-truhlar Apr 17, 2024
79478a5
wip: attempt to add github action for preview deploy
allison-truhlar Apr 17, 2024
cef7f8d
fix: invalid path name
allison-truhlar Apr 17, 2024
7a7f5ea
fix: move checkout before attempting to access shell script
allison-truhlar Apr 17, 2024
d4cab6b
fix: make the shell script executable
allison-truhlar Apr 17, 2024
846527b
fix: attempted again to add executable to shell script
allison-truhlar Apr 17, 2024
0224f4c
wip: changing to a different build action
allison-truhlar Apr 17, 2024
c0a4aa6
fix: making shell script executable - again?
allison-truhlar Apr 17, 2024
a1eb672
fix: add write permissions for pull request to enable commenting
allison-truhlar Apr 17, 2024
6a50377
fix: adding bash to attempt to execute shell script on each run
allison-truhlar Apr 17, 2024
ffea861
wip: going back to uploading a pages artifact to deploy
allison-truhlar Apr 17, 2024
13dcdfc
wip: adding a preview_path env variable to the baseurl
allison-truhlar Apr 18, 2024
6e6ab33
wip: change action to composite and add bash to make sure the script …
allison-truhlar Apr 18, 2024
ea8f8a5
test: create pr-preview folder
allison-truhlar Apr 18, 2024
3557bb7
wip: add flags to astro build command
allison-truhlar Apr 18, 2024
e519dda
wip: pull_request_target > pull_request
allison-truhlar Apr 18, 2024
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
28 changes: 28 additions & 0 deletions .github/actions/determine-auto-action.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Script copied from https://github.com/rossjrw/pr-preview-action/blob/main/lib/determine-auto-action.sh

case $GITHUB_EVENT_NAME in
"pull_request" | "pull_request_target")
echo "event_name is $GITHUB_EVENT_NAME; proceeding"
;;
*)
echo "unknown event $GITHUB_EVENT_NAME; no action to take"
echo "action=none" >> "$GITHUB_ENV"
exit 0
;;
esac

event_type=$(jq -r ".action" "$GITHUB_EVENT_PATH")
echo "event_type is $event_type"

case $event_type in
"opened" | "reopened" | "synchronize")
echo "action=deploy" >> "$GITHUB_ENV"
;;
"closed")
echo "action=remove" >> "$GITHUB_ENV"
;;
*)
echo "unknown event type $event_type; no action to take"
echo "action=none" >> "$GITHUB_ENV"
;;
esac
118 changes: 118 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: Deploy PR previews

on:
workflow_dispatch:
pull_request:
branches: main
types:
- opened
- reopened
- synchronize
- closed

permissions:
contents: read
pages: write
id-token: write
pull-requests: write

concurrency: preview-${{ github.ref }}

env:
INPUT_PATH: "."
PREVIEW_FOLDER: "/pr-preview" # subfolder
pr: ${{ github.event.number }}
OSSI_SITE_TOKEN: ${{ secrets.GITHUB_TOKEN }}
action: auto
preview-branch: main
actionref: ${{ github.action_ref }}
actionrepo: ${{ github.action_repository }}
deployrepo: ${{ github.repository }}

jobs:
build:
runs-on: composite

steps:
- name: Store environment variables
run: |
org=$(echo "${{env.deployrepo}}" | cut -d "/" -f 1)
thirdleveldomain=$(echo "${{env.deployrepo}}" | cut -d "/" -f 2)

if [ "${org}.github.io" == "$thirdleveldomain" ]; then
pagesurl="${org}.github.io"
else
pagesurl=$(echo "${{env.deployrepo}}" | sed 's/\//.github.io\//')
fi
echo "pagesurl=$pagesurl" >> $GITHUB_ENV
echo "PREVIEW_PATH=${{env.PREVIEW_FOLDER}}/pr-${{env.pr}}" >> $GITHUB_ENV
echo "emptydir=$(mktemp -d)" >> $GITHUB_ENV
echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV
shell: bash

- name: Checkout
uses: actions/checkout@v4

- name: Determine auto action
run: .github/actions/determine-auto-action.sh
shell: bash

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: npm
cache-dependency-path: ${{ env.INPUT_PATH }}/package-lock.json

- name: Setup Pages
id: pages
uses: actions/configure-pages@v4

- name: Install dependencies
run: npm ci
working-directory: ${{ env.INPUT_PATH }}

- name: Build with Astro
run: |
npx --no-install astro build \
--site "https://${{ env.pagesurl }}" \
--base "${{ env.PREVIEW_PATH }}"
--outdir "${{env.PREVIEW_PATH}}/dist"
--cachedir "${{env.PREVIEW_PATH}}/node_modules/.astro"
working-directory: ${{ env.INPUT_PATH}}

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: $${{env.PREVIEW_PATH}}/dist

deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: build
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4

- name: Expose deployment URL
id: url
run: echo "url=https://${{ env.pagesurl }}/${{ env.PREVIEW_PATH }}/" >> $GITHUB_OUTPUT
shell: bash

- name: Leave a comment after deployment
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0
with:
header: pr-preview
message: "
:rocket: Deployed preview to
https://${{ env.pagesurl }}/${{ env.PREVIEW_PATH }}/

on branch [`${{ env.preview-branch }}`](\
${{ github.server_url }}/${{ env.deployrepo }}\
/tree/${{ env.preview-branch }})
at ${{ env.datetime }}
"
Empty file added pr-preview/text.txt
Empty file.
8 changes: 6 additions & 2 deletions src/components/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import janeliaLogoBlack from "../assets/images/HHMI_Janelia_Logo-Black.png";
import janeliaLogoWhite from "../assets/images/HHMI_Janelia_Logo-White.png";

const baseUrl = import.meta.env.BASE_URL;
const previewPath = import.meta.env.PREVIEW_PATH
? import.meta.env.PREVIEW_PATH
: "";
---

<footer
Expand All @@ -20,8 +23,9 @@ const baseUrl = import.meta.env.BASE_URL;
<div class="flex justify-between items-start my-6">
<div>
<div class="mb-2">
<a class="inline-block font-bold text-xl" href={baseUrl}
>🔬 Janelia Software</a
<a
class="inline-block font-bold text-xl"
href={`${baseUrl}${previewPath}`}>🔬 Janelia Software</a
>
</div>
<div class="flex flex-col gap-2 text-sm text-muted">
Expand Down
5 changes: 4 additions & 1 deletion src/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import ToggleTheme from "../components/ToggleTheme.astro";
import ToggleTheme from "../components/ToggleTheme.astro";

const baseUrl = import.meta.env.BASE_URL;
const previewPath = import.meta.env.PREVIEW_PATH
? import.meta.env.PREVIEW_PATH
: "";
---

<header
Expand All @@ -14,7 +17,7 @@ const baseUrl = import.meta.env.BASE_URL;
<div class="absolute inset-0 bg-white dark:bg-dark"></div>

<div class="relative text-default p-3 md:px-6 w-full flex justify-between">
<a class="flex items-center" href={baseUrl}>
<a class="flex items-center" href={`${baseUrl}${previewPath}`}>
<span
class="self-center ml-2 rtl:ml-0 rtl:mr-2 text-2xl font-bold text-gray-900 whitespace-nowrap dark:text-white"
>
Expand Down
11 changes: 7 additions & 4 deletions src/data/navigation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const base = import.meta.env.BASE_URL;
const previewPath = import.meta.env.PREVIEW_PATH
? import.meta.env.PREVIEW_PATH
: "";

export const headerData = {
links: [
Expand All @@ -8,19 +11,19 @@ export const headerData = {
},
{
text: "Projects",
href: `${base}/projects`,
href: `${base}${previewPath}/projects`,
},
{
text: "Ecosystems",
href: `${base}/ecosystems`,
href: `${base}${previewPath}/ecosystems`,
},
{
text: "OSSI",
href: `${base}/ossi`,
href: `${base}${previewPath}/ossi`,
},
{
text: "Blog",
href: `${base}/blog`,
href: `${base}${previewPath}/blog`,
},
],
};
Expand Down
11 changes: 7 additions & 4 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import ContributorList from "../components/ContributorList.astro";
import Wrapper from "../components/Wrapper.astro";

const baseUrl = import.meta.env.BASE_URL;
const previewPath = import.meta.env.PREVIEW_PATH
? import.meta.env.PREVIEW_PATH
: "";
---

<BaseLayout>
Expand All @@ -25,11 +28,11 @@ const baseUrl = import.meta.env.BASE_URL;
{
variant: "primary",
text: "Janelia Software Projects",
href: `${baseUrl}/projects`,
href: `${baseUrl}${previewPath}/projects`,
},
{
text: "Software Ecosystems",
href: `${baseUrl}/ecosystems`,
href: `${baseUrl}${previewPath}/ecosystems`,
},
]}
>
Expand All @@ -52,7 +55,7 @@ const baseUrl = import.meta.env.BASE_URL;
{
variant: "primary",
text: "Learn more about OSSI",
href: `${baseUrl}/ossi`,
href: `${baseUrl}${previewPath}/ossi`,
target: "_blank",
},
]}
Expand All @@ -77,7 +80,7 @@ const baseUrl = import.meta.env.BASE_URL;
information="The latest updates on Janelia software projects."
content="blog"
linkText="View all posts"
linkUrl={`${baseUrl}/blog`}
linkUrl={`${baseUrl}${previewPath}/blog`}
/>

<Fragment slot="bg">
Expand Down