The goal of this kata is to practice setting up a continuous integration / continuous delivery system.
We start with my solution to the Roman Numerals kata (also known as the Reverse Roman Numerals kata). We've added on a simple web UI.
You will take this simple web app and set up a continuous delivery pipeline to run it in "production."
Here are the steps:
You will need your own Roman Numerals repository. To fork this repository:
- Visit https://github.com/rkasper/roman-python.
- Click the
Forkbutton. - In your GitHub account, visit the forked repository you just created. Note the URL of your Roman Numerals repository.
- In PyCharm Professional, create a new project from version control.
- Select
File / Project from Version Control.... - For
Version Control, selectGit. - For the
URL, copy-paste the URL of your Roman Numerals repository. - Click the
Clonebutton.
- Select
- If you notice an error like no such file or directory: venv/bin/python, then install a new Python virtual environment. One way to do this is to select
Preferences / Project / Python Interpreter. Click the gear icon, and add a new virtual environment. On my machine, PyCharm finds thepython3executable. - If you notice an error like no such file or directory: venv/bin/behave, then either:
- Open file
requirements.txtand follow the instructions at the top of the editor window, or - Type this inside a terminal in your project's root directory:
$ venv/bin/pip install -r requirements.txt - Run all the tests. In the Project browser at the top-left of the IDE, right-click
app / test. SelectRun 'Python tests in test...'. Confirm that all the tests are green.- 🤔 Open each file in the
testdirectory. What does each file do?
- 🤔 Open each file in the
- Run the web app. In the Project browser, right-click file
flask-app.py. SelectRun flask-app. Visit http://127.0.0.1:5000 and play with the web app.- 🤔 Open file
flask-app.py. How does it work? - 🤔 Where is the UI code for the web app?
- 🤔 Where is the Roman numerals business logic?
- 🤔 Is there any storage?
- 🤔 Are there any other files? What does each file do?
- 🤔 Open file
- Open file
We'll use DigitalOcean App Platform, a super-simple way to set up a continuous delivery pipeline and deploy an application.
- Login to DigitalOcean, either using your own account or using an account provided by your instructor.
- If you create a new account, DigitalOcean will give you $200 in credits for 2 months of use. You'll have to give them a form of payment. Don't worry! You won't get charged, as long as you remember to shut down any services you launch today.
- In the DigitalOcean UI, create your own Roman Python project.
- Navigate to
Projects / New Project. Name your projectRoman Pythonand clickCreate Project. - In the
Move Resourcesdialog, clickSkip for now.
- Navigate to
- Create your Roman Python app. Click
Create App.- At the prompt,
Create Resource from Source Code / Service Provider, selectGitHub. - For
Repository, login to your GitHub account and select your Roman Python repo. - Notice that
Autodeployis checked.- 🤔 What do you think Autodeploy does?
- Click
Next.
- At the prompt,
- On the
Edit Planpage, select these options:Plan:BasicSize:$5/mo - Basic- Click the
Backbutton. - Click
Next.
- On the
Environment Variablespage, just take a look. ClickNext.- 🤔 What might you use environment variables for?
- On the
Infopage, selectApp Infoto edit more project details.- Edit your app name. Rename it
roman-python. - Choose your Roman Python project
- Click
Save, thenNext.
- Edit your app name. Rename it
- On the
Reviewpage, take a look the settings. ClickCreate Resource. - Go to
Build Logsand watch your app build. - Your app is now running in a production environment. To run your app's web UI, click the
Live Appbutton.
One way we reduce risk is by reducing batch size: our CD pipeline automatically rebuilds and redeploys every time we introduce a code change. Remember that Autodeploy setting? We left it turned on. Whenever we push new code to the repository, our CD pipeline will notice, clone the repository, build it, and deploy it.
- 🤔 Try it: change some code on your machine, run the tests to make sure everything is "green", and commit and push to git.
- 🤔 What did you change?
- 🤔 Exactly what happened when you pushed to git?
- 🤔 What are the risks of automatic deployment?
Another way we reduce risk is via test-driven development. We have a test suite that we run on our machine while writing code. We can enhance the safety of our deployments by also running these tests in the build & deploy environment.
- In
App Settings / Components, select yourroman-pythonapp. - In
Commands, clickEdit. - Add a
Build Commandto run your tests:python app/test/tests.py. ClickSave. - Notice that when we change our app's configuration, the CD pipeline rebuilds it. Go to
Build Logsand watch the new build. Notice that the pipeline now runs tests, and that all the tests passed.
- On your machine, intentionally introduce a test that fails. Commit and push your code to the repository.
- 🤔 What happens in the CD pipeline?
- On your machine, make that test pass . Commit and push your code to the repository.
- 🤔 What happens in the CD pipeline?