To follow this tutorial on your own computer, please install the
jet CLI locally first.
When using microservices, you may have several repos you want to build together or in sequences. There are two primary setups to accomplish this, and for each setup there are a couple of methods you can use.
You can create separate CI/CD projects for each repository, allowing them to deploy independently of one another. Then, you can either have each project bring in all of your individual component repos for full-stack testing, or you can create a separate, agnostic project just for the purposes of combining them for full-stack testing.
You can create a single project that pulls from all of your repos, deploying all components together whenever the master project is updated.
Once you decide which workflow you like, there are two main ways to combine your repos for full-stack testing.
If you opt to create separate CI/CD projects for each individual component, you can have each component’s pipeline end with an image push to an image registry.
Then, either with a separate project or as a part of the build process for each component, you can pull in the latest images for each component to build and test your combined application.
In addition to the above method using images in a regsitry, you can also clone the repos themsleves during the build process - either with a separate project or as part of the build for each individual component.
The first step of cloning a repo into your build process is to create your SSH key authentication. Another option for cloning from GitHub would be to use OAuth instead of an SSH key.
The following command will create two files in your local repository.
keyfile.rsa contains your private key that we will encrypt and put into your repository. This encrypted file will be decrypted on Codeship as part of your build. The second file
keyfile.rsa.pub can be added to services that you want to access.
ssh-keygen -t rsa -b 4096 -C "email@example.com" -f keyfile.rsa
Now you have to copy the content of
keyfile.rsa into an environment file
sshkey.env. Make sure to replace newlines with \n so the whole SSH key is in one line. The following is an example of ssh_key.env.
PRIVATE_SSH_KEY=-----BEGIN RSA PRIVATE KEY-----\nMIIJKAIBAAKCFgEA2LcSb6INQUVZZ0iZJYYkc8dMHLLqrmtIrzZ...
After preparing the
sshkey.env file we can encrypt it with Jet. Follow the encryption tutorial to turn the
sshkey.env file into a
You can then add it to a service with the
encrypted_env_file option. It will be automatically decrypted on Codeship.
app: build: . encrypted_env_file: sshkey.env.encrypted
Before running a command that needs SSH available make sure to run the following commands in that container. They will set up the SSH key so you can access external services.
mkdir -p "$HOME/.ssh" echo -e $PRIVATE_SSH_KEY >> $HOME/.ssh/id_rsa
Once you have the SSH authentication working, then you’ll just need a Dockerfile that grabs git and clones the repo you want:
FROM ubuntu:latest COPY . RUN apt-get update && apt-get install -y ca-certificates git-core ssh RUN mkdir -p "$HOME/.ssh" RUN echo -e $PRIVATE_SSH_KEY >> $HOME/.ssh/id_rsa RUN git clone repo:repo.git
Note that this is a high-level, directional example and will require additional work to use in a live build.
Codeship Pro does not yet have an API for triggering builds, but we will be launching an API for this purpose later in 2017.