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
Merged

CI: Support PR Preview #38

merged 12 commits into from
Sep 9, 2022

Conversation

Shaikh-Ubaid
Copy link
Member

This PR adds support of previewing a pull request. The built site is currently pushed into gh-pages branch at https://github.com/Shaikh-Ubaid/pull_request_preview.

Example PR: https://github.com/Shaikh-Ubaid/lcompilers_frontend/pull/2

Update links in pr_preview workflow

CI: Merge steps checkout, commit and deploy

CI: cd into pr_preview

CI: Use global user

CI: Use deploy_repo_push link to deploy

CI: Push to main branch

CI: Define a separate upload_site.sh file
@Shaikh-Ubaid Shaikh-Ubaid requested a review from certik August 21, 2022 22:22
@Shaikh-Ubaid
Copy link
Member Author

It seems the Create comment about deployment step failed. I guess the GitHub actions bot is unable to comment as the CI is not yet a part of master or because the CI might not be running in its own GitHub remote. Since, the comment works at https://github.com/Shaikh-Ubaid/lcompilers_frontend/pull/2, I am hoping it would work at https://github.com/lfortran/lcompilers_frontend as well (I guess the pull request might need to be created at/from origin/feature_branch).

@Shaikh-Ubaid
Copy link
Member Author

This is ready. Please possibly review and please share feedback.

@certik
Copy link
Contributor

certik commented Aug 22, 2022

I would reuse the workflow that we have: the main purpose of the preview build is to ensure that it will work in production. For that we need to use the exact same steps, including checking out any production repositories etc. Only at the very end, instead of pushing into production, we push into the test repository.

@Shaikh-Ubaid Shaikh-Ubaid force-pushed the ci_pr_preview branch 3 times, most recently from 35d4e79 to 52e70e3 Compare August 22, 2022 09:01
@Shaikh-Ubaid
Copy link
Member Author

This is ready. Please possibly review and please share feedback.

@Shaikh-Ubaid Shaikh-Ubaid marked this pull request as draft August 22, 2022 09:13
@Shaikh-Ubaid
Copy link
Member Author

I transferred the pull_request_preview repository here https://github.com/lfortran/pull_request_preview

Comment on lines 7 to 15
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/Shaikh-Ubaid/pull_request_preview.git"
deploy_repo_push="[email protected]:Shaikh-Ubaid/pull_request_preview.git"
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not:

Suggested change
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/Shaikh-Ubaid/pull_request_preview.git"
deploy_repo_push="[email protected]:Shaikh-Ubaid/pull_request_preview.git"
fi
deploy_repo_pull="https://github.com/lfortran/lcompilers_frontend.git"
if [[ ${git_ref} == "refs/heads/main" ]]; then
# Production version - pipeline triggered from main branch
deploy_repo_push="[email protected]:lfortran/lcompilers_frontend.git"
else
# Test version - pipeline triggered from pull request
deploy_repo_push="[email protected]:Shaikh-Ubaid/pull_request_preview.git"
fi

Copy link
Member Author

Choose a reason for hiding this comment

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

doubt: Please, could you possibly share if the above approach could lead to errors such as:

(Not an exact error, just an example)

Failed to push refs: The repo is x commits ahead, use git pull

Copy link
Member Author

Choose a reason for hiding this comment

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

If we pull the pull_request_preview and then update/push to it, I guess, we are (hopefully) avoiding or reducing the chances of Failed to push refs error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. I think in the past I did force push, but force pushing to a production repository might not be a good practice, since it can hide issues.

How are different PRs handled, or do all PRs push into just master, so they overwrite each other?

Copy link
Member Author

Choose a reason for hiding this comment

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

How are different PRs handled, or do all PRs push into just master, so they overwrite each other?

Yes, it seems they would overwrite each other.

@Shaikh-Ubaid
Copy link
Member Author

I updated the links to point to the new repository at https://github.com/lfortran/pull_request_preview

@Shaikh-Ubaid
Copy link
Member Author

Ondrej Sir, please, could you possibly share if it would be possible for you to setup the needed SSH Keys, please?

@Shaikh-Ubaid
Copy link
Member Author

Shaikh-Ubaid commented Aug 22, 2022

Also, I updated the preview link here 893812e to https://preview.dev.lfortran.org. Please, could someone possibly share if the link is fine and if it is possible to use it?

@Shaikh-Ubaid
Copy link
Member Author

Shaikh-Ubaid commented Aug 22, 2022

Also, I updated the preview link here 893812e to https://preview.dev.lfortran.org/. Please, could someone possibly share if the link is fine and if it is possible to use it?

