Chatops #1015
This file contains 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: Chatops | |
on: [issue_comment] | |
jobs: | |
trigger-chatops: | |
if: (github.event.issue.pull_request != null) && contains(github.event.comment.body, '/preview') && (github.repository == 'kubeflow/blog') | |
env: | |
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} | |
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} | |
CHECK_RUN_NAME: "Draft-Site-Build" | |
runs-on: ubuntu-latest | |
steps: | |
- name: verify env exists | |
id: get_status | |
run: | | |
if [ -z ${NETLIFY_AUTH_TOKEN} ]; then echo "::set-output name=status::public"; else echo "::set-output name=status::private"; fi | |
- name: make comment on PR if env does not exist | |
if: steps.get_status.outputs.status == 'public' | |
run: | | |
./_action_files/pr_comment.sh "Was not able to generate site preview due to absent credentials." | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ISSUE_NUMBER: ${{ github.event.issue.number }} | |
- name: Fetch context about the PR that has been commented on | |
id: chatops | |
uses: machine-learning-apps/actions-chatops@master | |
with: | |
TRIGGER_PHRASE: "/preview" | |
env: # you must supply GITHUB_TOKEN | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Python | |
uses: actions/setup-python@v1 | |
with: | |
python-version: 3.6 | |
- name: install requests | |
run: pip3 install requests | |
- name: add check run | |
id: create_check | |
if: steps.get_status.outputs.status == 'private' | |
shell: python | |
run: | | |
import os, requests | |
sha = os.getenv('SHA') | |
token = os.getenv('GITHUB_TOKEN') | |
nwo = os.getenv('GITHUB_REPOSITORY') | |
name = os.getenv('CHECK_RUN_NAME') | |
url = f'https://api.github.com/repos/{nwo}/check-runs' | |
headers = {'authorization': f'token {token}', | |
'accept': 'application/vnd.github.antiope-preview+json'} | |
payload = { | |
'name': f'{name}', | |
'head_sha': f'{sha}', | |
'status': 'in_progress', | |
'output':{ | |
'title': f'Building preview of site for {sha}.', | |
'summary': ' ', | |
'text': ' ' | |
}, | |
} | |
response = requests.post(url=url, headers=headers, json=payload) | |
print(response) | |
id = response.json()['id'] | |
print(f"::set-output name=id::{id}") | |
env: | |
SHA: ${{ steps.chatops.outputs.SHA }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: add label | |
if: steps.get_status.outputs.status == 'private' | |
run: | | |
import os, requests | |
nwo = os.getenv('GITHUB_REPOSITORY') | |
token = os.getenv('GITHUB_TOKEN') | |
pr_num = os.getenv('PR_NUM') | |
headers = {'Accept': 'application/vnd.github.symmetra-preview+json', | |
'Authorization': f'token {token}'} | |
url = f"https://api.github.com/repos/{nwo}/issues/{pr_num}/labels" | |
data = {"labels": ["draft build pending"]} | |
result = requests.post(url=url, headers=headers, json=data) | |
# assert response.status_code == 201, f"Received status code of {response.status_code}" | |
print(result) | |
shell: python | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR_NUM: ${{ steps.chatops.outputs.PULL_REQUEST_NUMBER }} | |
GITHUB_REPOSITORY: $GITHUB_REPOSITORY | |
- name: Copy The PR's Branch Repository Contents | |
uses: actions/checkout@master | |
if: steps.get_status.outputs.status == 'private' | |
with: | |
ref: ${{ steps.chatops.outputs.SHA }} | |
- name: convert notebooks and word docs to posts | |
uses: ./_action_files | |
- name: setup directories for Jekyll build | |
if: steps.get_status.outputs.status == 'private' | |
run: | | |
rm -rf _site | |
sudo chmod -R 777 . | |
- name: Jekyll build with baseurl as root for netifly | |
if: steps.get_status.outputs.status == 'private' | |
uses: docker://hamelsmu/fastpages-jekyll | |
with: | |
args: bash -c "gem install bundler && jekyll build" | |
- name: deploy to netlify | |
if: steps.get_status.outputs.status == 'private' | |
id: py | |
run: | | |
sudo npm install netlify-cli -g | |
netlify deploy --dir _site | tee _netlify_logs.txt | |
cat _netlify_logs.txt | python _action_files/parse_netlify.py | |
- name: make comment on PR | |
if: steps.get_status.outputs.status == 'private' | |
run: | | |
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://blog.kubeflow.org/)" | |
echo "$MSG" | |
./_action_files/pr_comment.sh "${MSG}" | |
env: | |
URL: ${{ steps.py.outputs.draft_url }} | |
SHA: ${{ steps.chatops.outputs.SHA }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ISSUE_NUMBER: ${{ github.event.issue.number }} | |
- name: remove label | |
if: always() | |
run: | | |
import os, requests | |
nwo = os.getenv('GITHUB_REPOSITORY') | |
token = os.getenv('GITHUB_TOKEN') | |
pr_num = os.getenv('PR_NUM') | |
headers = {'Accept': 'application/vnd.github.symmetra-preview+json', | |
'Authorization': f'token {token}'} | |
url = f"https://api.github.com/repos/{nwo}/issues/{pr_num}/labels/draft%20build%20pending" | |
result = requests.delete(url=url, headers=headers) | |
print(result) | |
shell: python | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR_NUM: ${{ steps.chatops.outputs.PULL_REQUEST_NUMBER }} | |
GITHUB_REPOSITORY: $GITHUB_REPOSITORY | |
# defensively clear check run each time | |
- name: clear check run | |
if: always() | |
continue-on-error: true | |
shell: python | |
run: | | |
import os, requests | |
sha = os.getenv('SHA') | |
conclusion = os.getenv('WORKFLOW_CONCLUSION').lower() | |
token = os.getenv('GITHUB_TOKEN') | |
nwo = os.getenv('GITHUB_REPOSITORY') | |
check_run_id = os.getenv('CHECK_RUN_ID') | |
if not check_run_id: | |
quit() | |
url = f'https://api.github.com/repos/{nwo}/check-runs/{check_run_id}' | |
headers = {'authorization': f'token {token}', | |
'accept': 'application/vnd.github.antiope-preview+json'} | |
data = { | |
'conclusion': f'{conclusion}', | |
} | |
response = requests.patch(url=url, headers=headers, json=data) | |
print(response) | |
env: | |
SHA: ${{ steps.chatops.outputs.SHA }} | |
WORKFLOW_CONCLUSION: ${{ job.status }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
CHECK_RUN_ID: ${{ steps.create_check.outputs.id }} |