|
| 1 | +module Fastlane |
| 2 | + module Actions |
| 3 | + class BuildkiteTriggerBuildAction < Action |
| 4 | + def self.run(params) |
| 5 | + require 'buildkit' |
| 6 | + |
| 7 | + UI.message "Triggering build on branch #{params[:branch]}, commit #{params[:commit]}, using pipeline from #{params[:pipeline_file]}" |
| 8 | + |
| 9 | + pipeline_name = { |
| 10 | + PIPELINE: params[:pipeline_file] |
| 11 | + } |
| 12 | + |
| 13 | + client = Buildkit.new(token: params[:buildkite_token]) |
| 14 | + response = client.create_build( |
| 15 | + params[:buildkite_organization], |
| 16 | + params[:buildkite_pipeline], |
| 17 | + { |
| 18 | + branch: params[:branch], |
| 19 | + commit: params[:commit], |
| 20 | + env: params[:environment].merge(pipeline_name) |
| 21 | + } |
| 22 | + ) |
| 23 | + |
| 24 | + response.state == 'scheduled' ? UI.message('Done!') : UI.crash!("Failed to start job\nError: [#{response}]") |
| 25 | + end |
| 26 | + |
| 27 | + ##################################################### |
| 28 | + # @!group Documentation |
| 29 | + ##################################################### |
| 30 | + |
| 31 | + def self.description |
| 32 | + 'Triggers a job on Buildkite' |
| 33 | + end |
| 34 | + |
| 35 | + def self.available_options |
| 36 | + [ |
| 37 | + FastlaneCore::ConfigItem.new( |
| 38 | + key: :buildkite_token, |
| 39 | + env_name: 'BUILDKITE_TOKEN', |
| 40 | + description: 'Buildkite Personal Access Token', |
| 41 | + type: String, |
| 42 | + sensitive: true |
| 43 | + ), |
| 44 | + FastlaneCore::ConfigItem.new( |
| 45 | + key: :buildkite_organization, |
| 46 | + env_name: 'BUILDKITE_ORGANIZTION', |
| 47 | + description: 'The Buildkite organization that contains your pipeline', |
| 48 | + type: String |
| 49 | + ), |
| 50 | + FastlaneCore::ConfigItem.new( |
| 51 | + key: :buildkite_pipeline, |
| 52 | + env_name: 'BUILDKITE_PIPELINE', |
| 53 | + description: %(The Buildkite pipeline you'd like to build), |
| 54 | + type: String |
| 55 | + ), |
| 56 | + FastlaneCore::ConfigItem.new( |
| 57 | + key: :branch, |
| 58 | + description: 'The branch you want to build', |
| 59 | + type: String |
| 60 | + ), |
| 61 | + FastlaneCore::ConfigItem.new( |
| 62 | + key: :commit, |
| 63 | + description: 'The commit hash you want to build', |
| 64 | + type: String, |
| 65 | + default_value: 'HEAD' |
| 66 | + ), |
| 67 | + FastlaneCore::ConfigItem.new( |
| 68 | + key: :pipeline_file, |
| 69 | + description: 'The name of the pipeline file in the project', |
| 70 | + type: String |
| 71 | + ), |
| 72 | + FastlaneCore::ConfigItem.new( |
| 73 | + key: :environment, |
| 74 | + description: 'Any additional environment variables to provide to the job', |
| 75 | + type: Hash, |
| 76 | + default_value: {} |
| 77 | + ), |
| 78 | + ] |
| 79 | + end |
| 80 | + |
| 81 | + def self.authors |
| 82 | + ['Automattic'] |
| 83 | + end |
| 84 | + |
| 85 | + def self.is_supported?(platform) |
| 86 | + true |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments