Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI/CD execute script file that exist in the repository

I have the following project:

enter image description here

Inside the file .gitlab-ci.yml I have a script that I run writen in different lines:

deploy-uat:
  <<: *job_definition
  image: myimage 
  stage: publish
  script:
    - if [[ $START_DATE == "" ]]; then echo "START_DATE is empty"; exit 1; fi;
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER 'kinit [email protected] -kt /etc/security/keytabs/ad1frdqscuat.keytab'
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER 'rm -rf /opt/application/UAT/1FR/DQSC/contracts/'
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER 'mkdir /opt/application/UAT/1FR/DQSC/contracts/'
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER 'rm -rf /opt/application/UAT/1FR/DQSC/jar/'
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER 'mkdir /opt/application/UAT/1FR/DQSC/jar/'
    - scp $JAR_PATH $USER@$SERVER:/opt/application/UAT/1FR/DQSC/jar/
    - scp $CONTRACT_PATH $USER@$SERVER:/opt/application/UAT/1FR/DQSC/contracts/
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER 'chmod -R 755 /opt/application/UAT/1FR/DQSC/jar/'
    - ssh -o StrictHostKeyChecking=no $USER@$SERVER '/opt/application/UAT/1FR/DQSC/draguenelle/1.13.3/bin/deployEnricher.sh -f /opt/application/UAT/1FR/DQSC/contracts/*.xlsm -o PROFITABILITY_KPI -j /opt/application/UAT/1FR/DQSC/jar/dqsc-different-ip-bandwidth-assembly-*.jar -qo DQSC -qs DQSC -m enrichment -s ' $START_DATE'T00:00Z'
  rules:
    - if: $CI_COMMIT_BRANCH == "develop"
      when: manual
  when: manual

I want to put all the content of script tag in a separate file that I will create in the project repository called for example script.sh and replace all the lines in the gitlab-ci.yml.

like image 262
ExtraHassan Avatar asked Sep 20 '25 17:09

ExtraHassan


2 Answers

You can just create a script.sh at your root level folder

Then in your script part within yml:

script:
  - chmod +x ./script.sh; ./script.sh

And also don't forget to remove the "-" character from the beginning of each line in your script file.

like image 146
Raymond Avatar answered Sep 22 '25 05:09

Raymond


for me this worked

script:
 - chmod +x script.sh
 - source script.sh
like image 37
Rajsekar Reddy Avatar answered Sep 22 '25 06:09

Rajsekar Reddy