Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: print workflow uri immediately even when using --wait flag #663

Merged
merged 6 commits into from
Jan 7, 2025
54 changes: 35 additions & 19 deletions actions/trigger-argo-workflow/cmd/trigger-argo-workflow/argo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"bytes"
"bufio"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -58,41 +58,59 @@ func (a App) env() []string {

var nameRe = regexp.MustCompile(`^Name:\s+(.+)`)

func (a App) outputWithURI(input *bytes.Buffer) (string, string) {
output := strings.TrimSuffix(input.String(), "\n")
func (a App) outputWithURI(reader io.Reader) (string, string, error) {
scanner := bufio.NewScanner(reader)

matches := nameRe.FindStringSubmatch(output)
var uri string
var outputBuilder strings.Builder

for scanner.Scan() {
line := scanner.Text()
outputBuilder.WriteString(line + "\n")

if len(matches) != 2 {
a.logger.Warn("Couldn't find workflow name in output - can't construct URI for launched workflow")
return "", output
matches := nameRe.FindStringSubmatch(line)
dblinkhorn marked this conversation as resolved.
Show resolved Hide resolved
if len(matches) == 2 && uri == "" {
uri = fmt.Sprintf("https://%s/workflows/%s/%s", a.server(), a.namespace, matches[1])
a.logger.With("uri", uri).Info("workflow URI")
}
}

uri := fmt.Sprintf("https://%s/workflows/%s/%s", a.server(), a.namespace, matches[1])
if err := scanner.Err(); err != nil {
return uri, outputBuilder.String(), fmt.Errorf("error reading command output: %w", err)
}

return uri, output
return uri, outputBuilder.String(), nil
}

func (a App) runCmd(md GitHubActionsMetadata) (string, string, error) {
args := a.args(md)

cmd := exec.Command("argo", args...)
cmdOutput := &bytes.Buffer{}

cmd.Env = a.env()
cmd.Stdout = cmdOutput

stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return "", "", fmt.Errorf("failed to get stdout pipe: %w", err)
}

cmd.Stderr = os.Stderr

a.logger.With("executable", "argo", "command", cmd.Args, "retries", a.retries).Debug("running command")

err := cmd.Run()
if err != nil {
return "", "", err
if err := cmd.Start(); err != nil {
return "", "", fmt.Errorf("failed to start command: %w", err)
}

uri, output := a.outputWithURI(cmdOutput)
uri, out, scanErr := a.outputWithURI(stdoutPipe)
if scanErr != nil {
return uri, out, scanErr
dblinkhorn marked this conversation as resolved.
Show resolved Hide resolved
}

return uri, output, nil
if err := cmd.Wait(); err != nil {
return uri, out, fmt.Errorf("command failed: %w", err)
}

return uri, out, nil
}

func (a *App) setURIAsJobOutput(uri string, writer io.Writer) {
Expand Down Expand Up @@ -167,8 +185,6 @@ func (a *App) Run(md GitHubActionsMetadata) error {
return err
}

a.logger.With("uri", uri).Info("workflow URI")

writer := a.openGitHubOutput()
defer writer.Close()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ Namespace: argo
ServiceAccount: unset
Status: Pending
Created: Wed Dec 13 00:00:00 +0000 (now)
Progress:
Parameters:
Progress:
Parameters:
message: world
`

Expand Down Expand Up @@ -115,9 +115,12 @@ Parameters:
a.instance = tt.instance
a.namespace = "argo"

uri, _ := a.outputWithURI(bytes.NewBuffer([]byte(tt.input)))
reader := bytes.NewBufferString(tt.input)
uri, out, err := a.outputWithURI(reader)
require.NoError(t, err, "unexpected error reading command output")

require.Equal(t, tt.expected, uri)
require.Equal(t, tt.input, out)
})
}
}
Expand Down
Loading