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'
48 changes: 48 additions & 0 deletions ci/upload_site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#/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_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)
Copy link
Contributor

Choose a reason for hiding this comment

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

We already have ssh keys setup with a different variable (I believe), so this PR will only use GIT_PR_PREVIEW_PRIVATE_SSH_KEY to push even to production?

I think we should use the production keys for production and preview keys for previews, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have ssh keys setup with a different variable (I believe), so this PR will only use GIT_PR_PREVIEW_PRIVATE_SSH_KEY to push even to production?

It seems there are no ssh keys required to push to production (as we discussed here #1 (comment) and also tried/tested it by removing the SSH key setup step here c6cee87). We currently need the SSH keys for pushing the test version to pull_request_preview (as pull_request_preview is a separate repository). During production deployment, even if the SSH keys would be available, I guess/think that they would (hopefully) not be used.

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have ssh keys setup with a different variable (I believe)

Please, could you possibly share if it would be possible to rename it and setup its public key at https://github.com/lfortran/pull_request_preview, please

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. It's a little weird that it would quit to push to production if GIT_PR_PREVIEW_PRIVATE_SSH_KEY is empty. But I guess that's fine. Would it make sense to put this into if [[ ${git_ref} != "refs/heads/main" ]]; then if statement?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, got it.

Shaikh-Ubaid marked this conversation as resolved.
Show resolved Hide resolved

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