-
Notifications
You must be signed in to change notification settings - Fork 9
Add buildkite_add_trigger_step
action
#648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aabdd56
Action implementation
AliSoftware 2eeb61f
Add unit tests
AliSoftware 1545c7d
Fix test so it passes on CI
AliSoftware e959bd0
Add support for `depends_on` parameter
AliSoftware e14bcf0
Fix rubocop violation
AliSoftware 48c9bc3
Fix pipeline upload error detection
AliSoftware 296fc86
Add CHANGELOG entry
AliSoftware eb52f77
Catch early if we're running the action outside a Buildkite job
AliSoftware b421157
Apply suggestions from code review
AliSoftware 3568553
Use `sh` instead of backticks to get current branch
AliSoftware File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/buildkite_add_trigger_step_action.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'open3' | ||
|
||
module Fastlane | ||
module Actions | ||
class BuildkiteAddTriggerStepAction < Action | ||
BUILDKITE_ENV_ERROR_MESSAGE = 'This action can only be run from within a Buildkite build' | ||
|
||
def self.run(params) | ||
unless ENV.key?('BUILDKITE_JOB_ID') | ||
UI.user_error!(BUILDKITE_ENV_ERROR_MESSAGE) | ||
end | ||
|
||
# Extract parameters | ||
pipeline_file = params[:pipeline_file] | ||
build_name = File.basename(pipeline_file, '.yml') | ||
message = params[:message] || build_name | ||
branch = params[:branch] || sh('git', 'rev-parse', '--abbrev-ref', 'HEAD').strip | ||
environment = params[:environment] || {} | ||
buildkite_pipeline_slug = params[:buildkite_pipeline_slug] | ||
async = params[:async] | ||
label = params[:label] || ":buildkite: Trigger #{build_name} on #{branch}" | ||
depends_on = params[:depends_on]&.then { |v| v.empty? ? nil : Array(v) } | ||
|
||
# Add the PIPELINE environment variable to the environment hash | ||
environment = environment.merge('PIPELINE' => pipeline_file) | ||
|
||
# Create the trigger step YAML | ||
trigger_yaml = { | ||
'steps' => [ | ||
{ | ||
'trigger' => buildkite_pipeline_slug, | ||
'label' => label, | ||
'async' => async, | ||
'build' => { | ||
'branch' => branch, | ||
'message' => message, | ||
'env' => environment | ||
}, | ||
'depends_on' => depends_on | ||
}.compact, | ||
] | ||
}.to_yaml | ||
|
||
# Use buildkite-agent to upload the pipeline | ||
_stdout, stderr, status = Open3.capture3('buildkite-agent', 'pipeline', 'upload', stdin_data: trigger_yaml) | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Check for errors | ||
UI.user_error!("Failed to upload pipeline: #{stderr}") unless status.success? | ||
|
||
# Log success | ||
UI.success("Added a trigger step to the current Buildkite build to start a new build for #{pipeline_file} on branch #{branch}") | ||
end | ||
|
||
def self.description | ||
'Adds a trigger step to the current Buildkite pipeline to start a new build from this one' | ||
end | ||
|
||
def self.details | ||
<<~DETAILS | ||
This action adds a `trigger` step to the current Buildkite build, to start a separate build from the current one. | ||
|
||
This is slightly different from `buildkite-agent pipeline upload`-ing the YAML steps of the build directly to the current build, | ||
as this approach ensures the triggered build starts from a clean and independent context. | ||
- This is particularly important if the build being triggered rely on the fact that the current build pushed new commits | ||
to the Git branch and we want the new build's steps to start from the new commit. | ||
- This is also necessary for cases where we run builds on a mirror of the original Git repo (e.g. for pipelines of repos hosted | ||
in a private GitHub Enterprise server, mirrored on GitHub.com for CI building purposes) and we want the new build being triggered | ||
to initiate a new sync of the Git repo as part of the CI build bootstrap process, to get the latest commits. | ||
DETAILS | ||
end | ||
|
||
def self.available_options | ||
[ | ||
FastlaneCore::ConfigItem.new( | ||
key: :buildkite_pipeline_slug, | ||
env_name: 'BUILDKITE_PIPELINE_SLUG', | ||
description: 'The slug of the Buildkite pipeline to trigger. Defaults to the same slug as the current pipeline, so usually not necessary to provide explicitly', | ||
type: String, | ||
optional: false # But most likely to be auto-provided by the ENV var of the current build and thus not needed to be provided explicitly | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :label, | ||
description: 'Custom label for the trigger step. If not provided, defaults to ":buildkite: Trigger {`pipeline_file`\'s basename} on {branch}"', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :pipeline_file, | ||
description: 'The path (relative to `.buildkite/`) to the pipeline YAML file to use for the triggered build', | ||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :branch, | ||
description: 'The branch to trigger the build on. Defaults to the Git branch currently checked out at the time of running the action (which is not necessarily the same as the `BUILDKITE_BRANCH` the current build initially started on)', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :message, | ||
description: 'The message / title to use for the triggered build. If not provided, defaults to the `pipeline_file`\'s basename', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :environment, | ||
description: 'Environment variables to pass to the triggered build (in addition to the PIPELINE={pipeline_file} that will be automatically injected)', | ||
type: Hash, | ||
default_value: {}, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :async, | ||
description: 'Whether to trigger the build asynchronously (true) or wait for it to complete (false). Defaults to false', | ||
type: Boolean, | ||
default_value: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :depends_on, | ||
env_name: 'BUILDKITE_STEP_KEY', # This is the env var that Buildkite sets for the current step | ||
description: 'The steps to depend on before triggering the build. Defaults to the current step this action is called from, if said step has a `key:` attribute set. Use an empty array to explicitly not depend on any step even if the current step has a `key`', | ||
type: Array, | ||
default_value: [], | ||
optional: true | ||
), | ||
] | ||
end | ||
|
||
def self.return_value | ||
'Returns true if the pipeline was successfully uploaded. Throws a `user_error!` if it failed to upload the `trigger` step to the current build' | ||
end | ||
|
||
def self.authors | ||
['Automattic'] | ||
end | ||
|
||
def self.is_supported?(platform) | ||
true | ||
end | ||
|
||
def self.example_code | ||
[ | ||
<<~CODE, | ||
# Use default/inferred values for most parameters | ||
buildkite_add_trigger_step( | ||
pipeline_file: "release-build.yml", | ||
environment: { "RELEASE_VERSION" => "1.2.3" }, | ||
) | ||
CODE | ||
<<~CODE, | ||
# Use custom values for most parameters | ||
buildkite_add_trigger_step( | ||
label: "🚀 Trigger Release Build", | ||
pipeline_file: "release-build.yml", | ||
branch: "release/1.2.3", | ||
message: "Release Build (123)", | ||
environment: { "RELEASE_VERSION" => "1.2.3" }, | ||
async: false, | ||
) | ||
CODE | ||
] | ||
end | ||
|
||
def self.category | ||
:building | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.