Skip to content

Commit c4301b0

Browse files
Merge pull request #50 from layer5io/revert-49-build-preview-fix
Revert "clean up CI actions"
2 parents 9643606 + 2b1fb7d commit c4301b0

File tree

5 files changed

+139
-201
lines changed

5 files changed

+139
-201
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.7
2+
3+
RUN pip install httpx "pydantic==1.5.1" pygithub
4+
5+
COPY ./app /app
6+
7+
CMD ["python", "/app/main.py"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Comment Preview in PR
2+
description: Comment the preview URL in the PR
3+
author: Sebastián Ramírez <[email protected]>
4+
inputs:
5+
token:
6+
description: Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}
7+
required: true
8+
deploy_url:
9+
description: The deployment URL to comment in the PR
10+
required: true
11+
runs:
12+
using: docker
13+
image: Dockerfile
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import logging
2+
import sys
3+
from pathlib import Path
4+
from typing import Optional
5+
6+
import httpx
7+
from github import Github
8+
from github.PullRequest import PullRequest
9+
from pydantic import BaseModel, BaseSettings, SecretStr, ValidationError
10+
11+
github_api = "https://api.github.com"
12+
13+
14+
class Settings(BaseSettings):
15+
github_repository: str
16+
github_event_path: Path
17+
github_event_name: Optional[str] = None
18+
input_token: SecretStr
19+
input_deploy_url: str
20+
21+
22+
class PartialGithubEventHeadCommit(BaseModel):
23+
id: str
24+
25+
26+
class PartialGithubEventWorkflowRun(BaseModel):
27+
head_commit: PartialGithubEventHeadCommit
28+
29+
30+
class PartialGithubEvent(BaseModel):
31+
workflow_run: PartialGithubEventWorkflowRun
32+
33+
34+
if __name__ == "__main__":
35+
logging.basicConfig(level=logging.INFO)
36+
settings = Settings()
37+
logging.info(f"Using config: {settings.json()}")
38+
g = Github(settings.input_token.get_secret_value())
39+
repo = g.get_repo(settings.github_repository)
40+
try:
41+
event = PartialGithubEvent.parse_file(settings.github_event_path)
42+
except ValidationError as e:
43+
logging.error(f"Error parsing event file: {e.errors()}")
44+
sys.exit(0)
45+
use_pr: Optional[PullRequest] = None
46+
for pr in repo.get_pulls():
47+
if pr.head.sha == event.workflow_run.head_commit.id:
48+
use_pr = pr
49+
break
50+
if not use_pr:
51+
logging.error(
52+
f"No PR found for hash: {event.workflow_run.head_commit.id}"
53+
)
54+
sys.exit(0)
55+
github_headers = {
56+
"Authorization": f"token {settings.input_token.get_secret_value()}"
57+
}
58+
url = f"{github_api}/repos/{settings.github_repository}/issues/{use_pr.number}/comments"
59+
logging.info(f"Using comments URL: {url}")
60+
response = httpx.post(
61+
url,
62+
headers=github_headers,
63+
json={
64+
"body": f"🚀 Preview for commit {use_pr.head.sha} at: {settings.input_deploy_url}"
65+
},
66+
)
67+
if not (200 <= response.status_code <= 300):
68+
logging.error(f"Error posting comment: {response.text}")
69+
sys.exit(1)
70+
logging.info("Finished")
Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
name: Build Preview Site
1+
name: Build and Preview Site
22
on:
33
pull_request:
44
branches: [master]
55
types: [opened, synchronize, reopened, closed]
66

77
concurrency:
8-
group: pr-preview-build-${{ github.event.pull_request.number }}
8+
group: ${{ github.workflow }}-${{ github.ref }}
99
cancel-in-progress: true
10-
11-
# Only read permissions for building (safe for forks)
10+
1211
permissions:
13-
contents: read
12+
contents: write
13+
pull-requests: write
1414

1515
jobs:
1616
build:
@@ -37,23 +37,52 @@ jobs:
3737
bash .github/workflows/script.sh
3838
3939
- name: Upload files
40-
uses: actions/upload-artifact@v4
40+
uses: actions/upload-artifact@master
4141
with:
42-
name: public-dir-pr-${{ github.event.pull_request.number }}
42+
name: public-dir
4343
path: public-dir.zip
4444
retention-days: 1
45-
46-
# Save PR metadata for the deploy workflow
47-
- name: Save PR metadata
45+
46+
preview:
47+
needs: build
48+
runs-on: ubuntu-latest
49+
if: github.event_name == 'pull_request'
50+
permissions:
51+
contents: write
52+
pull-requests: write
53+
steps:
54+
- name: Checkout 🛎️
55+
uses: actions/checkout@v6
56+
57+
- name: Download pre-built site
58+
if: github.event_name == 'pull_request'
59+
uses: actions/download-artifact@v4
60+
with:
61+
name: public-dir
62+
path: .
63+
64+
- name: Extract site
65+
if: github.event_name == 'pull_request'
4866
run: |
49-
mkdir -p ./pr-metadata
50-
echo "${{ github.event.pull_request.number }}" > ./pr-metadata/pr_number
51-
echo "${{ github.event.action }}" > ./pr-metadata/pr_action
52-
echo "${{ github.event.pull_request.head.sha }}" > ./pr-metadata/pr_sha
53-
54-
- name: Upload PR metadata
55-
uses: actions/upload-artifact@v4
67+
unzip -q public-dir.zip
68+
# Ensure the extracted folder has the static files in a 'public' dir for the action
69+
if [ -d "public-dir" ] && [ ! -d "public" ]; then
70+
mv public-dir public
71+
elif [ ! -d "public" ]; then
72+
echo "Warning: Neither public nor public-dir found after extraction"
73+
fi
74+
75+
- name: Deploy Preview
76+
if: github.event_name == 'pull_request' && github.event.action != 'closed'
77+
uses: rossjrw/[email protected]
78+
with:
79+
source-dir: public
80+
token: ${{ secrets.GITHUB_TOKEN }}
81+
82+
- name: Remove Preview on Close
83+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
84+
uses: rossjrw/[email protected]
5685
with:
57-
name: pr-metadata-${{ github.event.pull_request.number }}
58-
path: pr-metadata/
59-
retention-days: 1
86+
source-dir: public
87+
remove: true
88+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/deploy-preview.yml

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)