Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ This action makes HEAD requests to a given URL until the required response code

The URL to poll. Default `"http://localhost/"`


### `responseCode`

Response code to wait for. Default `"200"`
Expand All @@ -21,14 +20,21 @@ Timeout before giving up in milliseconds. Default `"30000"`

Interval between polling in ms. Default `"1000"`

### `username` / `password`

For HTTP Basic Auth. Default: _empty_

## Example usage
```
uses: nev7n/wait_for_response@v1
with:
url: 'http://localhost:8081/'
responseCode: 200
timeout: 2000
interval: 500

```yaml
- uses: nev7n/wait_for_response@v1
with:
url: 'http://localhost:8081/'
responseCode: 200
timeout: 2000
interval: 500
username: ${{ secrets.BA_USERNAME }}
password: ${{ secrets.BA_PASSWORD }}
```

## Development
Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ inputs:
description: 'Interval between polling in ms'
required: false
default: '1000'
username:
description: 'HTTP Basic Auth username'
required: false
default: ''
password:
description: 'HTTP Basic Auth password'
required: false
default: ''
outputs:
result:
description: '0 if response code returned within timeout, otherwise 1'
Expand All @@ -33,3 +41,5 @@ runs:
- -code=${{ inputs.responseCode }}
- -timeout=${{ inputs.timeout }}
- -interval=${{ inputs.interval }}
- -username=${{ inputs.username }}
- -password=${{ inputs.password }}
42 changes: 35 additions & 7 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/base64"
"flag"
"fmt"
"net/http"
Expand All @@ -16,29 +17,52 @@ var (

func main() {
var (
// command flags
url = flag.String("url", "http://localhost/", "URL to poll")
responseCode = flag.Int("code", 200, "Response code to wait for")
timeout = flag.Int("timeout", 30000, "Timeout before giving up in ms")
interval = flag.Int("interval", 1000, "Interval between polling in ms")
localhost = flag.String("localhost", "", "Ip address to use for localhost")
username = flag.String("username", "", "HTTP Basic Auth: username")
password = flag.String("password", "", "HTTP Basic Auth: password")
)

flag.Parse()

var (
// runtime
userAgent = fmt.Sprintf(
"nev7n/wait_for_response/%s (+https://github.com/nev7n/wait_for_response)",
version)

startTime = time.Now()
timeoutDuration = time.Duration(*timeout) * time.Millisecond
intervalDuration = time.Duration(*interval) * time.Millisecond
basicAuth = ""
)

fmt.Printf("Polling URL `%s` for response code %d for up to %d ms at %d ms intervals\n",
*url, *responseCode, *timeout, *interval)

startTime := time.Now()
timeoutDuration := time.Duration(*timeout) * time.Millisecond
intervalDuration := time.Duration(*interval) * time.Millisecond

if *localhost != "" && strings.Contains(*url, "localhost") {
*url = strings.ReplaceAll(*url, "localhost", *localhost)
}

userAgent := fmt.Sprintf(
"nev7n/wait_for_response/%s (+https://github.com/nev7n/wait_for_response)",
version)
// validate both are set
if username != nil || password != nil {
if username == nil {
fmt.Printf("Missing username, password was set")
os.Exit(1)
}

if password == nil {
fmt.Printf("Missing password, username was set (%s)", *username)
os.Exit(1)
}

basicAuth = base64.StdEncoding.EncodeToString(
fmt.Appendf(nil, "%s:%s", *username, *password))
}

for {
ctx, cancel := context.WithTimeout(context.Background(), intervalDuration)
Expand All @@ -50,6 +74,10 @@ func main() {
}
req.Header.Set("User-Agent", userAgent)

if basicAuth != "" {
req.Header.Set("Authorization", "Basic "+basicAuth)
}

client := http.Client{}
res, err := client.Do(req)
cancel()
Expand Down