Add script to check "curl" return code and fail the job with clear message
Problem/Motivation
This is an edge case. The composer jobs were failing on a project after the last issue was merged: #3518751: Move calculation of gitlab templates version out of *show-ci-variables.
The reason for this is because the curl request returned a 404.
The .gitlab-ci.yml was missing the ref value, and looked like this:
include:
- project: $_GITLAB_TEMPLATES_REPO
file:
- '/includes/include.drupalci.main.yml'
- '/includes/include.drupalci.variables.yml'
- '/includes/include.drupalci.workflows.yml'
After adding the ref line it all worked, and this is the recommended syntax for the integration.
The failed composer job is: https://git.drupalcode.org/project/pinto/-/jobs/5142127
And we can see that the result of
Executing curl -OL https://git.drupalcode.org/project/gitlab_templates/-/raw/default-ref/scripts/get-gitlab-templates-version.php
was a 404 page, since that file is still NOT in the "default-ref" tag. We can see the 404 HTML output on line 31, and that is assigned to an env variable, which in turn provokes invalid syntax for the build.env file.
It's weird because it seems to be using default-ref, but it's also bringing the "latest" version of the yml file, with the renamed get-templates-version.php. This seems to be the result of not setting the ref line.
Reading the docs, we get this
include:ref: Optional. The ref to retrieve the file from. Defaults to the HEAD of the project when not specified
So that explains why it's using "main" but also "default-ref", as that's the default value for _GITLAB_TEMPLATES_REF.
Proposed resolution
I think we can add a -f option to the curl requests (https://curl.se/docs/manpage.html#-f).
Either that or try to detect when HEAD is different from "default-ref", and if we are on HEAD, either alter the value of _GITLAB_TEMPLATES_REF to "main" or fail somehow mentioning that ref might be needed. I think I prefer this one as it tackles the root issue but adding the -f can be a good failsafe as well.
Remaining tasks
Decide on an approach and implement it.