【问题标题】:How to pass 1 function for all jobs in gitlab ci?如何为 gitlab ci 中的所有作业传递 1 个函数?
【发布时间】:2021-12-08 03:50:47
【问题描述】:

Gitlab-ci

st1:
  stage: build
  before_script:
    - |+
      function curling()
        {
           echo "ip are $1"
        }
  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 $1"
        }

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 消息报告它们的结果,其中几个单词不同,而不是多次重复相同的模板

【问题讨论】:

    标签: bash function curl gitlab-ci


    【解决方案1】:

    听起来像 YAML Anchors 是你的答案 (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 $1"
            }
    
    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
    
    

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2022-08-16
      • 2022-01-05
      • 2021-12-05
      • 2021-05-05
      • 1970-01-01
      • 2022-08-18
      • 2019-11-03
      • 2023-03-14
      相关资源
      最近更新 更多