If we use a link which has some path after domain (for example: https://dev.lfortran.org/pull_request_preview), we would be needing to update the basepath as in here 0e80d77. It seems updating the basepath might be inconsistent with the production version which is deployed to https://dev.lfortran.org/ (which has no basepath).

@Shaikh-Ubaid Shaikh-Ubaid marked this pull request as ready for review August 22, 2022 09:37
@Shaikh-Ubaid
Copy link
Member Author

This is ready. Please possibly review and please share feedback.

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)

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.

@certik
Copy link
Contributor

certik commented Aug 22, 2022

I setup the keys.

I think we should do it to serve in a new branch, such as pr-38 for this PR. That way we can have more than one open PR and it will not clash.

Regarding the prefix, so that it does not clash, I think that's fine, if it can be done in some clean (hard to break) way. Usually if it works with a prefix, hopefully it will work with an empty prefix too.

@Shaikh-Ubaid
Copy link
Member Author

Shaikh-Ubaid commented Aug 22, 2022

I think we should do it to serve in a new branch, such as pr-38 for this PR. That way we can have more than one open PR and it will not clash.

It seems GitHub Pages allow setting only a single branch for deployment. So, it seems everytime we might need to change the branch of deployment.

@certik
Copy link
Contributor

certik commented Aug 22, 2022

The way to do it is to use a prefix directory with the PR number.

@Shaikh-Ubaid
Copy link
Member Author

The way to do it is to use a prefix directory with the PR number.

Got it. Thank you so much for the guidance.

@Shaikh-Ubaid
Copy link
Member Author

I came across nf-core/tools#765 and https://securitylab.github.com/research/github-actions-preventing-pwn-requests/. It seems to me that we can support previewing pull requests even when the PRs are submitted from a fork.

@Shaikh-Ubaid
Copy link
Member Author

Shaikh-Ubaid commented Aug 22, 2022

I came across nf-core/tools#765 and https://securitylab.github.com/research/github-actions-preventing-pwn-requests/. It seems to me that we can support previewing pull requests even when the PRs are submitted from a fork.

I would like to try this out. Please, could you possibly share if we shall create three separate workflows as follows:

  • first workflow runs only on push to main branch event
    • It is the same one that we currently have in the master
  • second workflow runs only on pull request event
    • It runs in an unprivileged environment and builds the project and uploads the built files
  • third workflow runs only on after completion of the second workflow
    • It runs in a privileged environment
    • It downloads the built files of the second workflow and pushes them to the pull_request_preview repository
    • It comments on the pull request with the deployment link

Notes:

  • unprivileged environment means the workflow does not have access to secrets/tokens.
  • privileged environment means the workflow has access to secrets/tokens.

@Shaikh-Ubaid Shaikh-Ubaid marked this pull request as draft August 22, 2022 18:37
ci/upload_site.sh Outdated Show resolved Hide resolved
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

I think this is fine. When you merge, carefully watch the builds, and test the final site. If something goes wrong, then first revert the deploy commit in the deploy repo to fix the site to the previous state, then try to fix it with new PRs.

@Shaikh-Ubaid
Copy link
Member Author

The way to do it is to use a prefix directory with the PR number.

Ondrej Sir, it seems, the workflow currently overwrites the gh-pages branch for every change in different pull-requests.

@Shaikh-Ubaid
Copy link
Member Author

I think this is fine. When you merge, carefully watch the builds, and test the final site. If something goes wrong, then first revert the deploy commit in the deploy repo to fix the site to the previous state, then try to fix it with new PRs.

Got it.

I looked into the changes of this pull request again and they seem fine to me. Since, this seems to be approved, I am merging this PR for now.

@Shaikh-Ubaid Shaikh-Ubaid marked this pull request as ready for review September 9, 2022 15:02
@Shaikh-Ubaid Shaikh-Ubaid merged commit 5a977ed into lfortran:main Sep 9, 2022
@Shaikh-Ubaid
Copy link
Member Author

The CI failed to push to gh-pages branch of https://github.com/lfortran/lcompilers_frontend. It seems that since we used the https url to clone the repository https://github.com/lfortran/lcompilers_frontend, it is needing ssh keys to push to the same repository.

I am submitting a new PR which would use the ssh url for the same repository https://github.com/lfortran/lcompilers_frontend for the commit in the main branch.

Since, the push to gh-pages branch of https://github.com/lfortran/lcompilers_frontend failed, the site should be hopefully intact. It also seems to be.

@Shaikh-Ubaid
Copy link
Member Author

I am submitting a new PR which would use the ssh url for the same repository https://github.com/lfortran/lcompilers_frontend for the commit in the main branch.

I fixed the CI failure issue while pushing to gh-pages branch of https://github.com/lfortran/lcompilers_frontend.

Also, while updating the latest_commit, I fixed the issues discovered related to this pull_request_preview feature in #44.

@certik
Copy link
Contributor

certik commented Sep 11, 2022

Thanks for fixing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants