Automate test and deploy using CircleCI with Django

Ken Kono
3 min readJun 12, 2019

--

Have you ever felt tired when you do test and deploy by a manual for your app? That is me. I would like to share how to avoid that tired action using CircleCI.

Environment

MacOS Mojave
Django 2.0.4
AWS EC2(Amazon Linux, User: ec2-user)
Docker

Below is the source code link.
https://github.com/kenkono/CircleCI_Docker-django_todo-

Generate a new SSH key

To access EC2 instance from CircleCI, we need to get SSH key. Note that CircleCI suggests the SSH key type ssh-keygen -m pem. https://circleci.com/docs/2.0/add-ssh-key/
So open the terminal and follow the below command.

# make new directory
$ mkdir ssh
# Generate a new key at the directory that we make before
$ ssh-keygen -m pem
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/kenkono/.ssh/id_rsa): ssh

Set up at EC2

We need to set up EC2 before setting CircleCI.
After entering the instance using SSH, follow the below command.

  • Git
# update yum at first
sudo yum update -y
sudo yum install git
  • Docker
sudo yum install -y docker
# start Docker
sudo service docker start
# add ec2-user at docker group
sudo usermod -a -G docker ec2-user
  • docker-compose
# change su
sudo -i
# install docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.11.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# add authority
chmod +x /usr/local/bin/docker-compose
# Cancel su
exit
# check the cocker-compose version
docker-compose --version
docker-compose version 1.11.2, build dfed245
  • Public key

Add Public key information that we made before.

vi .ssh/authorized_keys

Set up the CircleCI

2 step we should do.

  1. Register private key
  2. Register host name and user name about EC2 instance
  • Register private key
    After entering your repository, add your private key information as below. You can write any Hostname.
  • Register host name and user name about EC2 instance
    Register above information as Environment Variables.
    I think almost EC2 instance(Amazon Linux) has ec2-user@DomeinName .
    So we should register as below.
HOST_NAME = DomainName
USER_NAME = ec2-user

Write code to deploy CircleCI

Now we prepared for the auto deploy using CircleCI.Let’s write the code.CircleCI runs using the code at.circleci/config.yml when we push to the GitHub repository.

We would like to use docker-compose because the sample app uses a docker environment. Fortunately, we can choose the CircleCI machine at VM.So we choose the machine image .circleci/classic:edge .For another option, please kindly check CircleCI official document.
https://circleci.com/docs/2.0/executor-types/

That’s it. When you push your git repository to GitHub, CircleCI runs automatically followed by cofig.yml .

--

--