From ad4636e4b8a4059719ee41e622c1d66cfdf7ca27 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Wed, 21 May 2025 21:15:44 +0300 Subject: [PATCH] test pipeline 1 --- .gitea/workflows/ci.yaml | 44 ++++++++++++++++++++++++++++++++++++++++ README copy.md | 1 + ansible/inventory.yml | 37 +++++++++++++++++++++++++++++++++ ansible/playbook.yml | 14 +++++++++++++ app.py | 9 ++++++++ dockerfile | 7 +++++++ jenkinsfile | 35 ++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ test_app.py | 8 ++++++++ 9 files changed, 158 insertions(+) create mode 100644 .gitea/workflows/ci.yaml create mode 100644 README copy.md create mode 100644 ansible/inventory.yml create mode 100644 ansible/playbook.yml create mode 100644 app.py create mode 100644 dockerfile create mode 100644 jenkinsfile create mode 100644 requirements.txt create mode 100644 test_app.py diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml new file mode 100644 index 0000000..227e175 --- /dev/null +++ b/.gitea/workflows/ci.yaml @@ -0,0 +1,44 @@ +name: CI Pipeline +on: + push: + branches: + - main + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + - name: Install dependencies + run: pip install flake8 + - name: Run linting + run: flake8 . --max-line-length=120 + + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + - name: Install dependencies + run: pip install -r requirements.txt + - name: Run tests + run: pytest test_app.py + + build: + runs-on: ubuntu-latest + needs: [lint, test] + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Build Docker image + run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/my-flask-app:${{ github.sha }} . diff --git a/README copy.md b/README copy.md new file mode 100644 index 0000000..e6f6b35 --- /dev/null +++ b/README copy.md @@ -0,0 +1 @@ +# test1 \ No newline at end of file diff --git a/ansible/inventory.yml b/ansible/inventory.yml new file mode 100644 index 0000000..0cb4c3e --- /dev/null +++ b/ansible/inventory.yml @@ -0,0 +1,37 @@ +all: + hosts: + first_vm: + ansible_host: 192.168.0.23 + second_vm: + ansible_host: 192.168.0.27 + tretiy_vm: + ansible_host: 192.168.0.28 + vars: + ansible_user: ansible + ansible_password: 123qwe123 + ansible_connection: ssh + ansible_port: 22 + + +#----------------------------------------------------------------------------------------------------------------- + +#all: +# hosts: +# first_vm: +# ansible_host: 192.168.0.23 +# ansible_user: ansible +# ansible_password: 123qwe123 +# ansible_connection: ssh +# ansible_port: 22 +# second_vm: +# ansible_host: 192.168.0.27 +# ansible_user: ansible +# ansible_password: 123qwe123 +# ansible_connection: ssh +# ansible_port: 22 +# tretiy_vm: +# ansible_host: 192.168.0.28 +# ansible_user: ansible +# ansible_password: 123qwe123 +# ansible_connection: ssh +# ansible_port: 22 \ No newline at end of file diff --git a/ansible/playbook.yml b/ansible/playbook.yml new file mode 100644 index 0000000..fd5a74a --- /dev/null +++ b/ansible/playbook.yml @@ -0,0 +1,14 @@ +--- + - name: install and start nginx + hosts: all + become: yes + tasks: + - name: install ngnx + apt: + name: nginx + state: latest + - name: start engineX + service: + name: nginx + state: started + enabled: yes \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..d96fe31 --- /dev/null +++ b/app.py @@ -0,0 +1,9 @@ +from flask import Flask +app = Flask(__name__) + +@app.route('/') +def hello(): + return 'Hello, DevOps!' + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..dfe3af6 --- /dev/null +++ b/dockerfile @@ -0,0 +1,7 @@ +FROM python:3.9-slim +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY . . +EXPOSE 5000 +CMD ["python", "app.py"] \ No newline at end of file diff --git a/jenkinsfile b/jenkinsfile new file mode 100644 index 0000000..beac4fc --- /dev/null +++ b/jenkinsfile @@ -0,0 +1,35 @@ +pipeline { + agent {label 'vm-oracle'} + environment{ + IMAGE_NAME = "test-flask-app2" + IMAGE_TAG = "${env.BUILD_NUMBER}" + CONTAINER_NAME = "test-app" + } + stages{ + stage('Checkout'){ + steps{ + git url: 'https://github.com/ScoobyBo/test1.git', branch: 'main' + } + } + stage('Build docker image'){ + steps{ + sh 'docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .' + } + } + stage('deploy container'){ + steps{ + sh 'docker run -d --name $CONTAINER_NAME -p 5000:5000 ${IMAGE_NAME}:${IMAGE_TAG}' + } + } + + } + post{ + always { + sh "echo 'Cleaning up container ${CONTAINER_NAME} and удали свою жопу дубина ${IMAGE_NAME}:${IMAGE_TAG}'" + sh "docker stop ${CONTAINER_NAME} || true" + sh "docker rm ${CONTAINER_NAME} || true" + sh "docker rmi ${IMAGE_NAME}:${IMAGE_TAG} || true" + } + + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..44f269d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask==2.0.1 +pytest==7.4.0 +Werkzeug==2.0.3 \ No newline at end of file diff --git a/test_app.py b/test_app.py new file mode 100644 index 0000000..1d14736 --- /dev/null +++ b/test_app.py @@ -0,0 +1,8 @@ +from app import app +import pytest + +def test_hello(): + with app.test_client() as client: + response = client.get('/') + assert response.data == b'Hello, DevOps!' + assert response.status_code == 200 \ No newline at end of file