diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0cd04dc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:alpine3.7 +COPY . /app +WORKDIR /app +RUN pip install -r requirements.txt +EXPOSE 5001 +ENTRYPOINT [ "python" ] +CMD [ "demo.py" ] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..615ffb9 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,46 @@ +pipeline{ + options { + buildDiscarder(logRotator(numToKeepStr: '2', artifactNumToKeepStr: '6')) + } + agent any + environment { + DOCKERHUB_REPO = "techcoms/backend-python" + GITHUB_URL = "${params.url}" + BRANCH = "${params.branch}" + + } + stages{ + stage("git checkout"){ + steps{ + git branch: "${BRANCH}", credentialsId: 'github-creds', url: "${GITHUB_URL}" + } + } + + stage("build docker image"){ + steps { + sh "docker build -t ${DOCKERHUB_REPO}:${BUILD_NUMBER} ." + } + } + stage('run a docker container'){ + steps{ + sh "docker run -d -p 8088:5001 ${DOCKERHUB_REPO}:${BUILD_NUMBER}" + } + } + stage("login to dockerhub and push image"){ + steps { + withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) { + sh "docker login -u $USERNAME -p $PASSWORD " + sh "docker push ${DOCKERHUB_REPO}:${BUILD_NUMBER}" + + } + } + } + } + post{ + changed{ + mail to: "techcomsdevops@gmail.com", + subject: "jenkins build:${currentBuild.currentResult}: ${env.JOB_NAME}", + body: "${currentBuild.currentResult}: Job ${env.JOB_NAME}\nMore Info can be found here: ${env.BUILD_URL}" + } + } +} diff --git a/README.md b/README.md index 4d47f7e..20d3fde 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# backend-python \ No newline at end of file +# python-flask-docker-project \ No newline at end of file diff --git a/demo.py b/demo.py new file mode 100644 index 0000000..0966f90 --- /dev/null +++ b/demo.py @@ -0,0 +1,10 @@ +from flask import Flask +app = Flask(__name__) + +@app.route('/') +def hello(): + return "welcome to the flask tutorials" + + +if __name__ == "__main__": + app.run(host ='0.0.0.0', port = 5001, debug = True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..db07929 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +mysql-connector-python +boto3