Skip to content

Commit

Permalink
feat: support multiple preview jobs (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Aug 20, 2020
1 parent 7121e90 commit be3e3c7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 29 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ jobs:
npm install
npm run build
dist: public
preview2:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
name: test afc163/surge-preview
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
build: |
npm install
npm run build
dist: public
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A GitHub action that preview website in [surge.sh](https://surge.sh/) for your p
### Compare to Netlify/Vercel?

- It is **free**.
- It supports multiple preview jobs.

### Usage

Expand All @@ -34,7 +35,46 @@ jobs:
dist: public
```

The preview website url will be `https://{{repository.owner}}-{{repository.name}}-pr-{{pr.number}}.surge.sh`.
The preview website url will be `https://{{repository.owner}}-{{repository.name}}-{{actionName}}-pr-{{pr.number}}.surge.sh`.

#### Multiple Jobs

```yaml
name: 🔂 Surge PR Preview

on: [push, pull_request]

jobs:
preview-job-1:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: afc163/surge-preview@v1
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
build: |
npm install
npm run build
dist: public
preview-job-2:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: afc163/surge-preview@v1
with:
surge_token: ${{ secrets.SURGE_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
build: |
npm install
npm run build
dist: public
```

The preview website urls will be:

- `https://{{repository.owner}}-{{repository.name}}-preview-job-1-pr-{{pr.number}}.surge.sh`
- `https://{{repository.owner}}-{{repository.name}}-preview-job-2-pr-{{pr.number}}.surge.sh`

### Inputs

Expand Down
31 changes: 18 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Octokit, Repo } from './commentToPullRequest';

function headerComment(header = ': Surge Preview') {
return `<!-- Sticky Pull Request Comment${header} -->`;
function headerComment(header?: string) {
return `<!-- Sticky Pull Request Comment${header || ''} -->`;
}

export async function findPreviousComment(
Expand Down
21 changes: 18 additions & 3 deletions src/commentToPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,42 @@ interface CommentConfig {
number: number;
message: string;
octokit: Octokit;
header: string;
}

export async function comment({
repo,
number,
message,
octokit,
header,
}: CommentConfig) {
if (isNaN(number) || number < 1) {
core.info('no numbers given: skip step');
return;
}
const prefixedHeader = `: Surge Preview ${header}'`;

try {
const previous = await findPreviousComment(octokit, repo, number);
const previous = await findPreviousComment(
octokit,
repo,
number,
prefixedHeader
);
const body = message;

if (previous) {
await updateComment(octokit, repo, previous.id, body, undefined, false);
await updateComment(
octokit,
repo,
previous.id,
body,
prefixedHeader,
false
);
} else {
await createComment(octokit, repo, number, body);
await createComment(octokit, repo, number, body, prefixedHeader);
}
} catch (err) {
core.setFailed(err.message);
Expand Down
20 changes: 11 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ async function main() {
const dist = core.getInput('dist');
const octokit = github.getOctokit(token);
let prNumber: number | undefined;
core.debug('github.context.payload');
core.debug(JSON.stringify(github.context.payload, null, 2));
const gitCommitSha = github.context.payload.after;
if (github.context.payload.number && github.context.payload.pull_request) {
prNumber = github.context.payload.number;
core.debug('github.context');
core.debug(JSON.stringify(github.context, null, 2));
const { job, payload } = github.context;
const gitCommitSha = payload.after;
if (payload.number && payload.pull_request) {
prNumber = payload.number;
} else {
const result = await octokit.repos.listPullRequestsAssociatedWithCommit({
owner: github.context.repo.owner,
Expand All @@ -36,7 +37,7 @@ async function main() {
core.info(`Find PR number: ${prNumber}`);
const repoOwner = github.context.repo.owner.replace(/\./g, '-');
const repoName = github.context.repo.repo.replace(/\./g, '-');
const url = `${repoOwner}-${repoName}-pr-${prNumber}.surge.sh`;
const url = `${repoOwner}-${repoName}-${job}-pr-${prNumber}.surge.sh`;

const { data } = await octokit.checks.listForRef({
owner: github.context.repo.owner,
Expand All @@ -49,9 +50,7 @@ async function main() {
// 尝试获取 check_run_id,逻辑不是很严谨
let checkRunId;
if (data?.check_runs?.length >= 0) {
const checkRun = data?.check_runs?.find((item) =>
item.name.includes('preview')
);
const checkRun = data?.check_runs?.find((item) => item.name === job);
checkRunId = checkRun?.id;
}

Expand All @@ -69,6 +68,7 @@ async function main() {
<sub>🤖 By [surge-preview](https://github.com/afc163/surge-preview)</sub>
`,
octokit,
header: job,
});

const startTime = Date.now();
Expand Down Expand Up @@ -100,6 +100,7 @@ async function main() {
<sub>🤖 By [surge-preview](https://github.com/afc163/surge-preview)</sub>
`,
octokit,
header: job,
});
} catch (err) {
comment({
Expand All @@ -113,6 +114,7 @@ async function main() {
<sub>🤖 By [surge-preview](https://github.com/afc163/surge-preview)</sub>
`,
octokit,
header: job,
});
core.setFailed(err.message);
}
Expand Down

0 comments on commit be3e3c7

Please sign in to comment.