|
| 1 | +name: Chatops |
| 2 | +on: [issue_comment] |
| 3 | + |
| 4 | +jobs: |
| 5 | + trigger-chatops: |
| 6 | + if: (github.event.issue.pull_request != null) && contains(github.event.comment.body, '/preview') && (github.repository == 'fastai/fastpages') |
| 7 | + env: |
| 8 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 9 | + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
| 10 | + CHECK_RUN_NAME: "Draft-Site-Build" |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + |
| 14 | + - name: see payload |
| 15 | + run: | |
| 16 | + echo "FULL PAYLOAD:\n${PAYLOAD}\n" |
| 17 | + echo "PR_PAYLOAD PAYLOAD:\n${PR_PAYLOAD}" |
| 18 | + env: |
| 19 | + PAYLOAD: ${{ toJSON(github.event) }} |
| 20 | + PR_PAYLOAD: ${{ github.event.pull_request }} |
| 21 | + |
| 22 | + - name: verify env exists |
| 23 | + id: get_status |
| 24 | + run: | |
| 25 | + if [ -z ${NETLIFY_AUTH_TOKEN} ]; then echo "::set-output name=status::public"; else echo "::set-output name=status::private"; fi |
| 26 | +
|
| 27 | + - name: make comment on PR if env does not exist |
| 28 | + if: steps.get_status.outputs.status == 'public' |
| 29 | + run: | |
| 30 | + ./_action_files/pr_comment.sh "Was not able to generate site preview due to absent credentials." |
| 31 | + env: |
| 32 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + ISSUE_NUMBER: ${{ github.event.issue.number }} |
| 34 | + |
| 35 | + - name: Fetch context about the PR that has been commented on |
| 36 | + id: chatops |
| 37 | + uses: machine-learning-apps/actions-chatops@master |
| 38 | + with: |
| 39 | + TRIGGER_PHRASE: "/preview" |
| 40 | + env: # you must supply GITHUB_TOKEN |
| 41 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + |
| 43 | + - name: Set up Python |
| 44 | + uses: actions/setup-python@v1 |
| 45 | + with: |
| 46 | + python-version: 3.6 |
| 47 | + |
| 48 | + - name: install requests |
| 49 | + run: pip3 install requests |
| 50 | + |
| 51 | + - name: add check run |
| 52 | + id: create_check |
| 53 | + if: steps.get_status.outputs.status == 'private' |
| 54 | + shell: python |
| 55 | + run: | |
| 56 | + import os, requests |
| 57 | +
|
| 58 | + sha = os.getenv('SHA') |
| 59 | + token = os.getenv('GITHUB_TOKEN') |
| 60 | + nwo = os.getenv('GITHUB_REPOSITORY') |
| 61 | + name = os.getenv('CHECK_RUN_NAME') |
| 62 | +
|
| 63 | + url = f'https://api.github.com/repos/{nwo}/check-runs' |
| 64 | +
|
| 65 | + headers = {'authorization': f'token {token}', |
| 66 | + 'accept': 'application/vnd.github.antiope-preview+json'} |
| 67 | +
|
| 68 | + payload = { |
| 69 | + 'name': f'{name}', |
| 70 | + 'head_sha': f'{sha}', |
| 71 | + 'status': 'in_progress', |
| 72 | + 'output':{ |
| 73 | + 'title': f'Building preview of site for {sha}.', |
| 74 | + 'summary': ' ', |
| 75 | + 'text': ' ' |
| 76 | + }, |
| 77 | + } |
| 78 | + response = requests.post(url=url, headers=headers, json=payload) |
| 79 | + print(response) |
| 80 | + id = response.json()['id'] |
| 81 | + print(f"::set-output name=id::{id}") |
| 82 | + env: |
| 83 | + SHA: ${{ steps.chatops.outputs.SHA }} |
| 84 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + |
| 86 | + - name: add label |
| 87 | + if: steps.get_status.outputs.status == 'private' |
| 88 | + run: | |
| 89 | + import os, requests |
| 90 | + nwo = os.getenv('GITHUB_REPOSITORY') |
| 91 | + token = os.getenv('GITHUB_TOKEN') |
| 92 | + pr_num = os.getenv('PR_NUM') |
| 93 | + headers = {'Accept': 'application/vnd.github.symmetra-preview+json', |
| 94 | + 'Authorization': f'token {token}'} |
| 95 | + url = f"https://api.github.com/repos/{nwo}/issues/{pr_num}/labels" |
| 96 | + data = {"labels": ["draft build pending"]} |
| 97 | + result = requests.post(url=url, headers=headers, json=data) |
| 98 | + # assert response.status_code == 201, f"Received status code of {response.status_code}" |
| 99 | + print(result) |
| 100 | + shell: python |
| 101 | + env: |
| 102 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + PR_NUM: ${{ steps.chatops.outputs.PULL_REQUEST_NUMBER }} |
| 104 | + GITHUB_REPOSITORY: $GITHUB_REPOSITORY |
| 105 | + |
| 106 | + - name: Copy The PR's Branch Repository Contents |
| 107 | + uses: actions/checkout@main |
| 108 | + if: steps.get_status.outputs.status == 'private' |
| 109 | + with: |
| 110 | + ref: ${{ steps.chatops.outputs.SHA }} |
| 111 | + |
| 112 | + - name: convert notebooks and word docs to posts |
| 113 | + uses: ./ # use the code in this repo to instead of fastai/fastpages@master |
| 114 | + |
| 115 | + - name: setup directories for Jekyll build |
| 116 | + if: steps.get_status.outputs.status == 'private' |
| 117 | + run: | |
| 118 | + rm -rf _site |
| 119 | + sudo chmod -R 777 . |
| 120 | + |
| 121 | + - name: Jekyll build with baseurl as root for netifly |
| 122 | + if: steps.get_status.outputs.status == 'private' |
| 123 | + uses: docker://fastai/fastpages-jekyll |
| 124 | + with: |
| 125 | + args: bash -c "jekyll build" |
| 126 | + |
| 127 | + - name: deploy to netlify |
| 128 | + if: steps.get_status.outputs.status == 'private' |
| 129 | + id: py |
| 130 | + run: | |
| 131 | + sudo npm install -g --unsafe-perm=true netlify-cli |
| 132 | + netlify deploy --dir _site | tee _netlify_logs.txt |
| 133 | + cat _netlify_logs.txt | python _action_files/parse_netlify.py |
| 134 | +
|
| 135 | + - name: make comment on PR |
| 136 | + if: steps.get_status.outputs.status == 'private' |
| 137 | + run: | |
| 138 | + MSG="A preview build of this branch has been generated for SHA: $SHA and can be viewed **live** at: ${URL}\n\nThe current fastpages site built from master can be viewed for comparison [here](https://fastpages.fast.ai/)" |
| 139 | + echo "$MSG" |
| 140 | + ./_action_files/pr_comment.sh "${MSG}" |
| 141 | + env: |
| 142 | + URL: ${{ steps.py.outputs.draft_url }} |
| 143 | + SHA: ${{ steps.chatops.outputs.SHA }} |
| 144 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 145 | + ISSUE_NUMBER: ${{ github.event.issue.number }} |
| 146 | + |
| 147 | + - name: remove label |
| 148 | + if: always() |
| 149 | + run: | |
| 150 | + import os, requests |
| 151 | + nwo = os.getenv('GITHUB_REPOSITORY') |
| 152 | + token = os.getenv('GITHUB_TOKEN') |
| 153 | + pr_num = os.getenv('PR_NUM') |
| 154 | + headers = {'Accept': 'application/vnd.github.symmetra-preview+json', |
| 155 | + 'Authorization': f'token {token}'} |
| 156 | + url = f"https://api.github.com/repos/{nwo}/issues/{pr_num}/labels/draft%20build%20pending" |
| 157 | + result = requests.delete(url=url, headers=headers) |
| 158 | + print(result) |
| 159 | + shell: python |
| 160 | + env: |
| 161 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 162 | + PR_NUM: ${{ steps.chatops.outputs.PULL_REQUEST_NUMBER }} |
| 163 | + GITHUB_REPOSITORY: $GITHUB_REPOSITORY |
| 164 | + |
| 165 | + # defensively clear check run each time |
| 166 | + - name: clear check run |
| 167 | + if: always() |
| 168 | + continue-on-error: true |
| 169 | + shell: python |
| 170 | + run: | |
| 171 | + import os, requests |
| 172 | +
|
| 173 | + sha = os.getenv('SHA') |
| 174 | + conclusion = os.getenv('WORKFLOW_CONCLUSION').lower() |
| 175 | + token = os.getenv('GITHUB_TOKEN') |
| 176 | + nwo = os.getenv('GITHUB_REPOSITORY') |
| 177 | + check_run_id = os.getenv('CHECK_RUN_ID') |
| 178 | + if not check_run_id: |
| 179 | + quit() |
| 180 | + |
| 181 | + url = f'https://api.github.com/repos/{nwo}/check-runs/{check_run_id}' |
| 182 | + headers = {'authorization': f'token {token}', |
| 183 | + 'accept': 'application/vnd.github.antiope-preview+json'} |
| 184 | + |
| 185 | + data = { |
| 186 | + 'conclusion': f'{conclusion}', |
| 187 | + } |
| 188 | + response = requests.patch(url=url, headers=headers, json=data) |
| 189 | + print(response) |
| 190 | + env: |
| 191 | + SHA: ${{ steps.chatops.outputs.SHA }} |
| 192 | + WORKFLOW_CONCLUSION: ${{ job.status }} |
| 193 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 194 | + CHECK_RUN_ID: ${{ steps.create_check.outputs.id }} |
| 195 | + |
0 commit comments