#Get exit code from WithExec

1 messages · Page 1 of 1 (latest)

lean mountain
#

Hi,

Is there an idiomatic way in Dagger to get the exit code from a WithExec operation? In my case I have a binary that returns a non-zero exit code, e.g.

ctr, err := cli.Container().From(image).WithExec([]string{"mybinary", "--exit-code"}).Sync(ctx)

I was trying to cast the err into a dagger.ExecError to inspect for the exit code but apparently err is nil.

vestal wigeon
#

WithExec returns a *Container which does not necessarily have a concept of exit code.

I am not 100% sure, but I think you may need to run WithExec with a few additional options https://pkg.go.dev/dagger.io/dagger#ContainerWithExecOpts

My immediate thought though is to run WithExec($?) as the last step because this should give you the exit code of the previously executed step.

With all that being said, can you tell me more about what you are trying to do? One of the main benefits of having first class primitives like Files, Directories, Binaries, containers, Services etc... is that you don't have to rely only on exit codes to make complex pipelines.

tranquil shoal
#

If that binary does indeed return a non-zero exit code, that code should return a dagger.ExecError. Can you share more details? What does --exit-code do? It’s not printing it to stdout is it?

lean mountain
#

I'd like my Dagger func to analyze a container image and return an error if it contains vulnerabilities. In this case, the func takes an image tarball (file path) and uses docker scout cves archive://image.tar --exit-code . The stdout shows the result of the analysis and if there're vulns, the exit code returned is 2.

jagged current
#

If I run that I get a 2 exit code from that function

#

LMK if that works

lean mountain
#

Thanks @jagged current, it was a silly mistake on my side, all good now 👍

#

One last thing, do you happen to know why I'm seeing this error in the GH workflow run? [1]

ERROR: required flag(s) "docker-scout-hub-password", "tarball-path" not set

I've specified both values in the GH workflow in my PR [ 2].

It works fine locally but not in the GH workflow 😅

[1] https://github.com/felipecruz91/go-dagger-pipeline/actions/runs/8501050664/job/23283557847#step:7:137

[2] https://github.com/felipecruz91/go-dagger-pipeline/pull/1/files#diff-b803fcb7f17ed9235f1e5cb1fcd2f5d3b2838429d4368ae4c57ce4436577f03fR59

jagged current
#

I might be missing something since my screen is a bit small but have you checked that?