Home > front end >  How to pass 1 function for all jobs in gitlab ci?
How to pass 1 function for all jobs in gitlab ci?

Time:10-22

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

I want to increase the number of jobs to 3 and use the same function in them. Can I define a function once and use it in different jobs?

like

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

The purpose of this is to use many actions and report their results via echo/curl/webhook messages with the same template, where a couple of words differ, and not to repeat the same template several times

CodePudding user response:

Sounds like YAML Anchors are the answer for you (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

  • Related