Make it possible to control build status using exit codes
## Update
We've open a separate issue https://gitlab.com/gitlab-org/gitlab/-/issues/273157 to explore additional ways on how to solve this issue - Please add your comments to this issue only if the proposed solution does not fit your needs
### Description
It may be an interesting feature to be able to control build status using `.gitlab-ci.yml` and exit codes.
### Proposal
Implement control for exit codes as follows, based on the list of possible ones that are supported (failed, warning, success):
```yaml
my-job:
script:
- echo Hello world!
script_exit_codes:
success: [0, 1, 2]
warning: [7, 14]
canceled: [8]
# anything else is failed
```
`script_exit_codes` are applied to the job based on the first non-zero exit code encountered in the script, or the last zero if all exit codes were zero.
- If `success:` is not defined, it is evaluated as `success: [0]`. If it is defined, and you want 0 to be a success, it must be included in your array.
- If `warning:` is not defined, it is evaluated as `warning: []`
- If `canceled:` is not defined, it is evaluated as `canceled: []`
- If a job is set to `allow_failure: true`, all failure states will be also treated as warnings.
#### Invalid configurations:
The following configuration should be considered invalid and a syntax error should be returned because of duplicate values (`1` in this case). Both `success` and `warning` should have mutually exclusive values.
```yaml
my-job:
script:
- echo Hello world!
script_exit_codes:
success: [0, 1, 2]
warning: [1]
```
### References
It would be used by https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/8121
Idea from @ayufan during Slack conversation :)
## Sequencing
1. Add `script_exit_codes` to the YAML keywords
1. Include `script_exit_codes` in the `/request` jobs [response](https://gitlab.com/gitlab-org/gitlab/-/blob/75953cffaa8775f69fd5c068c2ce717d0e9339de/app/services/ci/register_job_service.rb#L98-104)
1. Add support for [`canceled` state](https://gitlab.com/gitlab-org/gitlab/-/blob/4d8bc1acd8db679ff68dbdbb9edb06c83c6789a3/lib/api/ci/runner.rb#L178-185). This should already be done as part of https://gitlab.com/gitlab-org/gitlab-runner/-/issues/4843
1. The runner should compare the exit code with the ones defined in `script_exit_codes`(if available) before sending the status.
<!-- triage-serverless v3 PLEASE DO NOT REMOVE THIS SECTION -->
*This page may contain information related to upcoming products, features and functionality.
It is important to note that the information presented is for informational purposes only, so please do not rely on the information for purchasing or planning purposes.
Just like with all projects, the items mentioned on the page are subject to change or delay, and the development, release, and timing of any products, features, or functionality remain at the sole discretion of GitLab Inc.*
<!-- triage-serverless v3 PLEASE DO NOT REMOVE THIS SECTION -->
issue