Skip to content
This repository was archived by the owner on Jun 16, 2024. It is now read-only.

Commit 2d43cf2

Browse files
authored
feat: configure local workflow (#1)
* feat: trigger test workflow on PR * feat: adding logic to connect to URL via PR number * feat: adding the ability to adjust the timeout * feat: updating README
1 parent 1ed1b71 commit 2d43cf2

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
on: [push]
1+
on: [pull_request]
22

33
jobs:
4-
hello_world_job:
4+
wait_for_netlify:
55
runs-on: ubuntu-latest
66
name: A job to test this action
77
steps:
8-
# To use this repository's private action, you must check out the repository
98
- name: Checkout
109
uses: actions/checkout@v1
1110
- name: Waiting for 200 response
1211
uses: ./ # Uses an action in the root directory
1312
id: waitFor200
1413
with:
15-
url: "https://jake.partus.ch"
16-
- name: Should timeout for non-existant url
17-
uses: ./ # Uses an action in the root directory
18-
id: waitFor404
19-
with:
20-
url: "https://askldsdjklflkjasdf.com"
14+
site_name: "jakepartusch"
15+
max_timeout: 60

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
# Hello world javascript action
1+
# Wait for Netlify GitHub Action 🎉
22

3-
This action prints "Hello World" or "Hello" + the name of a person to greet to the log.
3+
Do you have other Github actions (Lighthouse, Cypress, etc) that depend on the Netlify Preview URL? This action will wait until the url is available before running the next task.
44

55
## Inputs
66

7-
### `who-to-greet`
7+
### `site_name`
88

9-
**Required** The name of the person to greet. Default `"World"`.
9+
**Required** The name of the Netlify site to reach `https://{site_name}.netlify.com`
1010

11-
## Outputs
11+
### `max_timeout`
1212

13-
### `time`
14-
15-
The time we greeted you.
13+
Optional — The amount of time to spend waiting on Netlify. Defaults to `60` seconds
1614

1715
## Example usage
1816

19-
uses: actions/hello-world-javascript-action@v1
20-
with:
21-
who-to-greet: 'Mona the Octocat'
17+
```
18+
steps:
19+
- name: Waiting for 200 from the Netlify Preview
20+
uses: actions/wait-for-netlify-action@v1
21+
id: waitFor200
22+
with:
23+
site_name: "jakepartusch"
24+
max_timeout: 60
25+
```

index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@ const core = require("@actions/core");
22
const github = require("@actions/github");
33
const axios = require("axios");
44

5-
const waitForUrl = async url => {
6-
for (let i = 0; i < 5; i++) {
5+
const waitForUrl = async (url, MAX_TIMEOUT) => {
6+
const iterations = MAX_TIMEOUT / 2;
7+
for (let i = 0; i < iterations; i++) {
78
try {
89
await axios.get(url);
10+
return;
911
} catch (e) {
10-
await new Promise(r => setTimeout(r, 5000));
11-
if (i === 4) {
12-
core.setFailed(`Timeout reached: Unable to connect to ${url}`);
13-
}
12+
console.log("Url unavailable, retrying...");
13+
await new Promise(r => setTimeout(r, 2000));
1414
}
1515
}
16+
core.setFailed(`Timeout reached: Unable to connect to ${url}`);
1617
};
1718

1819
const run = async () => {
1920
try {
20-
const url = core.getInput("url");
21+
const PR_NUMBER = github.context.payload.number;
22+
const MAX_TIMEOUT = Number(core.getInput("site_name")) || 60;
23+
const siteName = core.getInput("site_name");
24+
const url = `https://deploy-preview-${PR_NUMBER}--${siteName}.netlify.com`;
2125
console.log(`Waiting for a 200 from: ${url}`);
22-
await waitForUrl(url);
23-
// Get the JSON webhook payload for the event that triggered the workflow
24-
const payload = JSON.stringify(github.context.payload, undefined, 2);
25-
console.log(`The event payload: ${payload}`);
26+
await waitForUrl(url, MAX_TIMEOUT);
2627
} catch (error) {
2728
core.setFailed(error.message);
2829
}

0 commit comments

Comments
 (0)