git resource¶
Use the git resource to manage source control resources that exist in a git repository. git version 1.6.5 (or higher) is required to use all of the functionality in the git resource.
Syntax¶
A git resource block manages source control resources that exist in a git repository:
git "#{Chef::Config[:file_cache_path]}/app_name" do
repository node[:app_name][:git_repository]
revision node[:app_name][:git_revision]
action :sync
end
The full syntax for all of the properties that are available to the git resource is:
git 'name' do
additional_remotes Hash
checkout_branch String # default value: deploy
depth Integer
destination String # default value: 'name' unless specified
enable_checkout true, false # default value: true
enable_submodules true, false # default value: false
environment Hash, nil
group String, Integer
remote String # default value: origin
repository String
revision String # default value: HEAD
ssh_wrapper String
timeout Integer
user String, Integer
action Symbol # defaults to :sync if not specified
end
where:
gitis the resource.nameis the name given to the resource block.actionidentifies which steps the chef-client will take to bring the node into the desired state.additional_remotes,checkout_branch,depth,destination,enable_checkout,enable_submodules,environment,group,remote,repository,revision,ssh_wrapper,timeout, anduserare the properties available to this resource.
Actions¶
The git resource has the following actions:
:checkout- Clone or check out the source. When a checkout is available, this provider does nothing.
:export- Export the source, excluding or removing any version control artifacts.
:nothing- Define this resource block to do nothing until notified by another resource to take action. When this resource is notified, this resource block is either run immediately or it is queued up to be run at the end of the Chef Client run.
:sync- Default. Update the source to the specified version, or get a new clone or checkout. This action causes a hard reset of the index and working tree, discarding any uncommitted changes.
Properties¶
The git resource has the following properties:
additional_remotesRuby Type: Hash
A Hash of additional remotes that are added to the git repository configuration.
checkout_branchRuby Type: String | Default Value:
deployDo a one-time checkout from git or use when a branch in the upstream repository is named
deploy. To prevent the git resource from attempting to check out master from master, setenable_checkouttofalsewhen using thecheckout_branchproperty. Seerevision.depthRuby Type: Integer
The number of past revisions to be included in the git shallow clone. The default behavior will do a full clone.
destinationRuby Type: String
The location path to which the source is to be cloned, checked out, or exported. Default value: the
nameof the resource block. See “Syntax” section above for more information.enable_checkoutRuby Type: true, false | Default Value:
trueCheck out a repo from master. Set to
falsewhen using thecheckout_branchattribute to prevent the git resource from attempting to check out master from master.enable_submodulesRuby Type: true, false | Default Value:
falsePerform a sub-module initialization and update.
environmentRuby Type: Hash
A Hash of environment variables in the form of
({"ENV_VARIABLE" => "VALUE"}). (These variables must exist for a command to be run successfully.)Note
The git provider automatically sets the
ENV['HOME']andENV['GIT_SSH']environment variables. To override this behavior and provide different values, addENV['HOME']and/orENV['GIT_SSH']to theenvironmentHash.groupRuby Type: String, Integer
The system group that is responsible for the checked-out code.
ignore_failureRuby Type: true, false | Default Value:
falseContinue running a recipe if a resource fails for any reason.
notifiesRuby Type: Symbol, ‘Chef::Resource[String]’
A resource may notify another resource to take action when its state changes. Specify a
'resource[name]', the:actionthat resource should take, and then the:timerfor that action. A resource may notify more than one resource; use anotifiesstatement for each resource to be notified.A timer specifies the point during the Chef Client run at which a notification is run. The following timers are available:
:before- Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed- Default. Specifies that a notification should be queued up, and then executed at the end of the Chef Client run.
:immediate,:immediately- Specifies that a notification should be run immediately, per resource notified.
The syntax for
notifiesis:notifies :action, 'resource[name]', :timer
referenceRuby Type: String
The alias for revision.
remoteRuby Type: String
The remote repository to use when synchronizing an existing clone.
repositoryRuby Type: String
The URI for the git repository.
retriesRuby Type: Integer | Default Value:
0The number of times to catch exceptions and retry the resource.
retry_delayRuby Type: Integer | Default Value:
2The retry delay (in seconds).
revisionRuby Type: String | Default Value:
HEADA branch, tag, or commit to be synchronized with git. This can be symbolic, like
HEADor it can be a source control management-specific revision identifier. Seecheckout_branch.The value of the
revisionattribute may change over time. From one branch to another, to a tag, to a specific SHA for a commit, and then back to a branch. Therevisionattribute may even be changed in a way where history gets rewritten.Instead of tracking a specific branch or doing a headless checkout, the chef-client maintains its own branch (via the git resource) that does not exist in the upstream repository. The chef-client is then free to forcibly check out this branch to any commit without destroying the local history of an existing branch.
For example, to explicitly track an upstream repository’s master branch:
revision 'master'
Use the
git rev-parseandgit ls-remotecommands to verify that the chef-client is synchronizing commits correctly. (The chef-client always runsgit ls-remoteon the upstream repository to verify the commit is made to the correct repository.)ssh_wrapperRuby Type: String
The path to the wrapper script used when running SSH with git. The
GIT_SSHenvironment variable is set to this.subscribesRuby Type: Symbol, ‘Chef::Resource[String]’
A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a
'resource[name]', the:actionto be taken, and then the:timerfor that action.Note that
subscribesdoes not apply the specified action to the resource that it listens to - for example:file '/etc/nginx/ssl/example.crt' do mode '0600' owner 'root' end service 'nginx' do subscribes :reload, 'file[/etc/nginx/ssl/example.crt]', :immediately end
In this case the
subscribesproperty reloads thenginxservice whenever its certificate file, located under/etc/nginx/ssl/example.crt, is updated.subscribesdoes not make any changes to the certificate file itself, it merely listens for a change to the file, and executes the:reloadaction for its resource (in this examplenginx) when a change is detected.A timer specifies the point during the Chef Client run at which a notification is run. The following timers are available:
:before- Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed- Default. Specifies that a notification should be queued up, and then executed at the end of the Chef Client run.
:immediate,:immediately- Specifies that a notification should be run immediately, per resource notified.
The syntax for
subscribesis:subscribes :action, 'resource[name]', :timer
timeoutRuby Type: Integer
The amount of time (in seconds) to wait for a command to execute before timing out. When this property is specified using the deploy resource, the value of the
timeoutproperty is passed from the deploy resource to the git resource.userRuby Type: String, Integer
The system user that is responsible for the checked-out code. Default value: the home directory of this user, as indicated by the
HOMEenvironment variable.
Examples¶
The following examples demonstrate various approaches for using resources in recipes. If you want to see examples of how Chef uses resources in recipes, take a closer look at the cookbooks that Chef authors and maintains: https://github.com/chef-cookbooks.
Use the git mirror
git '/opt/mysources/couch' do
repository 'git://git.apache.org/couchdb.git'
revision 'master'
action :sync
end
Use different branches
To use different branches, depending on the environment of the node:
if node.chef_environment == 'QA'
branch_name = 'staging'
else
branch_name = 'master'
end
git '/home/user/deployment' do
repository 'git@github.com:gitsite/deployment.git'
revision branch_name
action :sync
user 'user'
group 'test'
end
where the branch_name variable is set to staging or master, depending on the environment of the node. Once this is determined, the branch_name variable is used to set the revision for the repository. If the git status command is used after running the example above, it will return the branch name as deploy, as this is the default value. Run the chef-client in debug mode to verify that the correct branches are being checked out:
$ sudo chef-client -l debug
Install an application from git using bash
The following example shows how Bash can be used to install a plug-in for rbenv named ruby-build, which is located in git version source control. First, the application is synchronized, and then Bash changes its working directory to the location in which ruby-build is located, and then runs a command.
git "#{Chef::Config[:file_cache_path]}/ruby-build" do
repository 'git://github.com/sstephenson/ruby-build.git'
reference 'master'
action :sync
end
bash 'install_ruby_build' do
cwd '#{Chef::Config[:file_cache_path]}/ruby-build'
user 'rbenv'
group 'rbenv'
code <<-EOH
./install.sh
EOH
environment 'PREFIX' => '/usr/local'
end
To read more about ruby-build, see here: https://github.com/sstephenson/ruby-build.
Upgrade packages from git
The following example uses the git resource to upgrade packages:
# the following code sample comes from the ``source`` recipe
# in the ``libvpx-cookbook`` cookbook:
# https://github.com/enmasse-entertainment/libvpx-cookbook
git "#{Chef::Config[:file_cache_path]}/libvpx" do
repository node[:libvpx][:git_repository]
revision node[:libvpx][:git_revision]
action :sync
notifies :run, 'bash[compile_libvpx]', :immediately
end
Pass in environment variables
git '/opt/mysources/couch' do
repository 'git://git.apache.org/couchdb.git'
revision 'master'
environment 'VAR' => 'whatever'
action :sync
end