|
| 1 | +name: Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + |
| 6 | +jobs: |
| 7 | + preview: |
| 8 | + name: Run preview |
| 9 | + runs-on: ubuntu-latest |
| 10 | + env: |
| 11 | + PREVIEW_HOSTNAME: ep-preview.click |
| 12 | + GITHUB_BRANCH_NAME: ${{ github.head_ref || github.ref_name }} |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Set timestamp for build/deploy |
| 19 | + run: echo "TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV |
| 20 | + |
| 21 | + - name: Set up pnpm |
| 22 | + uses: pnpm/action-setup@v4 |
| 23 | + with: |
| 24 | + run_install: false |
| 25 | + |
| 26 | + - name: Set up Node.js |
| 27 | + uses: actions/setup-node@v4 |
| 28 | + with: |
| 29 | + node-version: 20 |
| 30 | + cache: "pnpm" |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: make install |
| 34 | + |
| 35 | + - name: Build the website |
| 36 | + run: make build |
| 37 | + |
| 38 | + - name: Set up SSH key |
| 39 | + uses: webfactory/ssh-agent@v0.9.0 |
| 40 | + with: |
| 41 | + ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }} |
| 42 | + |
| 43 | + - name: Get current branch name |
| 44 | + run: | |
| 45 | + BRANCH_NAME=$(make safe_branch BRANCH=$GITHUB_BRANCH_NAME) |
| 46 | + echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV |
| 47 | +
|
| 48 | + - name: ssh keyscan |
| 49 | + run: ssh-keyscan "static.europython.eu" > ~/.ssh/known_hosts |
| 50 | + |
| 51 | + - name: Upload preview |
| 52 | + run: make preview BRANCH=$GITHUB_BRANCH_NAME |
| 53 | + |
| 54 | + - name: Update PR Comment |
| 55 | + uses: actions/github-script@v6 |
| 56 | + if: github.event_name == 'pull_request' |
| 57 | + |
| 58 | + with: |
| 59 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + script: | |
| 61 | + console.log("Hello world!"); |
| 62 | + const pr_id = ${{ github.event.number }}; |
| 63 | + console.log("PR Id %d", pr_id); |
| 64 | +
|
| 65 | + comments = await github.paginate(github.rest.issues.listComments, { |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + issue_number: Number(pr_id) |
| 69 | + }) |
| 70 | +
|
| 71 | + const preview_identifier = "# Preview available" |
| 72 | +
|
| 73 | + let comment_id = null; |
| 74 | + comments.forEach(comment => { |
| 75 | + if(comment.body.indexOf(preview_identifier) >= 0) { |
| 76 | + comment_id = comment.id; |
| 77 | + } |
| 78 | + }); |
| 79 | +
|
| 80 | + const branch_name = process.env.BRANCH_NAME; |
| 81 | + const url = "https://" + branch_name + "." + process.env.PREVIEW_HOSTNAME; |
| 82 | + const timestamp = new Date().toISOString(); |
| 83 | + const header = "\n|Key|Value|\n|---|---|\n" |
| 84 | + const body = preview_identifier + header + "|url|" + url + "|\n|last update|" + timestamp + "|"; |
| 85 | +
|
| 86 | + if(comment_id > 0) { |
| 87 | + await github.rest.issues.updateComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + comment_id: comment_id, |
| 91 | + body: body |
| 92 | + }); |
| 93 | +
|
| 94 | + } else { |
| 95 | +
|
| 96 | + await github.rest.issues.createComment({ |
| 97 | + issue_number: Number(pr_id), |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + body: body |
| 101 | + }); |
| 102 | + } |
0 commit comments