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

CI: Support PR Preview #38

Merged
merged 12 commits into from
Sep 9, 2022
38 changes: 15 additions & 23 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
branches: [ "main" ]
repository_dispatch:
types: deploy
pull_request:
branches:
- main

jobs:
build:
Expand Down Expand Up @@ -43,28 +46,17 @@ jobs:
run: |
mv out deploy
- name: GIT Setup Config
- name: Upload Site
shell: bash -l {0}
run: |
git config user.email "noreply@deploy"
git config user.name "Deploy"
- name: GIT Checkout gh-pages and Add Deploy files
shell: bash -l {0}
run: |
cp deploy/ ../deploy -r
git reset --hard
git fetch origin
git checkout gh-pages
rm -rf *
mv ../deploy/* .
run: ci/upload_site.sh
env:
GIT_PR_PREVIEW_PRIVATE_SSH_KEY: ${{ secrets.GIT_PR_PREVIEW_PRIVATE_SSH_KEY }}

- name: GIT Add and Commit
shell: bash -l {0}
run: |
git add .
COMMIT_MESSAGE="Deploying on $(date "+%Y-%m-%d %H:%M:%S")"
git commit -m "${COMMIT_MESSAGE}"
- name: GIT Deploy
run: git push origin gh-pages
- name: Create Deployment Comment
if: ${{ github.ref != 'refs/heads/main' && github.event.pull_request.head.repo.name == github.repository }}
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
Your site is deployed at https://preview.dev.lfortran.org/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how subdomains work --- we can also just use regular github pages, to make it clear it is just a PR preview.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how subdomains work --- we can also just use regular github pages, to make it clear it is just a PR preview.

It seems when we use the regular github pages, the site would be available at https://lfortran.github.io/pull_request_preview and thus adding the path /pull_request_preview to the url. Supporting the path might be challenging as I shared here #38 (comment)

reactions: 'rocket'
50 changes: 50 additions & 0 deletions ci/upload_site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#/usr/bin/bash

set -ex

git_ref=${GITHUB_REF}

if [[ ${git_ref} == "refs/heads/main" ]]; then
# Production version - pipeline triggered from main branch
deploy_repo_pull="https://github.com/lfortran/lcompilers_frontend.git"
deploy_repo_push="[email protected]:lfortran/lcompilers_frontend.git"
else
# Test version - pipeline triggered from pull request
deploy_repo_pull="https://github.com/lfortran/pull_request_preview.git"
deploy_repo_push="[email protected]:lfortran/pull_request_preview.git"
fi


mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keyscan github.com >> ~/.ssh/known_hosts
eval "$(ssh-agent -s)"

D=`pwd`

mkdir $HOME/repos
cd $HOME/repos

git clone ${deploy_repo_pull} pr_preview
cd pr_preview
git fetch origin
git checkout gh-pages
rm -rf *
mv $D/deploy/* .

git config user.email "noreply@deploy"
git config user.name "Deploy"

git add .
COMMIT_MESSAGE="Deploying on $(date "+%Y-%m-%d %H:%M:%S")"
git commit -m "${COMMIT_MESSAGE}"

if [[ ${git_ref} != "refs/heads/main" ]]; then
if [[ "${GIT_PR_PREVIEW_PRIVATE_SSH_KEY}" == "" ]]; then
echo "Note: GIT_PR_PREVIEW_PRIVATE_SSH_KEY is empty, skipping..."
exit 0
fi
ssh-add <(echo "$GIT_PR_PREVIEW_PRIVATE_SSH_KEY" | base64 -d)
fi

git push ${deploy_repo_push} gh-pages:gh-pages