aboutsummaryrefslogtreecommitdiff
path: root/ansible/deploy.yaml
blob: 911868db9b3aa2bfa7250b4a203d9190da535e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
- name: Deploy to production
  hosts: all
  remote_user: root

  vars:
    app_dir: "/home/{{ username }}/invoice"
    nvm_sh_dir: "/home/{{ username }}/.nvm/nvm.sh"

  tasks:
    - name: Create system user
      ansible.builtin.user:
        name: "{{ username }}"
        group: users
        create_home: yes
        shell: /bin/bash
        append: yes

    - name: Check if invoice directory exists
      ansible.builtin.file:
        path: "{{ app_dir }}"
        state: directory
        owner: "{{ username }}"
        group: users
        mode: "0755"

    - name: Add github to known hosts
      become: yes
      become_user: "{{ username }}"
      shell: |
        mkdir -p home/{{ username }}/.ssh/
        ssh-keyscan github.com >> home/{{ username }}/.ssh/known_hosts
      args:
        executable: /bin/bash


    - name: Checkout repo
      ansible.builtin.git:
        repo: "{{ git_repo }}"
        dest: "{{ app_dir }}"
        version: main
        force: yes
      become: yes
      become_user: "{{ username }}"

    - name: Check if nvm is installed
      ansible.builtin.stat:
        path: "{{ nvm_sh_dir }}"
      register: nvm_stat

    - name: Install nvm
      shell: >
        curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh
        | bash
      when: not nvm_stat.stat.exists
      become: yes
      become_user: "{{ username }}"

    - name: Install node.js
      shell: |
        . {{ nvm_sh_dir }}
        nvm install 24.13.0
      args:
        executable: /bin/bash
        creates: "/home/{{ username }}/.nvm/versions/node/v24.13.0"
      become: yes
      become_user: "{{ username }}"

    - name: Install pm2
      shell: |
        . {{ nvm_sh_dir }}
        npm list -g pm2 || npm install -g pm2
      args:
        executable: /bin/bash
      become: yes
      become_user: "{{ username }}"

    - name: Install deps
      shell: |
        . {{ nvm_sh_dir }}
        npm install
      args:
        executable: /bin/bash
        chdir: "{{ app_dir }}"
      become: yes
      become_user: "{{ username }}"


    - name: Build nextjs server
      shell: |
        . {{ nvm_sh_dir }}
        npm run build
      args:
        executable: /bin/bash
        chdir: "{{ app_dir }}"
      become: yes
      become_user: "{{ username }}"

    - name: Sync .next/static
      shell: |
        rsync -avP {{ app_dir }}/.next/static {{ app_dir }}/.next/standalone/.next/
      become: yes
      become_user: "{{ username }}"


    - name: Sync public dir
      shell: |
        rsync -avP {{ app_dir }}/public {{ app_dir }}/.next/standalone/
      become: yes
      become_user: "{{ username }}"

    - name: Start/restart pm2 process
      shell: |
        . {{ nvm_sh_dir }}
        pm2 restart invoice-app || pm2 start server.js --name invoice-app
      args:
        executable: /bin/bash
        chdir: "{{ app_dir }}/.next/standalone"
      become: yes
      become_user: "{{ username }}"