Ei kuvausta
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.8KB

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