About the Workflow DSL
The Workflow DSL is a collection of helper methods that is used in recipes as a way to enhance the Recipe DSL to collect data about:
- Which testing tools—Foodcritic, Kitchen, ChefSpec, and so on—are present in a cookbook?
- What types of delivery environments are available?
- Have files been changed?
- Do the changes made to a cookbook require a version change?
- Should a cookbook should be pushed to the Chef Infra Server and/or to GitHub?
Warning
delivery-truck cookbook: https://github.com/opscode-cookbooks/delivery-truck. This cookbook is a recommended dependency for any build-cookbook for a project that publishes changes to a Chef Automate pipeline.Helpers
The following Chef Automate-specific helpers can be used in recipes:
bumped_version?Determine if files in a cookbook have been modified, and then to ensure that a cookbook’s version is updated in its metadata.rb file. The Verify phase will fail if files in a cookbook are modified without a corresponding version bump to the metadata.rb file.
changed_cookbooksGet a list of cookbooks that have been modified.
changed_filesGet a list of files that have been modified.
define_project_applicationDefine a set of attributes that are used to pin versions as the project moves through stages in the Chef Automate pipeline.
delivery_chef_serverReturn a Hash that contains details about how to talk to the Chef Infra Server.
delivery_chef_server_searchRun a search query against the node objects on the Chef Infra Server, using the default search query. The default search query is defined in the Chef Automate web UI on the “Default Search” tab.
delivery_environmentGet the name of the standard environment.
deployment_search_query-
Get a custom search query from the
config.jsonfile. By default, thedeploy.rbrecipe triggers a Chef Infra Client run via Chef Push Jobs against all nodes that belong to the current environment in Chef Automate and have modified cookbooks in the run-list.If the
config.jsonfile specifies:"delivery-truck": { "deploy": { "search": "SEARCH_QUERY" } }then the search is run against all node objects on the Chef Infra Server. For example:
"delivery-truck": { "deploy": { "search": "role:load_balancer" } } foodcritic_excludes-
Get a list of directories against which Foodcritic rules should not be run. This looks in the
config.jsonfile for thefoodcriticblock to see if theexcludessetting has been specified.THIS IS A DTAG delivery_config_json_setting_delivery_truck_lint_foodcritic_excludes
foodcritic_tags-
Get a list of Foodcritic tags (rule identifiers) to be run. This looks in the
config.jsonfile for thefoodcriticblock to see if theonly_rulesand/orignore_rulessettings have been specified.THIS IS A DTAG delivery_config_json_setting_delivery_truck_lint_foodcritic_ignore_rules
THIS IS A DTAG delivery_config_json_setting_delivery_truck_lint_foodcritic_only_rules
get_acceptance_environmentGet the name of the acceptance environment.
get_project_application-
Get attributes for a defined project application.
Warning
If the
get_project_applicationmethod is called from theprovision.rbrecipe, be sure thatdelivery-truck::provisionis executed beforeget_project_applicationby putting it in aruby_blockor into a separate recipe that is called fromprovision.rbviainclude_recipe. If theget_project_applicationmethod is called directly inprovision.rb:include_recipe 'delivery-truck::provision' get_project_application(<project_app_name_string>)you will get an error because Chef Infra Client will execute
get_project_applicationat compile time before it has runinclude_recipe 'delivery-truck::provision'. It is recommended to either useget_project_applicationindeploy.rb, a project cookbook, in aruby_blockor in a separateinclude_recipethat is executed afterinclude_recipe 'delivery-truck::provision'. get_project_secretsGet the contents of the
delivery-secretsdata bag from the Chef Infra Server. This data bag contains secrets that enable cookbooks to be published to a Chef Supermarket, an open source git repository, a repository on GitHub.has_kitchen_tests?Determine if a .kitchen.yml file exists in a cookbook.
has_spec_tests?Determine if tests exist in the
/specdirectory in a cookbook.push_repo_to_git?-
Determine if a repo should be pushed to an open source git repository.
THIS IS A DTAG delivery_config_json_setting_delivery_truck_publish_git
push_repo_to_github?-
Determine if a repo should be pushed to GitHub.
THIS IS A DTAG delivery_config_json_setting_delivery_truck_publish_github
share_cookbook_to_supermarket?-
Determine if a repo should be pushed to an open source git repository.
THIS IS A DTAG delivery_config_json_setting_delivery_truck_publish_supermarket
THIS IS A DTAG delivery_config_json_setting_delivery_truck_publish_supermarket_private
upload_cookbook_to_chef_server?-
Determine if a cookbook should be pushed to the Chef Infra Server.
THIS IS A DTAG delivery_config_json_setting_delivery_truck_publish_chef_server
use_custom_supermarket_credentialsTHIS IS A DTAG delivery_config_json_setting_delivery_truck_publish_supermarket_credentials
Node Attributes
Node attributes specific to the workspace, project changes, and project configuration are available for use in build-cookbook recipes.
Project Changes
Use attributes from the node['delivery']['change'] namespace to get details about the job execution for the current change in the pipeline.
change_idUse
node['delivery']['change']['change_id']for the change identifier.enterpriseUse
node['delivery']['change']['enterprise']for the enterprise name.git_urlUse
node['delivery']['change']['git_url']for the URL for the git project.organizationUse
node['delivery']['change']['organization']for the organization name.patchset_branchUse
node['delivery']['change']['patchset_branch']for the branch name.patchset_numberUse
node['delivery']['change']['patchset_number']for the patchset number.phaseUse
node['delivery']['change']['phase']for the current phase name.pipelineUse
node['delivery']['change']['pipeline']for the pipeline name.projectUse
node['delivery']['change']['project']for the project name.shaUse
node['delivery']['change']['sha']for the SHA-1 hash.stageUse
node['delivery']['change']['stage']for the current stage name.
Project Configuration
Use attributes from the node['delivery']['config'] namespace to get configuration settings from the .delivery/config.json file.
['build-cookbook']Use
node['delivery']['config']['build-cookbook']for the contents of the"build-cookbook"configuration setting.['build_nodes']Use
node['delivery']['config']['build_nodes']for the contents of the"build-cookbook"configuration setting.['delivery-truck']Use
node['delivery']['config']['delivery-truck']for the contents of the"build-cookbook"configuration setting.['delivery-truck']['PHASE_NAME']Use
node['delivery']['config']['delivery-truck']['phase_name']for the contents of a specific phase within the"build-cookbook"configuration setting. ReplacePHASE_NAMEwith the actual phase name. For example:node['delivery']['config']['delivery-truck']['publish'].dependenciesUse
node['delivery']['config']['dependencies']for the contents of the"build-cookbook"configuration setting.skip_phasesUse
node['delivery']['config']['skip_phases']for the contents of the"skip_phases"configuration setting.versionUse
node['delivery']['config']['version']for the contents of the"version"configuration setting.
Workspace
Use attributes from the node['delivery']['workspace'] namespace to get paths to workspace directories on a build node/runner.
cacheUse
node['delivery']['workspace']['cache']for the/cachedirectory.chefUse
node['delivery']['workspace']['chef']for the/chefdirectory.rootUse
node['delivery']['workspace']['root']for the root directory, typically/var/opt/delivery/workspace.repoUse
node['delivery']['workspace']['repo']for the/repodirectory.
Workspace Path
Use the node['delivery']['workspace_path'] attribute to get the path to the global workspace.
Examples
The following examples show how to use the Workflow DSL in a cookbook:
changed_cookbooks
changed_cookbooks.each do |cookbook|
execute "unit_rspec_#{cookbook[:name]}" do
cwd cookbook[:path]
command "rspec --format documentation --color"
only_if { has_spec_tests?(cookbook[:path]) }
end
end
bumped_version?
changed_cookbooks.each do |cookbook|
unless bumped_version?(cookbook[:path])
raise DeliveryTruck::Error, "The #{cookbook[:name]} cookbook was modified " \
"but the version was not updated in the " \
"metadata file."
end
execute "syntax_check_#{cookbook[:name]}" do
command "knife cookbook test -o #{cookbook[:path]} -a"
end
end
push_repo_to_github?
if push_repo_to_github?
git_ssh = File.join(node['delivery']['workspace']['cache'], 'git_ssh')
deploy_key = File.join(node['delivery']['workspace']['cache'], 'github.pem')
secrets = get_project_secrets
file deploy_key do
content secrets['github']
owner 'dbuild'
mode '0600'
sensitive true
end
template git_ssh do
source 'git_ssh.erb'
owner 'dbuild'
mode '0755'
end
execute "set_git_username" do
command "git config user.name 'Delivery'"
cwd node['delivery']['workspace']['repo']
environment({"GIT_SSH" => git_ssh})
end
execute "set_git_email" do
command "git config user.email '[email protected]'"
cwd node['delivery']['workspace']['repo']
environment({"GIT_SSH" => git_ssh})
end
github_repo = node['delivery']['config']['delivery-truck']['publish']['github']
execute "add_github_remote" do
command "git remote add github [email protected]:#{github_repo}.git"
cwd node['delivery']['workspace']['repo']
environment({"GIT_SSH" => git_ssh})
not_if "git remote --verbose | grep ^github"
end
execute "push_to_github" do
command "git push github master"
cwd node['delivery']['workspace']['repo']
environment({"GIT_SSH" => git_ssh})
end
end
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/dsl_delivery/