如何为 gitlab ci 中的所有作业传递 1 个函数?

How to pass 1 function for all jobs in gitlab ci?

Gitlab-ci

st1:
  stage: build
  before_script:
    - |+
      function curling()
        {
           echo "ip are "
        }
  script:
    - foo=$(curl +x socks5 ifconfig.co) # like get proxy ext ip
    - boo=$(curl ifconfig.co)           # like get my ext ip
    - |+
      if [ $foo -eq $foo ]
      then
        curling equal
      else
        curling not_equal
      fi
  when: manual
  only:
    - master
  tags: 
   - tag

我想将作业数量增加到 3 个,并在其中使用相同的功能。我可以一次定义一个函数并在不同的工作中使用它吗?

喜欢

stages:
  -st1
  -st2
  -st3

st1:
  stage: st1
  script:
    - |+
      function curling()
        {
           echo "ip are "
        }

st2:
  stage: st2
  script:
    - foo=$(curl +x socks4 ifconfig.co) # like get first proxy ext ip
    - boo=$(curl ifconfig.co)           # like get my ext ip
    - |+
      if [ $foo -eq $foo ]
      then
        curling equal
      else
        curling not_equal
      fi
  when: manual
  only:
    - master
  tags: 
   - tag

st3:
  stage: st3
  script:
    - foo=$(curl +x socks5 ifconfig.co) # like get second proxy ext ip
    - boo=$(curl ifconfig.co)           # like get my ext ip
    - |+
      if [ $foo -eq $foo ]
      then
        curling equal
      else
        curling not_equal
      fi
  when: manual
  only:
    - master
  tags: 
   - tag

这样做的目的是使用许多操作并通过具有相同模板的echo/curl/webhook消息报告它们的结果,其中几个词不同,而不是多次重复相同的模板

听起来 YAML 锚点就是适合您的答案 (gitlab docs)

.shared_curl_tmp: &shared_curl
    - |+
      if [ $foo -eq $foo ]
      then
        curling equal
      else
        curling not_equal
      fi

stages:
  -st1
  -st2
  -st3

st1:
  stage: st1
  script:
    - |+
      function curling()
        {
           echo "ip are "
        }

st2:
  stage: st2
  script:
    - foo=$(curl +x socks4 ifconfig.co) # like get first proxy ext ip
    - boo=$(curl ifconfig.co)           # like get my ext ip
    <<: *shared_curl
  when: manual
  only:
    - master
  tags: 
   - tag

st3:
  stage: st3
  script:
    - foo=$(curl +x socks5 ifconfig.co) # like get second proxy ext ip
    - boo=$(curl ifconfig.co)           # like get my ext ip
    <<: *shared_curl
  when: manual
  only:
    - master
  tags: 
   - tag