diff --git a/.github/workflows/learn-github-actions.yml b/.github/workflows/learn-github-actions.yml new file mode 100644 index 000000000..fa42c6d2e --- /dev/null +++ b/.github/workflows/learn-github-actions.yml @@ -0,0 +1,13 @@ +name: learn-github-actions +run-name: ${{ github.actor }} is learning GitHub Actions +on: [push] +jobs: + check-bats-version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - uses: actions/setup-node@v4 + with: + node-version: '20' + - run: npm install -g bats + - run: bats -v \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..4e20cfb54 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM ubuntu:22.04 + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + python3 python3-pip \ + && rm -rf /var/lib/apt/lists/* + +COPY requirements.txt /tmp/requirements.txt +RUN pip install --upgrade pip \ + && pip install --no-cache-dir -r /tmp/requirements.txt + +COPY /app /app +WORKDIR /app + +RUN useradd appuser \ + && chown -R appuser:appuser /app +USER appuser + +CMD ["python3", "app.py"] \ No newline at end of file diff --git a/M1-3-Ansible/build-and-run.yml b/M1-3-Ansible/build-and-run.yml new file mode 100644 index 000000000..c220bdc44 --- /dev/null +++ b/M1-3-Ansible/build-and-run.yml @@ -0,0 +1,53 @@ +--- +- name: Build/push/run locally + hosts: localhost + connection: local + gather_facts: false + + vars: + image_name: devops-programme + image_tag: v2 + container_name: devops-programme-container + container_port: 3000 + listen_port: 3000 + app_dir: "{{ (playbook_dir + '/..') | realpath }}" + dockerfile_relpath: "Dockerfile" + + tasks: + + - name: Compose image ref and Dockerfile absolute path + ansible.builtin.set_fact: + registry_image: "localhost:5000/{{ image_name }}:{{ image_tag }}" + dockerfile_fullpath: "{{ app_dir }}/{{ dockerfile_relpath }}" + + - name: Verify Dockerfile exists at repo root + ansible.builtin.stat: + path: "{{ dockerfile_fullpath }}" + register: dockerfile_stat + + - name: Fail if Dockerfile is missing + ansible.builtin.fail: + msg: "Dockerfile not found at {{ dockerfile_fullpath }}. Check app_dir/dockerfile_relpath." + when: not dockerfile_stat.stat.exists + + - name: Build image from repo root + community.docker.docker_image: + name: "{{ registry_image }}" + source: build + build: + path: "{{ app_dir }}" + dockerfile: "{{ dockerfile_relpath }}" + pull: true + push: false + + - name: Run (or update) container from local registry + community.docker.docker_container: + name: "{{ container_name }}" + image: "{{ registry_image }}" + state: started + recreate: true + restart_policy: unless-stopped + published_ports: + - "{{ listen_port | int }}:{{ container_port | int }}" + env: + PORT: "{{ listen_port | string }}" diff --git a/M1-3-Ansible/pull-and-start.yml b/M1-3-Ansible/pull-and-start.yml new file mode 100644 index 000000000..d282fcdea --- /dev/null +++ b/M1-3-Ansible/pull-and-start.yml @@ -0,0 +1,20 @@ +--- +- name: Deploy Docker image + hosts: localhost + vars: + image_name: devops-programme + image_tag: v1 + listen_port: 3000 + tasks: + - name: Pull Docker Image + docker_image: + name: "{{ image_name }}" + source: pull + tag: "{{ image_tag }}" + - name: Start Docker container + docker_container: + name: "{{ image_name }}" + image: "{{ image_name }}" + ports: + - "{{ listen_port | int }}:{{ listen_port | int }}" + state: started \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..134fc824f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0" +itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0" +jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0" +markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0" +werkzeug==3.0.0 ; python_version >= "3.10" and python_version < "4.0" \ No newline at end of file