#Is it possible to return a string (stdout) from dagger but still exit with a non-zero status code?

1 messages · Page 1 of 1 (latest)

tropic wigeon
#

I am trying to run ruff check --output-format github and ruff formata --check inside Dagger.

This is supposed to fail on linting rules violation, but still output valuable data (to be picked up as GitHub PR annotations).

As far as I can tell, Dagger can either return stdout and exit with 0 or just fail without any stdout.

I found the expect option, but that's still not exactly what I need

container.with_exec(
    args,
    expect=ReturnType.ANY
)
tropic wigeon
#

Is it possible to return a string (stdout) from dagger but still exit with a non-zero status code?

modest temple
#

Do you have a dagger cloud trace?

violet elk
#

I think you're on the right path with expect. You can still check the exit code and get stdout/stderr

modest temple
#

if the command fails, you can still capture the error and the stdout/err even if it fails

modest temple
#

i.e:

func (m *Fofo) Test(ctx context.Context) (string, error) {
    return dag.Container().From("alpine:latest").
        WithExec([]string{"sh", "-c", "echo hello && exit 1"}).Stdout(ctx)
}

^ that will return both the error and the output

violet elk
#

But can you get that stdout to another function in code? If it exits like that the pipeline will bail out

modest temple
#

you can get the error, assert the status code and decide what to do

#

my point is that you don't need to do anything for that. The stdout function will give you both the error and the output

#

and then you decide what you want to do

violet elk
#

But in python it will raise an exception

modest temple
#

my undersanding from the op's request is that you couldn't get the stdout when the command fails which you actually can

modest temple
#

there's a typed exception which gives you both stdout and stderr

tropic wigeon
#

Thanks @modest temple, I’ll look into it. My main problem was returning exactly the stdout from ruff (without raising any errors) but also setting a non-zero status code.

modest temple
#

You mentioned that expect doesn't seem to be what you need. Could you expand on that a bit?