Class: Toys::CI::Template
- Inherits:
-
Object
- Object
- Toys::CI::Template
- Includes:
- Template
- Defined in:
- lib/toys/ci/template.rb
Overview
A template that can be used to implement a CI tool.
This template generates flags and implementation methods in the current
tool to implement CI. In particular, it generates the run method
itself. If you need more control over the CI tool's implementation,
consider using Mixin which provides a lower-level interface.
To implement a CI tool using this template, simply expand the template and provide the necessary configuration, including specifying at least one CI task to run. The generated tool will use Mixin under the hood.
Instance Attribute Summary collapse
-
#fail_fast_default ⇒ Object
writeonly
Set the default value of fail-fast.
-
#jobs_disabled_by_default ⇒ Object
writeonly
If set to true, all jobs are disabled by default unless explicitly enabled by their individual flags.
Instance Method Summary collapse
-
#all_flag=(value) ⇒ Object
Create a flag that will enable all jobs.
-
#base_ref_flag=(value) ⇒ Object
Create a flag that can be used to specify the base ref for the change directly.
-
#before_run(&block) ⇒ Object
Provide a block that will be run at the beginning of the CI job.
-
#cmd_job(name, cmd, flag: nil, override_flags: nil, trigger_paths: nil, env: nil, chdir: nil) ⇒ Object
Add a job implemented by an external process.
-
#collection(name, flag, job_flags, override_flags: nil) ⇒ Object
Define a collection of jobs that can be enabled/disabled as a group.
-
#fail_fast_flag=(value) ⇒ Object
Create flags that will enable and disable fail-fast.
-
#initialize ⇒ Template
constructor
Create the CI template.
-
#job(name, flag: nil, override_flags: nil, trigger_paths: nil, &block) ⇒ Object
Add a job implemented by a block.
-
#only_flag=(value) ⇒ Object
Create a flag that will disable all jobs.
-
#tool_job(name, tool, flag: nil, override_flags: nil, trigger_paths: nil, env: nil, chdir: nil) ⇒ Object
Add a job implemented by a tool call.
-
#use_github_base_ref_flag=(value) ⇒ Object
Create a flag that enables obtaining the change base ref from the GitHub workflow environment.
Constructor Details
#initialize ⇒ Template
Create the CI template. This template provides no direct arguments. All configuration of this template should be done by calling methods on the template in the expand block.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/toys/ci/template.rb', line 49 def initialize @jobs = [] @collections = [] @all_flag = nil @only_flag = nil @jobs_disabled_by_default = nil @fail_fast_flag = nil @fail_fast_default = false @base_ref_flag = nil @use_github_base_ref_flag = nil @prerun = nil end |
Instance Attribute Details
#fail_fast_default=(value) ⇒ Object
Set the default value of fail-fast. You can also create flags that can override this value using #fail_fast_flag=. Default is false.
260 261 262 |
# File 'lib/toys/ci/template.rb', line 260 def fail_fast_default=(value) @fail_fast_default = value end |
#jobs_disabled_by_default=(value) ⇒ Object
If set to true, all jobs are disabled by default unless explicitly enabled by their individual flags.
This setting is mutually exclusive with #all_flag= and #only_flag=. If you set up one of those flags, the default enabling behavior is also set implicitly.
237 238 239 |
# File 'lib/toys/ci/template.rb', line 237 def jobs_disabled_by_default=(value) @jobs_disabled_by_default = value end |
Instance Method Details
#all_flag=(value) ⇒ Object
Create a flag that will enable all jobs. All jobs will otherwise be disabled by default. This setting is mutually exclusive with #only_flag= and #jobs_disabled_by_default=.
The value can either be the flag key as a symbol, true to use the
default (which is :all), or false to disable such a flag.
For example, passing true will define the flag --all which will set
the context key :all.
207 208 209 |
# File 'lib/toys/ci/template.rb', line 207 def all_flag=(value) @all_flag = value end |
#base_ref_flag=(value) ⇒ Object
Create a flag that can be used to specify the base ref for the change directly. This can be used to filter CI jobs based on what has changed.
A change base ref provided in this way will override any obtained from other means, such as from the GitHub environment using #use_github_base_ref_flag=.
276 277 278 |
# File 'lib/toys/ci/template.rb', line 276 def base_ref_flag=(value) @base_ref_flag = value end |
#before_run(&block) ⇒ Object
Provide a block that will be run at the beginning of the CI job.
The block will run in the tool execution context, with self set to
the Toys::Context.
191 192 193 |
# File 'lib/toys/ci/template.rb', line 191 def before_run(&block) @prerun = block end |
#cmd_job(name, cmd, flag: nil, override_flags: nil, trigger_paths: nil, env: nil, chdir: nil) ⇒ Object
Add a job implemented by an external process.
118 119 120 121 122 123 124 |
# File 'lib/toys/ci/template.rb', line 118 def cmd_job(name, cmd, flag: nil, override_flags: nil, trigger_paths: nil, env: nil, chdir: nil) if override_flags && !flag raise ::Toys::ToolDefinitionError, "override_flags is meaningless without a flag" end @jobs << CmdJob.new(name, flag, Array(override_flags), trigger_paths, cmd, env, chdir) self end |
#collection(name, flag, job_flags, override_flags: nil) ⇒ Object
Define a collection of jobs that can be enabled/disabled as a group.
176 177 178 179 180 181 182 |
# File 'lib/toys/ci/template.rb', line 176 def collection(name, flag, job_flags, override_flags: nil) if job_flags.empty? raise ::Toys::ToolDefinitionError, "You must provide at least one entry in job_flags" end @collections << Collection.new(name, flag, Array(override_flags), job_flags) self end |
#fail_fast_flag=(value) ⇒ Object
Create flags that will enable and disable fail-fast. The flag should be
specified by symbol, and the actual flag will be set accordingly. You
can also use the value true which will set the default :fail_fast.
For example, passing true will define the flags --fail-fast and
--no-fail-fast.
250 251 252 |
# File 'lib/toys/ci/template.rb', line 250 def fail_fast_flag=(value) @fail_fast_flag = value end |
#job(name, flag: nil, override_flags: nil, trigger_paths: nil, &block) ⇒ Object
Add a job implemented by a block.
The block should perform a CI job and return a boolean indicating
whether or not the job succeeded. It will execute in the tool execution
context, with self set to the Toys::Context.
151 152 153 154 155 156 157 |
# File 'lib/toys/ci/template.rb', line 151 def job(name, flag: nil, override_flags: nil, trigger_paths: nil, &block) if override_flags && !flag raise ::Toys::ToolDefinitionError, "override_flags is meaningless without a flag" end @jobs << BlockJob.new(name, flag, Array(override_flags), trigger_paths, block) self end |
#only_flag=(value) ⇒ Object
Create a flag that will disable all jobs. All jobs will otherwise be enabled by default. This setting is mutually exclusive with #all_flag= and #jobs_disabled_by_default=.
The value can either be the flag key as a symbol, true to use the
default (which is :only), or false to disable such a flag.
For example, passing true will define the flag --only which will
set the context key :only.
223 224 225 |
# File 'lib/toys/ci/template.rb', line 223 def only_flag=(value) @only_flag = value end |
#tool_job(name, tool, flag: nil, override_flags: nil, trigger_paths: nil, env: nil, chdir: nil) ⇒ Object
Add a job implemented by a tool call.
86 87 88 89 90 91 92 |
# File 'lib/toys/ci/template.rb', line 86 def tool_job(name, tool, flag: nil, override_flags: nil, trigger_paths: nil, env: nil, chdir: nil) if override_flags && !flag raise ::Toys::ToolDefinitionError, "override_flags is meaningless without a flag" end @jobs << ToolJob.new(name, flag, Array(override_flags), trigger_paths, tool, env, chdir) self end |
#use_github_base_ref_flag=(value) ⇒ Object
Create a flag that enables obtaining the change base ref from the
GitHub workflow environment. This can be used to filter CI jobs based
on what has changed in a GitHub Actions workflow. The flag should be
specified by symbol, and the actual flag will be set accordingly. You
can also use the value true which will set the default
:use_github_base_ref. For example, passing true will define the
flags --use-github-base-ref and --no-use-github-base-ref.
291 292 293 |
# File 'lib/toys/ci/template.rb', line 291 def use_github_base_ref_flag=(value) @use_github_base_ref_flag = value end |