|
6 | 6 | - hosts: "{{ host }}" |
7 | 7 | become: yes |
8 | 8 |
|
9 | | - roles: |
10 | | - # Build & run project's backend |
11 | | - - deploy/cgapp_backend |
12 | | - # Configure and run project's webserver |
13 | | - - deploy/cgapp_webserver |
14 | | - # Configure and run project's database |
15 | | - - deploy/cgapp_database |
| 9 | + tasks: |
| 10 | + # |
| 11 | + # Create a new Docker network for connect all project elements into one network. |
| 12 | + # |
| 13 | + - name: Create a new Docker network ({{ network_name }}) |
| 14 | + docker_network: |
| 15 | + name: "{{ network_name }}" |
| 16 | + |
| 17 | + # |
| 18 | + # Check all necessary project folders for future use. |
| 19 | + # |
| 20 | + - name: Check, if ./backend directory is exists |
| 21 | + stat: |
| 22 | + path: ./backend |
| 23 | + register: backend_folder |
| 24 | + |
| 25 | + - name: Check, if ./frontend directory is exists (for static files) |
| 26 | + stat: |
| 27 | + path: ./frontend |
| 28 | + register: frontend_folder |
| 29 | + |
| 30 | + - name: Check, if ./webserver directory is exists |
| 31 | + stat: |
| 32 | + path: ./webserver |
| 33 | + register: webserver_folder |
| 34 | + |
| 35 | + - name: Check, if ./database directory is exists |
| 36 | + stat: |
| 37 | + path: ./database |
| 38 | + register: database_folder |
| 39 | + |
| 40 | + # |
| 41 | + # The block that builds and runs the backend part of the project. |
| 42 | + # |
| 43 | + - name: Backend block |
| 44 | + block: |
| 45 | + - name: Builds Docker image for backend |
| 46 | + docker_image: |
| 47 | + name: cgapp_backend # name of the backend image |
| 48 | + build: |
| 49 | + path: ./backend # folder with Dockerfile |
| 50 | + pull: yes |
| 51 | + source: build |
| 52 | + |
| 53 | + - name: Runs Docker container with backend |
| 54 | + docker_container: |
| 55 | + name: cgapp-backend # name of the backend container |
| 56 | + image: cgapp_backend:latest |
| 57 | + recreate: yes |
| 58 | + networks: |
| 59 | + - name: "{{ network_name }}" |
| 60 | + volumes: |
| 61 | + # If ./frontend folder is exists, playbook will include a `dist` folder to container, |
| 62 | + # or include a default placeholder webpage (to sure, what everything is working). |
| 63 | + - "{{ './frontend/dist:/static:ro' if frontend_folder.stat.exists else './backend/static:/static:ro' }}" |
| 64 | + ports: |
| 65 | + - "5000:5000" |
| 66 | + state: started |
| 67 | + # Run block only if ./backend is a folder and exists. |
| 68 | + when: backend_folder.stat.exists and backend_folder.stat.isdir |
| 69 | + |
| 70 | + # |
| 71 | + # The block that builds and runs the webserver part of the project. |
| 72 | + # |
| 73 | + - name: Webserver block |
| 74 | + block: |
| 75 | + - name: Builds Docker image for webserver |
| 76 | + docker_image: |
| 77 | + name: cgapp_webserver # name of the webserver image |
| 78 | + build: |
| 79 | + path: ./webserver |
| 80 | + pull: yes |
| 81 | + source: build |
| 82 | + |
| 83 | + - name: Runs Docker container with webserver |
| 84 | + docker_container: |
| 85 | + name: cgapp-webserver # name of the webserver container |
| 86 | + image: cgapp_webserver:latest |
| 87 | + recreate: yes |
| 88 | + networks: |
| 89 | + - name: "{{ network_name }}" |
| 90 | + env: |
| 91 | + APP_DOMAIN: "{{ host }}" |
| 92 | + ports: |
| 93 | + - "80:80" |
| 94 | + state: started |
| 95 | + # Run block only if ./webserver is a folder and exists. |
| 96 | + when: webserver_folder.stat.exists and webserver_folder.stat.isdir |
| 97 | + |
| 98 | + # |
| 99 | + # The block that builds and runs the database part of the project. |
| 100 | + # |
| 101 | + - name: Database block |
| 102 | + block: |
| 103 | + - name: Builds Docker image for database |
| 104 | + docker_image: |
| 105 | + name: cgapp_database # name of the database image |
| 106 | + build: |
| 107 | + path: ./database # folder with Dockerfile |
| 108 | + pull: yes |
| 109 | + source: build |
| 110 | + |
| 111 | + - name: Runs Docker container with database |
| 112 | + docker_container: |
| 113 | + name: cgapp-database # name of the database container |
| 114 | + image: cgapp_database:latest |
| 115 | + recreate: yes |
| 116 | + networks: |
| 117 | + - name: "{{ network_name }}" |
| 118 | + ports: |
| 119 | + - "5432:5432" |
| 120 | + state: started |
| 121 | + # Run block only if ./database is a folder and exists. |
| 122 | + when: database_folder.stat.exists and database_folder.stat.isdir |
0 commit comments