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.
-
#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.
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.
241 242 243 |
# File 'lib/toys/ci/template.rb', line 241 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.
218 219 220 |
# File 'lib/toys/ci/template.rb', line 218 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.
188 189 190 |
# File 'lib/toys/ci/template.rb', line 188 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=.
257 258 259 |
# File 'lib/toys/ci/template.rb', line 257 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.
172 173 174 |
# File 'lib/toys/ci/template.rb', line 172 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.
99 100 101 102 103 104 105 |
# File 'lib/toys/ci/template.rb', line 99 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.
157 158 159 160 161 162 163 |
# File 'lib/toys/ci/template.rb', line 157 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.
231 232 233 |
# File 'lib/toys/ci/template.rb', line 231 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.
132 133 134 135 136 137 138 |
# File 'lib/toys/ci/template.rb', line 132 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.
204 205 206 |
# File 'lib/toys/ci/template.rb', line 204 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.
67 68 69 70 71 72 73 |
# File 'lib/toys/ci/template.rb', line 67 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.
272 273 274 |
# File 'lib/toys/ci/template.rb', line 272 def use_github_base_ref_flag=(value) @use_github_base_ref_flag = value end |