This document describes additional open source command line tools that are provided by Semaphore and available in all VMs.
cache tool helps optimize CI/CD runtime by reusing files that your
project depends on, but are not part of version control. You should typically
use caching to:
- Reuse your project's dependencies, so that Semaphore fetches and installs them only when the dependency list changes.
- Propagate a file from one block to the next.
A cache is created on a per-project basis and is available in every pipeline job. All cache keys are scoped per project. Total cache size is 9.6GB.
cache tool uses key-path pairs for managing cached archives. An archive
can be a single file or a directory.
cache supports the following options:
cache store key path#
cache store our-gems vendor/bundle cache store gems-$SEMAPHORE_GIT_BRANCH vendor/bundle cache store gems-$SEMAPHORE_GIT_BRANCH-revision-$(checksum Gemfile.lock) vendor/bundle
cache store command archives a file or directory specified by
associates it with a given
cache store uses
tar, which automatically removes any leading
/ from a
Any further changes of
path after the store command completes will not
be automatically propagated to cache. This command always passes, i.e. exits
with return code 0.
In the event of insufficient disk space,
cache store frees disk space by deleting
the oldest keys.
cache restore key[,second-key,...]#
cache restore our-gems cache restore gems-$SEMAPHORE_GIT_BRANCH cache restore gems-$SEMAPHORE_GIT_BRANCH-revision-$(checksum Gemfile.lock),gems-master
Restores an archive which partially matches any given
In the event of a cache hit, the archive is retrieved and available at its original
path in the job environment.
Each archive is restored in the current path from which the function is called.
In the event of a cache miss, the comma-separated fallback takes over and the command looks up the next key. If no archives are restored, this command exits with 0.
cache has_key key#
cache has_key our-gems cache has_key gems-$SEMAPHORE_GIT_BRANCH cache has_key gems-$SEMAPHORE_GIT_BRANCH-revision-$(checksum Gemfile.lock)
Checks if an archive with the provided key exists in the cache. This command passes if the key is found in the cache, otherwise it fails.
Lists all cache archives for the project.
cache delete key#
cache delete our-gems cache delete gems-$SEMAPHORE_GIT_BRANCH cache delete gems-$SEMAPHORE_GIT_BRANCH-revision-$(checksum Gemfile.lock)
Removes any archive with the given key, if it is found in the cache. This command always passes.
Removes all cached archives for the project. This command always passes.
Note that in all commands related to the
cache, only the
cache has_key command can fail
(exit with non-zero status).
cache tool depends on the following environment variables,
which are automatically set in every job environment:
SEMAPHORE_CACHE_URL: stores the IP address and port number of the cache server (
SEMAPHORE_CACHE_USERNAME: stores the username that will be used for connecting to the cache server (
SEMAPHORE_CACHE_PRIVATE_KEY_PATH: stores the path to the SSH key that will be used for connecting to the cache server (
tar to archive specified directories or files.
The repository used in a Semaphore project is not automatically cloned for reasons of efficiency.
libcheckout script includes the implementation of a single function
checkout(), which is used for making the entire repository of the running Semaphore project available to the VM used for executing jobs in the pipeline.
checkout() function is called as
checkout from the command line.
By default, the implementation of the
checkout command uses shallow clone
during the clone operation.
With shallow clone, you get all the files in your repository and a small number of recent revisions. This makes the checkout process faster than downloading the entire history of the repository.
Note that some deployment scenarios, like Heroku, require the presence of a full Git history.
If you'd like to do a full clone, execute
checkout with the
flag, or use the following code snippet:
checkout git fetch --unshallow git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' git fetch --all
checkout() function of the
libcheckout script depends on the following
three Semaphore environment variables:
SEMAPHORE_GIT_URL: This environment variable holds the URL of the repository used in the Semaphore project (
SEMAPHORE_GIT_DIR: This environment variable holds the UNIX path where the repository will be placed in the VM (
SEMAPHORE_GIT_SHA: This environment variable holds the SHA key for the HEAD reference used when executing
git reset -q --hard.
SEMAPHORE_GIT_DEPTH: This environment variable holds the shallow clone depth level. By default, this value is
All these environment variables are automatically defined by Semaphore.
checkout command supports the
--use-cache flag. The purpose of this
flag is to tell
checkout to get the contents of the repository from
the Semaphore Cache server instead of from the servers, because it is faster.
If there is no cache entry for the active repository, the functionality
--use-cache flag will create one.
When using the
checkout supports the following
SEMAPHORE_GIT_CACHE_AGE: This environment variable specifies how often the cache for that repository will be updated. Its value is always in seconds and by default it is
259200, which is 3 days. The value that you are going to choose depends on your project and how often it gets updated.
SEMAPHORE_GIT_CACHE_KEEP: Each time there is an update to the cache, the key in the Cache server is also updated. Before updating the key, the previous key related to the current project is deleted. This environment variable tells the Semaphore Cache server to keep a history. The default value of
Note: When used with the
checkout does not use
Notice that the
checkout command automatically changes the current working
directory in the Operating System of the VM to the directory defined in the
SEMAPHORE_GIT_DIR environment variable.
The following command will tell
checkout to use the Semaphore Cache server
to get the contents of the repository, instead of using git server:
If you set
1, it will keep two copies in
the Semaphore Cache server: the active one and its antecedent.
If you set
SEMAPHORE_GIT_CACHE_AGE=86400, the cache for the
repository will be updated after 1 day.
libchecksum scripts provides the
useful for tagging artifacts or generating cache keys. It takes a
single argument, a file path, and outputs an
md5 hash value.
$ checksum package.json 3dc6f33834092c93d26b71f9a35e4bb3
retry script is used for retrying a command at
given time intervals – waiting for resources to become available or waiting
for network connectivity issues to be resolved is the main reason for using
retry bash script supports two command line parameters:
--times: this is used for defining the number of retries before giving up. The default value is 3.
--sleep: this is used for defining the time interval between retries. The default value is 0.
retry bash script only depends on the
$ retry bundle install
$ retry --times 10 go get ./...
$ retry --times 3 --sleep 5 make install.deps
install-package script can be used to cache missing packages and their dependencies.
It works by creating a "temporary package cache" folder (if its not present), downloading packages (that are not present),
installing the packages from the "temporary package cache", and uploading the folder to the projects cache.
install-package [--update|--skip-update|--update-new] pkg1[=version] [pkg2[=version]]
--update forces a repository list update before installing packages.
--skip-update skips the repository list update before installing packages.
--update-new updates only repository lists added in the last hour.
pkg1 is the desired package that accepts the
Multiple package names with versions are supported.