CI/CD Integration
DevSpace is designed to work in non-interactive environments the same way it does in interactive environments. To use DevSpace inside a CI/CD pipeline, simply install it as part of your pipeline or start the pipeline with a VM or container image that already contains DevSpace and run any of the DevSpace commands.
Docker Image
DevSpace provides an official Docker image that can be found on Docker Hub. The Dockerfile for this image is available on GitHub.
Use this image as shown here:
docker run devspacesh/devspace devspace [command] [args]
Example: Get Version
docker run devspacesh/devspace devspace --version
Example: Deployment
docker run -v ${PWD}:/project -w /project devspacesh/devspace devspace deploy
- Mounts current working directory into
/project
within the container - Sets
/project
as working dir - Runs
devspace deploy
inside working dir
Example: Start Interactive Terminal Session
docker run -it -v ${PWD}:/project -w /project devspacesh/devspace
Deployment Tips
Avoid Pickers
If you want to run commands such as devspace enter
, make sure you specify a label selector using -l
, a container using -c
etc. to make sure DevSpace can find the right pod/container without having to ask you to select one (which would lead to your pipeline getting stuck).
--wait
Flag
DevSpace is configured to deploy and terminate when you run devspace deploy
. In CI/CD pipelines, it is common to wait until the deployment is actually up and running before continuing the pipeline. You can achieve this by using --wait
:
devspace deploy --wait --timeout=120
The above command will wait until the desired number of pods is created for all ReplicaSets and until none of the pods are failing anymore. If this is not achieved within the number of seconds specified in --timeout
, DevSpace will exit with a fatal error.
Define Profiles
It is often useful to define profiles in devspace.yaml
for staging, review or production deployments using CI/CD. You can deploy a project using such a profile using the -p / --profile
flag:
devspace deploy -p production
Separate Build & Deploy
DevSpace allows you to split up the build and deployment process into two separate commands:
devspace build # Build, tag and push images
devspace deploy --skip-build # Deploy project without rebuilding images
Run Tests
To run unit or integration tests in your CI/CD pipeline, you have multiple options:
- Split up build and deployment command and run tests before after or in between the commands
- Define hooks that execute commands before/after certain image builds or deployments
DevSpace Cloud
Authentication
If you want to use DevSpace Cloud in your CI/CD pipeline as well, you need to generate an "Access Key" and use it to login to DevSpace Cloud using the following non-interactive command:
devspace login --key=[YOUR_ACCESS_KEY]
Encryption Key
If you are using the SaaS edition of DevSpace Cloud, you will need an encryption key to run commands such as devspace create space
or devspace use space
. To set an encryption key in a non-interactive environment, use:
devspace set encryptionkey [ENCRYPTION_KEY] --cluster=[cluster-name]
The encryption key is a user-defined password that you defined when connecting your cluster to DevSpace Cloud. You can reset the encryption key for a cluster by running the following command and choosing the appropriate kube-context of the cluster:
devspace reset key