How to use script from template in .gitlab-ci.yml - gitlab

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

Related

accessing user defined pipeline variables within definitions steps bitbucket

Im trying pass a user defined variable from a custom pipeline to a step defined within the definitions section.
My yml snippet is below:
definitions:
steps:
- step: &TagVersion
trigger: manual
script:
- export VERSION=$VERSION
- echo "VERSION $VERSION"
custom:
run_custom:
- variables:
- name: VERSION
- step:
script:
- echo "starting"
- parallel:
- step:
<<: *TagVersion
variables:
VERSION: $VERSION
When I build the pipeline I can see the variable is listed as a pipeline variable when running the step TagVersion, and the correct value is shown there but dont know how to use this within the scripts section where im trying to echo out the value.
thanks

Gitlab-ci lost environment variables

I'm developing a pipeline on GitLab-ci, in the first job I use gittools/gitversion the obtain the semantic version of my software.
Here a small piece of code of /gitversion-ci-cd-plugin-extension.gitlab-ci.yml (Full documentation here https://gitversion.net/docs/reference/build-servers/gitlab)
.gitversion_function:
image:
name: gittools/gitversion
entrypoint: ['']
stage: .pre
.....
.....
artifacts:
reports:
#propagates variables into the pipeline level
dotenv: thisversion.env
Then a simplified version of my pipeline is as follows
stages:
- .pre
- install_dependencies
- build
- deploy
include:
- local: '/gitversion-ci-cd-plugin-extension.gitlab-ci.yml'
determineversion:
extends: .gitversion_function
install_dependencies:
image: node:16.14
stage: install_dependencies
script:
- echo ${PACKAGE_VERSION}
build:
image: node:16.14
stage: build
script:
- echo $PACKAGE_VERSION
deploy:
image: bitnami/kubectl
stage: deploy
needs: ['build']
script:
- echo $PACKAGE_VERSION
The problem is that the environment variable $PACKAGE_VERSION works in the first two jobs install_dependencies and build.
echo $PACKAGE_NAME; //0.0.1
But when the jobs deploy is executed the environment variable is not expanded by pipeline and I obtain literally this
echo $PACKAGE_NAME; //$PACKAGE_NAME
I found the problem.
In the last job of my pipeline, I use needs (https://docs.gitlab.com/ee/ci/yaml/#needs) to establish dependencies between jobs.
The problem is that artifact is not automatically passed because there is no a dependency between determineversion and deploy, to fix I do this:
...
deploy:
image: bitnami/kubectl
stage: deploy
needs: ['determineversion', 'build'] # <------
script:
- echo $PACKAGE_VERSION
...
I added determineversion as a dependency of deploy, in this way $PACKAGE_VERSION is printed correctly

How to reuse job in .gitlab-ci.yml

I currently have two jobs in my CI file which are nearly identical.
The first is for manually compiling a release build from any git branch.
deploy_internal:
stage: deploy
script: ....<deploy code>
when: manual
The second is to be used by the scheduler to release a daily build from develop branch.
scheduled_deploy_internal:
stage: deploy
script: ....<deploy code from deploy_internal copy/pasted>
only:
variables:
- $MY_DEPLOY_INTERNAL != null
This feels wrong to have all that deploy code repeated in two places. It gets worse. There are also deploy_external, deploy_release, and scheduled variants.
My question:
Is there a way that I can combine deploy_internal and scheduled_deploy_internal such that the manual/scheduled behaviour is retained (DRY basically)?
Alternatively: Is there is a better way that I should structure my jobs?
Edit:
Original title: Deploy job. Execute manually except when scheduled
You can use YAML anchors and aliases to reuse the script.
deploy_internal:
stage: deploy
script:
- &deployment_scripts |
echo "Deployment Started"
bash command 1
bash command 2
when: manual
scheduled_deploy_internal:
stage: deploy
script:
- *deployment_scripts
only:
variables:
- $MY_DEPLOY_INTERNAL != null
Or you can use extends keyword.
.deployment_script:
script:
- echo "Deployment started"
- bash command 1
- bash command 2
deploy_internal:
extends: .deployment_script
stage: deploy
when: manual
scheduled_deploy_internal:
extends: .deployment_script
stage: deploy
only:
variables:
- $MY_DEPLOY_INTERNAL != null
Use GitLab's default section containing a before_script:
default:
before_script:
- ....<deploy code>
job1:
stage: deploy
script: ....<code after than deploy>
job2:
stage: deploy
script: ....<code after than deploy>
Note: the default section fails to function as such if you try to execute a job locally with the gitlab-runner exec command - use YAML anchors instead.

How to use `extends` with `gitlab-runner exec`

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

How to combine GitLab variable expansion with YAML anchors?

I've got something like this in my .gitlab-ci.yml:
.templates:
- &deploy-master-common
script:
- ansible-playbook … --limit=${environment:name}.example.org
environment:
url: https://${environment:name}.example.org
…
deploy-master1:
<<: *deploy-master-common
environment:
name: master1
only:
- master
When running the deploy-master1 job, unfortunately, ${environment:name} is expanded to the empty string. Is this sort of expansion not supported by YAML/GitLab CI?
I can't tell yet whether this is a GitLab or YAML restriction, but it looks like the environment hash is being replaced rather than merged. Moving <<: *deploy-master-common to the bottom of deploy-master1 I get the following error message from the GitLab CI lint API endpoint:
.gitlab-ci.yml is not valid. Errors:
[
"jobs:deploy-master1:environment name can't be blank"
]
I was able to work around this with custom variables because the template doesn't specify any:
.templates:
- &deploy-master-common
script:
- ansible-playbook … --limit=${environment_name}.example.com
environment:
name: $environment_name
url: https://${environment_name}.example.com
…
deploy-master1:
<<: *deploy-master-common
variables:
environment_name: master1
only:
- master

Resources