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..ea6deaa --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,50 @@ +pipeline { + agent any + environment { + INVENTORY = '/var/lib/jenkins/ansible/inventory' + } + + stages { + + stage('Checkout') { + steps { + git url: 'https://github.com/techcoms/backend-python.git',credentialsId: 'github-creds', branch:'feature' + } + } + stage('Build Docker Image') { + steps { + sh "docker build -t techoms/myapp:latest ." + } + } + // stage('Push Docker Image') { + // steps { + // withCredentials([usernamePassword( + // credentialsId: 'dockerhub-creds', + // usernameVariable: 'USER', + // passwordVariable: 'PASS' + // )]) { + // sh ''' + // echo "$PASS" | docker login -u "$USER" --password-stdin + // docker push techoms/myapp:latest + // docker logout + // ''' + // } + // } + // } + + stage('Deploy with Ansible') { + steps { + withCredentials([sshUserPrivateKey( + credentialsId: 'deploy-ssh-key', + keyFileVariable: 'SSH_KEY', + usernameVariable: 'SSH_USER' + )]) { + sh ''' + ansible nodes -i "$INVENTORY" -m ping --private-key "$SSH_KEY" + ansible-playbook -i "$INVENTORY" deploy.yml -u "$SSH_USER" --private-key "$SSH_KEY" + ''' + } + } + } + } +} 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/deploy.yml b/deploy.yml new file mode 100644 index 0000000..7f3666b --- /dev/null +++ b/deploy.yml @@ -0,0 +1,15 @@ +--- +- name: Run Docker container on nodes + hosts: nodes + become: yes + + tasks: + + - name: Run Docker container + community.docker.docker_container: + name: simple-app + image: techcoms/my-app + state: started + restart_policy: always + published_ports: + - "5001:5001" 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