GuidesModules
git.cloned
Ensures a Git repository is cloned at a target path. Optionally pins to a specific branch or commit revision.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | state ID | Filesystem path where the repo should be cloned |
url | string | required | Remote repository URL |
branch | string | — | Branch or tag to checkout after cloning |
rev | string | — | Specific commit SHA to checkout (takes precedence over branch for comparison) |
depth | int | 0 | Shallow clone depth; 0 means full clone |
force | bool | false | Reset local changes before checkout (git reset --hard HEAD) |
Behavior
Check
- If the target directory does not exist →
NeedsChange: true. - If it exists, runs
git -C <path> remote get-url originand compares tourl. If they differ →NeedsChange: true. - If
revorbranchis specified, runsgit -C <path> rev-parse HEADand compares. If HEAD does not match →NeedsChange: true. - Otherwise →
NeedsChange: false.
Apply
- If directory is missing:
git clone [--depth N] [--branch B] <url> <path>. - If directory exists with wrong remote URL:
git remote set-url origin <url>. - If
revorbranchspecified:git fetch originthengit checkout <target>. - If
forceis set:git reset --hard HEADbefore checkout.
Revert
Removes the directory with RemoveAll only if the directory was created during the most recent Apply. If the directory existed before Apply (e.g. only the remote URL was fixed), Revert is a no-op.
Examples
# Clone a public repo with no pinned version
/opt/myapp:
git.cloned:
- url: https://github.com/example/myapp
# Clone a specific branch with shallow depth
/srv/deploy/myapp:
git.cloned:
- url: git@github.com:example/myapp.git
- branch: production
- depth: 1
# Pin to a specific commit, require git to be installed first
/opt/tools/mylib:
git.cloned:
- url: https://github.com/example/mylib
- rev: a1b2c3d4e5f6
- require:
- pkg.installed:gitNotes
revandbranchcan be used together in config, butrevtakes precedence when checking whether HEAD matches.- The state shells out to the
gitbinary viaCommandExec;gitmust be installed on the target node. - TLS verification and SSH key configuration are handled by the system Git config, not by this module.