Categoryk8s.io/git-sync
module
3.6.9+incompatible
Repository: https://github.com/kubernetes/git-sync.git
Documentation: pkg.go.dev

# README

git-sync

git-sync is a simple command that pulls a git repository into a local directory. It is a perfect "sidecar" container in Kubernetes - it can periodically pull files down from a repository so that an application can consume them.

git-sync can pull one time, or on a regular interval. It can pull from the HEAD of a branch, from a git tag, or from a specific git hash. It will only re-pull if the target of the run has changed in the upstream repository. When it re-pulls, it updates the destination directory atomically. In order to do this, it uses a git worktree in a subdirectory of the --root and flips a symlink.

git-sync can pull over HTTP(S) (with authentication or not) or SSH.

git-sync can also be configured to make a webhook call upon successful git repo synchronization. The call is made after the symlink is updated.

Building it

We use docker buildx to build images.

# build the container
make container REGISTRY=registry VERSION=tag
# build the container behind a proxy
make container REGISTRY=registry VERSION=tag \
    HTTP_PROXY=http://<proxy_address>:<proxy_port> \
    HTTPS_PROXY=https://<proxy_address>:<proxy_port>
# build the container for an OS/arch other than the current (e.g. you are on
# MacOS and want to run on Linux)
make container REGISTRY=registry VERSION=tag \
    GOOS=linux GOARCH=amd64

Usage

# make a directory (owned by you) for the volume
export DIR="/tmp/git-data"
mkdir -p $DIR

# run the container (as your own UID)
docker run -d \
    -v $DIR:/tmp/git \
    -u$(id -u):$(id -g) \
    registry/git-sync:tag \
        --repo=https://github.com/kubernetes/git-sync \
        --branch=master \
        --wait=30

# run an nginx container to serve the content
docker run -d \
    -p 8080:80 \
    -v $DIR:/usr/share/nginx/html \
    nginx

Webhooks

Webhooks are executed asynchronously from the main git-sync process. If a webhook-url is configured, when a change occurs to the local git checkout a call is sent using the method defined in webhook-method (default to POST). git-sync will continually attempt this webhook call until it succeeds (based on webhook-success-status). If unsuccessful, git-sync will wait webhook-backoff (default 3s) before re-attempting the webhook call.

Usage

A webhook is configured using a set of CLI flags. At its most basic only webhook-url needs to be set.

docker run -d \
    -v $DIR:/tmp/git \
    registry/git-sync:tag \
        --repo=https://github.com/kubernetes/git-sync \
        --branch=master \
        --wait=30 \
        --webhook-url="http://localhost:9090/-/reload"

Primary flags

FlagEnvironment VariableDefaultDescription
--repoGIT_SYNC_REPO(required)the git repository to clone
--branchGIT_SYNC_BRANCH"master"the git branch to check out
--revGIT_SYNC_REV"HEAD"the git revision (tag or hash) to check out
--rootGIT_SYNC_ROOT"$HOME/git"the root directory for git-sync operations, under which --dest will be created
--destGIT_SYNC_DEST""the name of (a symlink to) a directory in which to check-out files under --root (defaults to the leaf dir of --repo)
--waitGIT_SYNC_WAIT1 (second)the number of seconds between syncs
--one-timeGIT_SYNC_ONE_TIMEfalseexit after the first sync
--max-sync-failuresGIT_SYNC_MAX_SYNC_FAILURES0the number of consecutive failures allowed before aborting (the first sync must succeed, -1 will retry forever after the initial sync)
-v(none)""log level for V logs

Flags which control how git runs

FlagEnvironment VariableDefaultDescription
--depthGIT_SYNC_DEPTH0use a shallow clone with a history truncated to the specified number of commits
--submodulesGIT_SYNC_SUBMODULESrecursivegit submodule behavior: one of 'recursive', 'shallow', or 'off'
--timeoutGIT_SYNC_TIMEOUT120the max number of seconds allowed for a complete sync
--sparse-checkout-fileGIT_SYNC_SPARSE_CHECKOUT_FILE""the location of an optional sparse-checkout file, same syntax as a .gitignore file.
--git-configGIT_SYNC_GIT_CONFIG""additional git config options in 'key1:val1,key2:val2' format
--git-gcGIT_SYNC_GIT_GC"auto"git garbage collection behavior: one of 'auto', 'always', 'aggressive', or 'off'
--gitGIT_SYNC_GIT"git"the git command to run (subject to PATH search, mostly for testing

Flags which configure authentication

FlagEnvironment VariableDefaultDescription
--usernameGIT_SYNC_USERNAME""the username to use for git auth
--passwordGIT_SYNC_PASSWORD""the password or personal access token to use for git auth. (users should prefer --password-file or env vars for passwords)
--password-fileGIT_SYNC_PASSWORD_FILE""the path to password file which contains password or personal access token (see --password)
--sshGIT_SYNC_SSHfalseuse SSH for git operations
--ssh-key-fileGIT_SSH_KEY_FILE"/etc/git-secret/ssh"the SSH key to use
--ssh-known-hostsGIT_KNOWN_HOSTStrueenable SSH known_hosts verification
--ssh-known-hosts-fileGIT_SSH_KNOWN_HOSTS_FILE"/etc/git-secret/known_hosts"the known_hosts file to use
--add-userGIT_SYNC_ADD_USERfalseadd a record to /etc/passwd for the current UID/GID (needed to use SSH with a different UID)
--cookie-fileGIT_COOKIE_FILEfalseuse git cookiefile
--askpass-urlGIT_ASKPASS_URL""the URL to query for a username and password for git auth

Flags which configure hooks

FlagEnvironment VariableDefaultDescription
--exechook-commandGIT_SYNC_EXECHOOK_COMMAND""the command executed with the syncing repository as its working directory after syncing a new hash of the remote repository. it is subject to the sync time out and will extend period between syncs. (doesn't support the command arguments)
--exechook-timeoutGIT_SYNC_EXECHOOK_TIMEOUT30 (seconds)the timeout for the sync hook command
--exechook-backoffGIT_SYNC_EXECHOOK_BACKOFF3 (seconds)the time to wait before retrying a failed sync hook command
--webhook-urlGIT_SYNC_WEBHOOK_URL""the URL for a webhook notification when syncs complete
--webhook-methodGIT_SYNC_WEBHOOK_METHOD"POST"the HTTP method for the webhook
--webhook-success-statusGIT_SYNC_WEBHOOK_SUCCESS_STATUS200the HTTP status code indicating a successful webhook (-1 disables success checks to make webhooks fire-and-forget)
--webhook-timeoutGIT_SYNC_WEBHOOK_TIMEOUT1 (second)the timeout for the webhook
--webhook-backoffGIT_SYNC_WEBHOOK_BACKOFF3 (seconds)the time to wait before retrying a failed webhook

Flags which configure observability

FlagEnvironment VariableDefaultDescription
--http-bindGIT_SYNC_HTTP_BIND""the bind address (including port) for git-sync's HTTP endpoint
--http-metricsGIT_SYNC_HTTP_METRICStrueenable metrics on git-sync's HTTP endpoint
--http-pprofGIT_SYNC_HTTP_PPROFfalseenable the pprof debug endpoints on git-sync's HTTP endpoint

Other flags

FlagEnvironment VariableDefaultDescription
--change-permissionsGIT_SYNC_PERMISSIONS0the file permissions to apply to the checked-out files (0 will not change permissions at all)
--error-fileGIT_SYNC_ERROR_FILE""the name of a file into which errors will be written under --root

# Packages

No description provided by the author
No description provided by the author