Last active
December 18, 2019 05:55
-
-
Save kudaliar032/2d914d9ec3a007b440ab1bf3248d9d0e to your computer and use it in GitHub Desktop.
Python Heroku GitLab CI/CD Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stages: | |
- test | |
- deploy | |
test_flask: | |
variables: | |
REDIS_URL: redis://redis:6379 | |
services: | |
- redis | |
image: python:3.7-alpine | |
stage: test | |
tags: | |
- docker | |
script: | |
- pip install -r requirements.txt | |
- python app.test.py | |
deploy_heroku: | |
image: kudaliar032/dpl | |
stage: deploy | |
only: | |
- master | |
tags: | |
- docker | |
script: | |
- dpl --provider=heroku --app=${APP_NAME} --api-key=${HEROKU_API} | |
environment: | |
name: heroku | |
url: https://${APP_NAME}.herokuapp.com/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import redis | |
import os | |
from flask import Flask | |
app = Flask(__name__) | |
cache = redis.from_url(os.environ.get("REDIS_URL")) | |
def get_hit_count(): | |
retries = 5 | |
while True: | |
try: | |
return cache.incr('hits') | |
except redis.exceptions.ConnectionError as exc: | |
if retries == 0: | |
raise exc | |
retries -= 1 | |
time.sleep(0.5) | |
@app.route('/') | |
def hello(): | |
count = get_hit_count() | |
return 'Hello World! Saya sudah terlihat {} kali.'.format(count) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from app import app | |
class BasicTestCase(unittest.TestCase): | |
def test_home(self): | |
tester = app.test_client(self) | |
response = tester.get('/', content_type='html/text') | |
self.assertEqual(response.status_code, 200) | |
if __name__ == '__main__': | |
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: gunicorn app:app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Click==7.0 | |
Flask==1.1.1 | |
gunicorn==20.0.4 | |
itsdangerous==1.1.0 | |
Jinja2==2.10.3 | |
MarkupSafe==1.1.1 | |
redis==3.3.11 | |
Werkzeug==0.16.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment