update_ci_variables.rake 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. namespace :zammad do # rubocop:disable Metrics/BlockLength
  3. namespace :ci do # rubocop:disable Metrics/BlockLength
  4. desc 'Update CI variables that need it, like refresh tokens'
  5. task update_ci_variables: :environment do # rubocop:disable Metrics/BlockLength
  6. %w[MICROSOFT365_CLIENT_ID MICROSOFT365_CLIENT_SECRET MICROSOFT365_CLIENT_TENANT MICROSOFT365_REFRESH_TOKEN CI_VARIABLE_UPDATE_TOKEN].each do |var|
  7. raise "Error: the required environment variable #{var} was not found." if !ENV[var].presence
  8. end
  9. puts 'Trying to fetch a new MICROSOFT365_REFRESH_TOKEN...'
  10. result = ExternalCredential::Microsoft365.refresh_token(
  11. created_at: 30.days.ago,
  12. client_id: ENV['MICROSOFT365_CLIENT_ID'],
  13. client_secret: ENV['MICROSOFT365_CLIENT_SECRET'],
  14. client_tenant: ENV['MICROSOFT365_CLIENT_TENANT'],
  15. refresh_token: ENV['MICROSOFT365_REFRESH_TOKEN'],
  16. )
  17. if !result[:refresh_token].presence
  18. pp result
  19. raise 'Error: a new MICROSOFT365_REFRESH_TOKEN could not be found.'
  20. end
  21. puts "Trying to update the corresponding CI variable with the new token #{result[:refresh_token]}..."
  22. api_result = UserAgent.put(
  23. "#{ENV['CI_API_V4_URL']}/projects/#{ENV['CI_PROJECT_ID']}/variables/MICROSOFT365_REFRESH_TOKEN",
  24. {
  25. value: result[:refresh_token],
  26. },
  27. {
  28. headers: {
  29. 'PRIVATE-TOKEN' => ENV['CI_VARIABLE_UPDATE_TOKEN']
  30. }
  31. },
  32. )
  33. if !api_result.success?
  34. pp api_result
  35. raise 'Error: the CI variable could not be updated. Please make sure that CI_VARIABLE_UPDATE_TOKEN has a valid token.'
  36. end
  37. puts 'Done.'
  38. end
  39. end
  40. end