Fix branch name sanitation in preview #1246
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Preview | |
on: | |
pull_request: | |
jobs: | |
preview: | |
if: github.event.pull_request.head.repo.fork == false | |
name: Run preview | |
runs-on: ubuntu-latest | |
env: | |
PREVIEW_HOSTNAME: ep-preview.click | |
GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }} | |
GITHUB_COMMIT_HASH: ${{ github.event.pull_request.head.sha }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Set timestamp for build/deploy | |
run: echo "TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV | |
- name: Set up pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
run_install: false | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
cache: "pnpm" | |
- name: Install dependencies | |
run: make install | |
- name: Build the website | |
env: | |
BRANCH: "${{ env.GITHUB_BRANCH_NAME }}" | |
GIT_VERSION: "${{ env.GITHUB_COMMIT_HASH }}" | |
MODE: "preview" | |
run: make build | |
- name: Set up SSH key | |
uses: webfactory/ssh-agent@v0.9.1 | |
with: | |
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }} | |
- name: ssh keyscan | |
run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts | |
- name: Upload preview | |
env: | |
BRANCH: "${{ env.GITHUB_BRANCH_NAME }}" | |
run: make preview | |
- name: Get safe branch and export to env | |
env: | |
BRANCH: ${{ env.GITHUB_BRANCH_NAME }} | |
run: | | |
echo "SAFE_BRANCH=$(make safe_branch)" >> $GITHUB_ENV | |
- name: Update PR Comment | |
uses: actions/github-script@v7 | |
if: github.event_name == 'pull_request' | |
with: | |
github-token: ${{ secrets.COMMENT_TOKEN }} | |
script: | | |
console.log("Hello world!"); | |
const pr_id = ${{ github.event.number }}; | |
console.log("PR Id %d", pr_id); | |
comments = await github.paginate(github.rest.issues.listComments, { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: Number(pr_id) | |
}) | |
const preview_identifier = "# Preview available" | |
let comment_id = null; | |
comments.forEach(comment => { | |
if(comment.body.indexOf(preview_identifier) >= 0) { | |
comment_id = comment.id; | |
} | |
}); | |
const safe_branch = process.env.SAFE_BRANCH; | |
const url = "https://" + safe_branch + "." + process.env.PREVIEW_HOSTNAME; | |
const timestamp = new Date().toISOString(); | |
const header = "\n|Key|Value|\n|---|---|\n" | |
const body = preview_identifier + header + "|url|" + url + "|\n|last update|" + timestamp + "|"; | |
if(comment_id > 0) { | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: comment_id, | |
body: body | |
}); | |
} else { | |
await github.rest.issues.createComment({ | |
issue_number: Number(pr_id), | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}); | |
} |