This template is aimed to help you set up a CI/CD development environment on the cloud!
Check my article about CI/CD!
We will be using the following tools for, but feel free to adapt to your project needs:
For the sake of this tutorial, Python is the language of choice to make things easier. But since we're using Docker, you can build anything with it!
image
|__cover.jpeg
- image directory has no practical use, only for reference and aesthetics.
scripts
|__create-ecs.sh
|__deploy.sh
- create-ecs.sh it's just a reference on how my ECS Cluster is configured. You should not run this script, adapt for your needs.
- deploy.sh is the script used by Travis to deploy our application on AWS.
src
|__main.py
- src is where your src code should be.
|__.gitignore
|__.travis.yml
|__docker-compose.yml
|__Dockerfile
|__ecs-params.yml
|__LICENSE
|__README.md
|__requirements.txt
- .gitignore stores the files that git will ignore if there are any changes.
- .travis.yml is the CI script for Travis.
- docker-compose.yml holds the settings for building and running the container.
- Dockerfile has the environment configuration to build your application.
- ecs-params.yml has the service settings for ECS.
- LICENSE is the repository License.
- README.md is the file you're reading right now.
- requirements.txt dependencies for our Python application.
Dockerfile and docker-compose.yml are responsible for the build. You need Docker and Docker Compose installed.
To build the Dockerfile, use the following command:
$ docker build -t cool-image-name .
Running:
$ docker run -p 80:8000 cool-image-name
You can use Docker Hub as your image repository, just change on the image on docker-compose file for your user and image name, following by the tag like this:
leozz37/cicd-example:latest
To push an image to Docker Hub, follow these commands:
$ docker build -t leozz37/cicd-example .
$ docker tag leozz37/cicd-example:latest leozz37/cicd-example:latest
$ docker push leozz37/cicd-example:latest
For docker-compose:
$ docker-compose up
We're using Travis CI building, running tests, and deploy. Check their documentation for more information. Access Travis website, log in with your Github account and enable your repository.
On our travis.yml, we're only using the deploy and before_install steps, but feel free to use all of them! They go in the following order:
- install:
- before_install:
- script:
- after_success:
- after_failure:
- after_script:
- before_deploy:
- deploy:
- after_deploy:
I recommend you use them to install dependencies, run tests, generate alerts, etc.
Travis lets you set up environment variables on the settings tab of the project. For deploy, should look like this:
Amazon Web Services (AWS) is our tool of choice. They have so many tools and services to offer, we don't have a general sample to use. But since we're using Docker to deploy our system, we'll be using ECS to deploy ou application to the world. I highly recommend that you READ THE DOCS, since it can charge you for its use. But for a small application, AWS free tier is enough.
For the ECS setup, create-ecs.sh offers an example for the commands used to create the sample for this project. It has some env variables, that you can swap for your credentials and configs. It shows two ways of hosting the image, through DockerHub and ECR, pick what fits you better.
There's basically three ways of setting deploy.sh up. The first is through Docker Hub, so you can remove ECR's code. The second is through ECR, then you can remove Docker Hub code. The third (and probably better one) is setting up AWS Blue-Green Deployment and use ecs-deploy lib for deploying.
We'll be using (kinda) free tools for this example, but feel free to use and explore other tools! Here are some recommendations:
Instead of Github, you can use Gitlab or Bitbucket.
For Docker, you can't go much farther than Kubernetes.
Travis has a lot of competitors, such as appveyor, Jenkins, CodePipeline, CircleCI, Gitlab CI Tool and many more...
For AWS it depends on your system. If you hosting a static web page, you can use Github Pages, if it's a more complex system there's Google Cloud, Azure, Heroku and many many more.
Check my book and tools recommendations for software engineering, for more CI/CD tools.
Feel free to send me an email or follow my Github account!