self-hosting/roles/gitea/tasks/main.yml

247 lines
5.5 KiB
YAML
Raw Normal View History

2022-04-07 21:00:33 +02:00
---
2022-06-25 14:48:38 +02:00
- name: Include vault variables
include_vars: vault.yml
- name: Install needed packages
2022-04-07 21:00:33 +02:00
become: true
apt:
2022-04-08 00:10:43 +02:00
name:
- git
- unzip
- gpg # to verify binary
- acl # for become_user: git
2022-04-07 21:00:33 +02:00
state: present
- name: Create git group
become: true
group:
name: git
system: true
- name: Create git user
become: true
user:
name: git
group: git
append: true
groups:
- sudo
- mail
create_home: true
home: /home/git
shell: /bin/bash
system: true
2022-04-07 21:07:45 +02:00
2022-04-10 17:26:53 +02:00
- name: Set sudoer permissions to git user
become: true
copy:
2022-04-11 15:05:40 +02:00
content: 'git ALL=(root) NOPASSWD:/usr/bin/systemctl'
2022-04-10 17:26:53 +02:00
dest: /etc/sudoers.d/git
owner: root
group: root
mode: 0440
validate: /usr/sbin/visudo -csf %s
2022-04-07 21:07:45 +02:00
- name: Create /var/lib/gitea directory
become: true
file:
path: /var/lib/gitea
state: directory
owner: git
group: git
mode: 0750
recurse: true
- name: Create /var/lib/gitea subdirectories
become: true
file:
path: "/var/lib/gitea/{{ item }}"
state: directory
owner: git
group: git
mode: 0750
recurse: true
with_items:
- custom
- data
- log
- name: Create /etc/gitea directory
become: true
file:
path: /etc/gitea
state: directory
owner: git
2022-04-07 21:07:45 +02:00
group: git
mode: 0750
2022-04-07 21:07:45 +02:00
recurse: false
2022-04-07 23:18:20 +02:00
- name: Find latest gitea version
uri:
url: https://dl.gitea.io/gitea/version.json
register: gitea_binary
- name: Find if latest gitea version is installed
stat:
path: "/home/git/gitea-{{ gitea_binary.json.latest.version }}"
register: latest_gitea_binary
2022-04-07 23:18:20 +02:00
- name: Set gitea binary architecture to amd64
set_fact:
gitea_binary_arch: amd64
when: ansible_facts['architecture'] == 'x86_64'
- name: Set gitea binary architecture to arm-6
set_fact:
2022-04-09 13:03:23 +02:00
gitea_binary_arch: arm-6
2022-04-07 23:18:20 +02:00
when: ansible_facts['architecture'] != 'x86_64'
- name: Get latest gitea binary
become: true
get_url:
url: "https://dl.gitea.io/gitea/{{ gitea_binary.json.latest.version }}/gitea-{{ gitea_binary.json.latest.version }}-linux-{{ gitea_binary_arch }}"
dest: "/home/git/gitea-{{ gitea_binary.json.latest.version }}"
owner: git
group: git
mode: 0664
when: not latest_gitea_binary.stat.exists
2022-04-09 11:04:23 +02:00
notify:
2022-04-09 12:51:52 +02:00
- Receive gitea pgp key
- Download gitea asc file
- Verify gitea binary with gpg
2022-04-07 23:18:20 +02:00
2022-04-09 11:04:23 +02:00
- name: Verify downloaded binary
meta: flush_handlers
2022-04-08 00:10:43 +02:00
2022-04-07 23:18:20 +02:00
- name: Copy gitea binary to global location
become: true
copy:
src: "/home/git/gitea-{{ gitea_binary.json.latest.version }}"
dest: /usr/local/bin/gitea
remote_src: true
owner: root
group: root
mode: 0755
2022-06-08 22:14:29 +02:00
- name: Copy /etc/systemd/system/gitea.service
2022-04-07 23:18:20 +02:00
become: true
copy:
src: gitea.service
dest: /etc/systemd/system/gitea.service
owner: root
group: root
mode: 0644
notify:
- Reload systemd daemon
- Start gitea service
2022-04-09 13:30:32 +02:00
- name: Copy /etc/gitea/app.ini
become: true
template:
src: app.ini.j2
dest: /etc/gitea/app.ini
owner: git
group: git
mode: 0640
notify:
2022-10-01 00:41:21 +02:00
- Restart gitea service
- name: Make sure systemd daemon is reloaded
2022-04-09 13:30:32 +02:00
meta: flush_handlers
- name: Make sure gitea is running
become: true
systemd:
name: gitea
state: started
enabled: true
2022-04-09 13:30:32 +02:00
- name: Copy gitea_backup.sh script
become: true
template:
src: gitea_backup.sh.j2
2022-04-09 13:46:41 +02:00
dest: /home/git/gitea_backup.sh
2022-04-09 13:30:32 +02:00
owner: git
group: git
mode: 0775
- name: Create gitea-dumps directory
become: true
file:
path: /home/git/gitea-dumps
state: directory
owner: git
group: git
mode: 0755
- name: Set today's string for zipfile name
set_fact:
today: "{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}"
2022-08-21 16:15:44 +02:00
- name: Ask if we push latest gitea_dump zipfile
pause: # today's gitea dump zipfile must be in the manager's /tmp
prompt: Push latest gitea dump? [yes/no]
echo: true
register: push_latest_gitea_dump
2022-04-13 22:21:05 +02:00
- name: Push latest gitea_dump zipfile
become: true
copy:
src: "/tmp/gitea-dump-{{ today }}.zip"
dest: "/home/git/gitea-dumps/gitea-dump-{{ today }}.zip"
owner: git
group: git
mode: 0640
2022-08-28 21:56:34 +02:00
when: push_latest_gitea_dump.user_input == 'yes'
- name: Deploy repos
become: true
become_user: git
command:
cmd: "/home/git/gitea_backup.sh restore /home/git/gitea-dumps/gitea-dump-{{ today }}.zip"
creates: /home/git/gitea-repositories # when this dir exists, the command won't run, so we don't overwrite existing repos
2022-08-28 22:17:48 +02:00
2022-09-28 22:03:57 +02:00
- name: Setup gitea-backup crontab
become: true
copy:
src: gitea-backup.cron
dest: /etc/cron.d/gitea-backup
mode: 0644
- name: Generate SSH keys for git
become: true
become_user: git
openssh_keypair:
path: ~/.ssh/id_rsa
type: rsa
comment: "git@{{ ansible_fqdn }}"
register: ssh_key
- name: Get previously added SSH keys
uri:
url: https://git.tunuifranken.info/api/v1/user/keys
method: GET
user: "{{ gitea_user }}"
password: "{{ gitea_pass }}"
force_basic_auth: true
register: present_ssh_keys
- name: List SSH fingerprints
set_fact:
present_ssh_fingerprints: "{{ present_ssh_keys.json | map(attribute='fingerprint') }}"
- name: Add SSH key using Gitea's API
uri:
url: https://git.tunuifranken.info/api/v1/user/keys
method: POST
user: "{{ gitea_user }}"
password: "{{ gitea_pass }}"
force_basic_auth: true
status_code: 201
body_format: json
body:
key: "{{ ssh_key.public_key | trim }}"
read_only: false
title: "{{ ssh_key.comment | trim }}"
when: ssh_key.fingerprint not in present_ssh_fingerprints