|
| 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 | + |
| 13 | + steps: |
| 14 | + |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + sparse-checkout: | |
| 19 | + build |
| 20 | +
|
| 21 | + - name: Set up SSH key |
| 22 | + uses: webfactory/[email protected] |
| 23 | + with: |
| 24 | + ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }} |
| 25 | + |
| 26 | + - name: Get current branch name |
| 27 | + run: | |
| 28 | + BRANCH_NAME=$(make safe_branch) |
| 29 | + echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV |
| 30 | +
|
| 31 | + - name: Upload preview |
| 32 | + run: make preview |
| 33 | + |
| 34 | + - name: Update PR Comment |
| 35 | + uses: actions/github-script@v6 |
| 36 | + if: github.event_name == 'pull_request' |
| 37 | + |
| 38 | + with: |
| 39 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 40 | + script: | |
| 41 | + console.log("Hello world!"); |
| 42 | + const pr_id = ${{ github.event.number }}; |
| 43 | + console.log("PR Id %d", pr_id); |
| 44 | +
|
| 45 | + comments = await github.paginate(github.rest.issues.listComments, { |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + issue_number: Number(pr_id) |
| 49 | + }) |
| 50 | +
|
| 51 | + const preview_identifier = "# Preview available" |
| 52 | +
|
| 53 | + let comment_id = null; |
| 54 | + comments.forEach(comment => { |
| 55 | + if(comment.body.indexOf(preview_identifier) >= 0) { |
| 56 | + comment_id = comment.id; |
| 57 | + } |
| 58 | + }); |
| 59 | +
|
| 60 | + const branch_name = process.env.BRANCH_NAME; |
| 61 | + const url = "https://" + branch_name + "." + process.env.PREVIEW_HOSTNAME; |
| 62 | + const timestamp = new Date().toISOString(); |
| 63 | + const header = "\n|Key|Value|\n|---|---|\n" |
| 64 | + const body = preview_identifier + header + "|url|" + url + "|\n|last update|" + timestamp + "|"; |
| 65 | +
|
| 66 | + if(comment_id > 0) { |
| 67 | + await github.rest.issues.updateComment({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + comment_id: comment_id, |
| 71 | + body: body |
| 72 | + }); |
| 73 | +
|
| 74 | + } else { |
| 75 | +
|
| 76 | + await github.rest.issues.createComment({ |
| 77 | + issue_number: Number(pr_id), |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + body: body |
| 81 | + }); |
| 82 | + } |
0 commit comments