'Gitlab CICD: use functions inside gitlab-ci.yml

I have a .gitlab-ci.yml file which allows needs to execute the same function for every step. I have the following and this works.

image:
  name: hashicorp/terraform

before_script: 
  - export MYDATE=$(date "+%d/%m/%y - %H:%M:%S")

stages:
  - validate
  - plan

validate:
  stage: validate
  script:
    - terraform validate
    - 'curl --request POST --header "Authorization: Bearer $bearer"  --form "text=$MYDATE $msg" https://api.teams.com/v1/messages'
  variables:
    msg: "Example1"

plan:
  stage: plan
  script:
    - terraform validate
    - 'curl --request POST --header "Authorization: Bearer $bearer"  --form "text=$MYDATE $msg" https://api.teams.com/v1/messages'
  variables:
    msg: "Example2"

Given it is always the same curl command, I wanted to use a function which I declare once and can use in every step. Something along the lines of below snippet.

image:
  name: hashicorp/terraform

before_script: 
  - export MYDATE=$(date "+%d/%m/%y - %H:%M:%S")

.send_message: &send_message
  script:  
  - 'curl --request POST --header "Authorization: Bearer $bearer"  --form "text=$MYDATE $msg" https://api.teams.com/v1/messages'

stages:
  - validate
  - plan

validate:
  stage: validate
  script:
    - terraform validate
    - &send_message
  variables:
    msg: "Example1"

plan:
  stage: plan
  script:
    - terraform validate
    - &send_message
  variables:
    msg: "Example2"

How could I use such a function in a .gitlab-ci.yml file.



Solution 1:[1]

you can used include with !reference such as:

  • functions.yml
.send_message:
  script:  
  - 'curl --request POST --header "Authorization: Bearer $bearer"  --form "text=$MYDATE $msg" https://api.teams.com/v1/messages'
  • .gitlab-ci.yml
include:
  - local: functions.yml

default:
  image:
    name: hashicorp/terraform
  before_script: 
    - export MYDATE=$(date "+%d/%m/%y - %H:%M:%S")

stages:
  - validate
  - plan

validate:
  stage: validate
  script:
    - terraform validate
    - !reference [.send_message, script]
  variables:
    msg: "Example1"

plan:
  stage: plan
  script:
    - terraform validate
    - !reference [.send_message, script]
  variables:
    msg: "Example2"

ref: https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#reference-tags

Solution 2:[2]

You can also use a regular old bash function defined at the top:

before_script: 
  - export MYDATE=$(date "+%d/%m/%y - %H:%M:%S")
  - send_bearer () { terraform validate; curl --request POST --header "Authorization: Bearer $bearer"  --form "text=$MYDATE $1" https://api.teams.com/v1/messages; }

...

validate:
  stage: validate
  script:
    - send_bearer $msg
  variables:
    msg: "Example1"

plan:
  stage: plan
  script:
    - send_bearer $msg
  variables:
    msg: "Example2"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mouson Chen
Solution 2 Ken Williams