Honestly setting up CI/CD pipelines seems to be table stakes for doing any software development work. The idea of continuously building and deploying a solution is a well established concept. But it’s not without it’s problems.
One of those being that you run into the age old problems of dependencies, and how to handle those. One of the ironies of this is that often we create a devcontainers and other tools to make it easier to stand up a developer’s environment. But then we turn around and manually configure and maintain our build agents, creating double the effort and increasing the chances of problems.
What are devcontainers?
So let’s start with a fairly direct question, what are devcontainers? To put it simply, classic development says that I would clone a repo to my local machine, and then have to take my time to install any dependency required, and then start working with the code. That problem was managable in days-of-old, but is not anymore. The number of open source tools, and updates to those tools has exploded.
So the idea being, could we leverage containers to solve that problem? The answer is yes. The idea being that if we spin up a container with all dependencies installed and then enable developers to work within that container.
If you want to read more, you can find it on the VSCode article.
So the question becomes, why can’t we leverage this on the CI/CD side of things, to keep our github runners clean, and make our devcontainer the source of truth. The answer is we can, with devcontainer-cli.
What is devcontainer-cli?
The devcontainer-cli is command line tool that can be installed and configured to support managing and working with devcontainers via a command line. The idea being that we can take repos, and standup devcontainers and work with them completely from a terminal. This capability makes it possible to support running your devcontainers in a github workflow.
Installing devcontainer-cli:
The following are the commands to install the devcontainer-cli:
First you will need to install npm, and you can do that with the following:
sudo npm install -g n
Then you can install the devcontainer-cli:
sudo npm install -g @devcontainers/cli
And then validate the install with:
devcontainer --version
Using the devcontainer-cli:
The key commands for devcontainer-cli we are going to use are the following:
devcontainer up --workspace-folder ${PWD}
When you are in a repo with a devcontainer json, you can enable stand up the container in docker by running this command.
The next command that is particularly valuable to us is the “exec” command, which is the following:
devcontainer exec --workspace-folder . "path/to/script/to/execute"
This command will allow us to run commands inside the devcontainer. The benefit to this is we can take advantage of all the tools installed inside the container as our runner’s configuration.
What does this look like in Github Actions?
The good news, I took the liberty of building composite actions you can implement directly into your workflows that will enable using devcontainers to support CI/CD operations. And those actions are:
- install-devcontainer-cli – Supports AMD64 and ARM64.
- devcontainer-up
- devcontainer-exec-script
These tasks can be used to install devcontainer-cli, standup the devcontainer, and run scripts and tasks within the devcontainer. This enables and makes your life easier.