Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Jenkinsfile 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. pipeline {
  2. agent any
  3. environment {
  4. BACKUP_DIR = './backup'
  5. }
  6. stages {
  7. stage('Checkout') {
  8. steps {
  9. checkout scm
  10. }
  11. }
  12. stage('Setup') {
  13. steps {
  14. script {
  15. BRANCH_NAME = env.BRANCH_NAME.split('/')[-1] // Extract branch name
  16. switch(BRANCH_NAME) {
  17. case "test08":
  18. env.BASE_URL = "https://test08.bizgaze.com/Account/Import/"
  19. env.AUTH_TOKEN = "6884972896664f4582e08f7f93da953d"
  20. break
  21. case "qaanwi":
  22. env.BASE_URL = "https://qa.anwisystems.com/Account/Import/"
  23. env.AUTH_TOKEN = "541c35d52a7b449f956175992619ed3a"
  24. break
  25. default:
  26. error "Unknown branch: ${BRANCH_NAME}. Exiting."
  27. }
  28. }
  29. }
  30. }
  31. stage('Process XML files') {
  32. steps {
  33. script {
  34. process_folder('./app', 'app')
  35. process_folder('./report', 'report')
  36. process_folder('./form', 'form')
  37. commitChanges()
  38. }
  39. }
  40. }
  41. }
  42. }
  43. def process_folder(folder, import_type) {
  44. import_url = "${env.BASE_URL}${import_type}"
  45. backup_folder = "${env.BACKUP_DIR}/${import_type}"
  46. sh "mkdir -p ${backup_folder}"
  47. // Use find command to get XML files and avoid error if none found
  48. def fileList = sh(script: "find ${folder} -name '*.xml' || echo ''", returnStdout: true).trim().split('\n')
  49. // Remove any empty strings from the list
  50. fileList = fileList.findAll { it != '' }
  51. // Check if we found any files
  52. if (fileList.size() == 0) {
  53. echo "No XML files found in ${folder}."
  54. return
  55. }
  56. echo "Detected files: ${fileList.join(', ')}"
  57. for(file in fileList) {
  58. def response = sh(script: """
  59. curl --location --request POST "${import_url}" \
  60. --header 'Authorization: stat ${env.AUTH_TOKEN}' \
  61. --form "=@${file}"
  62. """, returnStdout: true).trim()
  63. if(response != '') { // or other success condition
  64. sh "mv \"${file}\" \"${backup_folder}/\""
  65. echo "API Response: ${response}"
  66. } else {
  67. echo "Error while uploading ${file} to ${import_url}"
  68. }
  69. }
  70. }
  71. def commitChanges() {
  72. // Configure Git
  73. sh """
  74. git config user.email "jenkins@bizgaze.in"
  75. git config user.name "jenkinstest"
  76. """
  77. // Check for changes
  78. sh """
  79. git pull origin ${env.BRANCH_NAME}
  80. git add .
  81. git commit -m "Moved processed XML files to backup"
  82. git push --force https://jenkinstest:Bizgaze123@code.bizgaze.com/devops.bizgaze/imports.git ${env.BRANCH_NAME}
  83. """
  84. }