Running your Travis CI builds locally with Docker2 min read
Setting up the environment to run the tests on a CI/CD can take a few roundtrips between your host machine and the CI/CD running remotely. For every attempt, you’ll have to commit and publish your changes before waiting for the CI/CD platform to pick up the latest version of your code, initialize the environment such as downloading and installing the dependencies, and finally run the tests.
With Travis CI and a little of work, you can recreate the conditions of your tests by downloading and recreating the Docker image which Travis CI is using. This is a little advanced topic which comes in handy when your tests are a little more complex than regular.
Before starting, your project must already be on GitHub and activated in Travis CI. At least one build must have been executed. We use the build log to extract the Docker image to use as well as the commands executed by Travis CI inside the container to prepare the job. In the
Job log of your project, expand the
Worker information line and search for the line starting with
Worker information hostname: email@example.com version: v6.2.20-1-g3a987d6 https://github.com/travis-ci/worker/tree/3a987d61ed169c9539ad435d1a9b5f2d8c6ce4a6 instance: travis-job-8c5d2a02-68a2-4b51-bc02-625ef2c37ad3 travis-ci-sardonyx-xenial-1593004276-4d46c6b3 (via amqp)startup: 5.913615831s
The name of the Docker image is the word after
sardonyx in the example above. From there, go to the Docker registry of Travis CI, localize the appropriate Docker image,
travisci/ci-sardonyx in my case, and extract the latest version in the tags section. It is
packer-1606895335-7957c7a9 for me.
Now, update the command below with the name of the Docker image:
You can now start the container and enter inside:
BUILDID="build-$RANDOM" docker run --name $BUILDID -dit $INSTANCE /sbin/init docker exec -it $BUILDID bash -l
Once you enter into the container, login with the
travis user and install the packages using the Travis CI logs as an example:
su - travis git clone --depth=50 --branch=master https://github.com/adaltas/remark-gatsby-plugins.git adaltas/remark-gatsby-plugins cd adaltas/remark-gatsby-plugins git checkout master nvm install 14 yarn --frozen-lockfile yarn test yarn run v1.22.5 $ lerna run test lerna notice cli v3.22.1 lerna info versioning independent lerna info ci enabled lerna info Executing command in 1 package: "yarn run test" lerna info run Ran npm script 'test' in 'gatsby-remark-title-to-frontmatter' in 1.2s: $ mocha 'test/**/*.coffee' Extract title ✓ Move the title to frontmatter 1 passing (21ms) lerna success run Ran npm script 'test' in 1 package in 1.2s: lerna success - gatsby-remark-title-to-frontmatter Done in 1.95s.
If the tests are successful locally on your Docker image, chances are that they will pass remotely on the Travis CI platform.