Search code examples
gitlabshgitlab-ci

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 ad1frdqscuat@DATAUAT.EDHV2 -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.


Solution

  • 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.