Skip to content

Fix Kube attach strategy to return correct exit code for job

Georgi N. Georgiev requested to merge fix_attach_incorrect_exit_code into master

What does this MR do?

Fixes incorrect exit code capture inside bash trap shell.

Why was this MR needed?

When using Kubernetes attach strategy the bash trap shell would always report exit code of 0. That's because the assignment log_file=%s would cause the last exit code $? to become 0.

What's the best way to test this MR?

Build the latest docker image: make helper-docker.

Comment the tests inside TestRunTestsWithFeatureFlag apart from testKubernetesSuccessRun and testKubernetesBuildFail. Run:

go test -ldflags "-X gitlab.com/gitlab-org/gitlab-runner/common.NAME=gitlab-runner -X gitlab.com/gitlab-org/gitlab-runner/common.VERSION=13.2.0~beta.303.$(git rev-parse --short=8 HEAD) -X gitlab.com/gitlab-org/gitlab-runner/common.REVISION=$(git rev-parse --short=8 HEAD) -X gitlab.com/gitlab-org/gitlab-runner/common.BUILT=2020-06-24T15:36:24+0000 -X gitlab.com/gitlab-org/gitlab-runner/common.BRANCH=fix_attach_incorrect_exit_code" -run TestRunTestsWithFeatureFlag -v ./executors/kubernetes/...

The tests should pass.

Also, a job with a command of exit 1 should exit with an error, rather than with success. E.g.

 Running with gitlab-runner development version (HEAD)
   on Georgis-MacBook-Pro-2.local efc1edjz
Preparing the "kubernetes" executor
00:00
 WARNING: Namespace is empty, therefore assuming 'default'.
 Using Kubernetes namespace: default
 Using Kubernetes executor with image gngeorgiev/ca-certs-busybox ...
 Using attach strategy to execute scripts...
Preparing environment
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Waiting for pod default/runner-efc1edjz-project-15339497-concurrent-0b5nwn to be running, status is Pending
 Running on runner-efc1edjz-project-15339497-concurrent-0b5nwn via Georgis-MacBook-Pro-2.local...
Getting source from Git repository
00:02
 Fetching changes with git depth set to 50...
 Initialized empty Git repository in /builds/ggeorgiev_/playground/.git/
 Created fresh repository.
 Checking out 2a5b6fec as master...
 Skipping Git submodules setup
Executing "step_script" stage of the job script
00:01
 $ echo hi
 hi
 $ exit 1
 ERROR: Job failed: command terminated with exit code 1

What are the relevant issue numbers?

Closes #26347 (closed)

Edited by Steve Xuereb

Merge request reports

Loading