0

I have an gitlab ci yaml file. and 2 jobs. My .gitlab-ci.yaml file is:

variables:
  MSBUILD_PATH: 'C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe'
  SOLUTION_PATH: 'Source/NewProject.sln'

stages:
  - build
  - trigger_IT_service

build_job:
  stage: build
  script:
    - '& "$env:MSBUILD_PATH" "$env:SOLUTION_PATH" /nologo /t:Rebuild /p:Configuration=Debug'

trigger_IT_service_job:
  stage: trigger_IT_service
  script:
    - 'curl http://webapps.xxx.com.tr/dataBus/runTransfer/ctDigiTransfer'

And It's my trigger_IT_service job report:

Running on DIGITALIZATION...
00:00
Fetching changes with git depth set to 50...
00:05
 Reinitialized existing Git repository in D:/GitLab-Runner/builds/c11pExsu/0/personalname/newproject/.git/
 Checking out 24be087a as master...
 Removing Output/
 git-lfs/2.5.2 (GitHub; windows amd64; go 1.10.3; git 8e3c5c93)
 Skipping Git submodules setup
$ curl http://webapps.xxx.com.tr/dataBus/runTransfer/ctDigiTransfer
00:02
 StatusCode        : 200
 StatusDescription : 200
 Content           : {"status":200,"message":"SAP transfer started. Please 
                     check in db","errorCode":0,"timestamp":"2020-03-25T13:53:05
                     .722+0300","responseObject":null}
 RawContent        : HTTP/1.1 200 200
                     Keep-Alive: timeout=10
                     Connection: Keep-Alive
                     Transfer-Encoding: chunked
                     Content-Type: application/json;charset=UTF-8
                     Date: Wed, 25 Mar 2020 10:53:05 GMT
                     Server: Apache

I have to control the this report "Content" part in gitlab ci yaml If "message" is "SAP transfer started. Please check in db" the pipeline should pass otherwise must be failed. Actually my question is: how to parse Http json response and fail or pass job based on that Thank you for all your helps.

mrbytr
  • 23
  • 1
  • 2
  • 8

1 Answers1

0

Best way would be to install some tool to parse json and use it, different examples here

Given json example from comment:

{
  "status": 200,
  "message": "SAP transfer started. Please check in db",
  "errorCode": 0,
  "timestamp": "2020-03-25T17:06:43.430+0300",
  "responseObject": null
}

If you can install python3 on your runner you could achieve it all with script:

import requests; # note this might require additional install with pip install requests

message = requests.get('http://webapps.xxx.com.tr/dataBus/runTransfer/ctDigiTransfer').json()['message']
if message != 'SAP transfer started. Please check in db':
    print('Invalid message: ' + message)
    exit(1)
else:
    print('Message ok')

So trigger_IT_service stage in your yaml would be:

trigger_IT_service_job:
  stage: trigger_IT_service
  script: >
    python -c "import requests; message = requests.get('http://webapps.xxx.com.tr/dataBus/runTransfer/ctDigiTransfer').json()['message']; (print('Invalid message: ' + message), exit(1)) if message != 'SAP transfer started. Please check in db' else (print('Message ok'), exit(0))"
makozaki
  • 3,772
  • 4
  • 23
  • 47
  • At C:\Windows\TEMP\build_script224173828\script.ps1:173 char:136 00:01 + ... "import sys, json; if json.load(sys.stdin)['message'] != 'SAP transfe ... + ~~~~~~~~~~~~~~~~~ Unexpected token 'message'] != 'SAP' in expression or statement. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : UnexpectedToken I took this error Do you know it? Really thank you@makozaki – mrbytr Mar 25 '20 at 16:14
  • Cannot convert value "curl -f -s http://webapps.xxx.com.tr/dataBus/runTransfer/ctDigiTransfer" to type "System.Int32". Error: "Input string was not in a correct format." I try to learn how can I fix this error.But if you know i want to learn.thank you @makozaki – mrbytr Mar 25 '20 at 22:15
  • OK, I'm not that familiar with powershell + curl but it seems curl is only an alias in powershell to some native method. If you installed python3 I propose you use only python with requests library I updated my solution to use only python so you can try again. Note that this might require additional install of requests lib (`pip install requests`) – makozaki Mar 26 '20 at 07:07