Skip to content

Instantly share code, notes, and snippets.

@kudaliar032
Last active December 18, 2019 05:55
Show Gist options
  • Save kudaliar032/2d914d9ec3a007b440ab1bf3248d9d0e to your computer and use it in GitHub Desktop.
Save kudaliar032/2d914d9ec3a007b440ab1bf3248d9d0e to your computer and use it in GitHub Desktop.
Python Heroku GitLab CI/CD Example
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/
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)
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()
web: gunicorn app:app
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