I have a .gitlab-ci.yaml with the extends directive, which works on the gitlab-ci, but I cannot test it with gitlab-runner exec, it looks like the extends is ignored by gitlab-runner.
I have two files, like:
# .2extend.yml
.job2extend:
image: some/docker/img
stage: test
script:
- echo do things with "${myvar}"
and
# .gitlab-ci.yml
include:
- project: 'project/to/extend'
ref: master
file: '.2extend.yml'
myjob:
extends: .job2extend
variables:
myvar: 'My Variable'
This is the error I'm having:
$ gitlab-runner exec docker myjob
Runtime platform arch=amd64 os=linux pid=29785 revision=45d9c1d6 version=12.4.0~beta.1935.g45d9c1d6
FATAL: missing 'script' for job
The job I am extending from, have both script and image directives.
I have come to this issue which has the same problem as I do.
So, there is workaround to do this while the issue is not solved?
It seems gitlab-runner exec has some limitations, before using this functional please check with Limitations of gitlab-runner exec
As you can see for now extends is not listed in the table.
I suppose this is because the .gitlab-ci.yml file of the project does not have a script block at the root level (for example like this)
# .gitlab-ci.yml
include:
- project: 'project/to/extend'
ref: master
file: '.2extend.yml'
myjob:
extends: .job2extend
variables:
myvar: 'My Variable'
script:
- myjob
Related
I have this .gitlab-ci.yml file in the main repo
image: alpine:latest
include:
- project: 'abc/story'
ref: pipeline_creation
file: '/check.yml'
stages:
- abc
test:
tags:
- docker
stage: abc
script:
- echo "$first_name"
and this is the other external check.yml file in the same repo
first_name: "bilbo"
last_name: "baggins"
So basically, I am trying to access the variable from the check.yml and use that variable for different purposes. In this example i am just trying to print it.
I am using the include command as mentioned in the gitlab document. Doing so i am getting this error in the pipeline error
Can someone tell me what am i doing wrong? Thanks in advance.
I’ve created a hidden job that requires a specific image:
myhiddenjob.yml
.dosomething:
inherit:
default: false
image: curlimages/curl
script: …
This job is used in a pipeline that has a different base image:
default:
image: maven:3.8.6-jdk-11
include:
- remote: 'https://myhiddenjob.yml’
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS compile
testjob:
stage: mystage
script:
- echo "Working…"
- !reference [.dosomething, script]
The default image provides the mvn command used in the build job. However I want that the testjob uses a different image, the one defined inside the hidden job (curlimages/curl). However, although I specified inherit false, my hidden job uses the default image (maven:3.8.6-jdk-11) instead of the specified (curlimages/curl).
How can I override it in the hidden job definition?
One way is to use extends
.dosomething:
image: curlimages/curl
script: …
testjob:
stage: mystage
before_script:
- .....testjob sepcific commands
extends: .dosomething
You can also use before_script to add testjob specific commands.
I create template for gitlab-ci with script
.test-script: &test
- echo "hello"
How to use this script in .gitlab-ci.yml? I tried like this
include: '/templates/test-template.yml'
example-stage:
script:
- *test
Error occurs "This GitLab CI configuration is invalid: Unknown alias: test." because there is no alias for this script in the merged YAML.
GitLab version: GitLab Community Edition 14.6.0
Why not use GitLab's extends or reference keywords?
---
include:
- local: '/templates/test-template.yml'
example-stage:
script:
- !reference [.test-script, script]
or
---
include:
- local: '/templates/test-template.yml'
example-stage:
extends: .test-script
Maybe you could try this syntax :
.test-script: &test-script
script:
- echo "hello"
For your example-stage job :
example-stage:
<<: *test-script
I'd like to parameterize the environment URL of one of my GitLab CI job in a project which belongs to a subgroup.
If for example I have:
CI_PROJECT_PATH = mygroup/mysubgroup/myproject
CI_PROJECT_NAMESPACE = mygroup/mysubgroup
CI_PROJECT_NAME = myproject
I'd like to have the URL to be something like https://mygroup.gitlab.com/-/mysubgroup/myproject/-/jobs/12345/artifacts/public/index.html.
But I cannot find a way to do this since there is no predefined variable for the "sub-namespace" (here mysubgroup) and there is no variable substitution in environment.url as far as I can see.
I tried in my gitlab-ci.yml things like that:
build:
stage: build
image: bash:latest
script:
- export # print the available variables
environment:
name: test
url: ${CI_SERVER_PROTOCOL}://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-/${CI_PROJECT_PATH#${CI_PROJECT_ROOT_NAMESPACE}/}/-/jobs/$CI_JOB_ID/artifacts/public/index.html
but the result is https://mygroup.gitlab.com/-/${CI_PROJECT_PATH#myproject/}/-/jobs/12345/artifacts/public/index.html.
References:
https://stackoverflow.com/a/58402821/1064669
https://gitlab.com/gitlab-org/gitlab/-/issues/350902
job:environment:url is directly processed by gitlab, which does not support this kind of parameter expansion.
You'll need to use bash or a similar shell for this to work as intended:
build:
stage: build
image: bash:latest
script:
- echo "DEPLOY_URL=${CI_SERVER_PROTOCOL}://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-/${CI_PROJECT_PATH#${CI_PROJECT_ROOT_NAMESPACE}/}/-/jobs/$CI_JOB_ID/artifacts/public/index.html" > deploy.env
- cat deploy.env
artifacts:
reports:
dotenv: deploy.env
environment:
name: test
url: $DEPLOY_URL
Based on the example given here: https://docs.gitlab.com/ee/ci/environments/#example-of-setting-dynamic-environment-urls
I think you need to escape the inner curly braces like this: (${CI_PROJECT_ROOT_NAMESPACE} should become $`{CI_PROJECT_ROOT_NAMESPACE`})
So the whole thing becomes:
url: ${CI_SERVER_PROTOCOL}://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-/${CI_PROJECT_PATH#$`{CI_PROJECT_ROOT_NAMESPACE`}/}/-/jobs/$CI_JOB_ID/artifacts/public/index.html
Path to all projects is valid. I can confirm this because the code below works and runs a child pipeline:
stage: multi_project_test
trigger:
project: "my_organization/my_group/project_1"
strategy: depend
But the following code results in downstream project could not be found:
stages:
- test
- multi_project_test
- deploy
include:
- local: ".gitlab-ci-common.yml"
test:
extends: .test_stage_common
script:
- poetry install -vv
- !reference [.test_stage_common, script]
downstream_pipelines:
stage: multi_project_test
parallel:
matrix:
- DOWNSTREAM_PROJECT:
[
my_organization/my_group/project_1,
my_organization/my_group/project_2,
my_organization/my_group/project_3,
my_organization/my_group/project_4,
]
trigger:
project: $DOWNSTREAM_PROJECT
strategy: depend
variables:
PIPELINE_TYPE: "multi-project-pipeline"
Why the error happens?
Variables are allowed in many places, however trigger:project does not support use of variables. There is an open issue tracking enabling this feature to work for trigger:project.
As of GitLab 15.0, variables can be used in the following places in the .gitlab-ci.yml:
Definition
Can be expanded?
Expansion place
after_script
yes
Script execution shell
artifacts:name
yes
Runner
before_script
yes
Script execution shell
cache:key
yes
Runner
environment:name
yes
GitLab
environment:url
yes
GitLab
except:variables:[]
no
n/a
image
yes
Runner
include
yes
GitLab
only:variables:[]
no
n/a
resource_group
yes
GitLab
rules:if
no
n/a
script
yes
Script execution shell
services:[]:name
yes
Runner
services:[]
yes
Runner
tags
yes
GitLab
variables
yes
GitLab/Runner
For a full reference, including other files/places variables can be used, see the documentation: where variables can be used.
As a workaround, you could trigger the projects in a script: instead, which should provide the same effect.
trigger:
image: alpine
parallel:
matrix:
- DOWNSTREAM_PROJECT_ID: ["1234", "5678"]
script:
- apk add --no-cache curl
- curl --request POST --form "token=$CI_JOB_TOKEN" --form ref=main "https://${CI_SERVER_HOST}/api/v4/projects/${DOWNSTREAM_PROJECT_ID}/trigger/pipeline"