Skip to content

Commit f395277

Browse files
authored
Merge pull request roles-ansible#92 from roles-ansible/forgejo
Option to install Forgejo
2 parents 85f20a7 + 23cb86c commit f395277

File tree

15 files changed

+240
-22
lines changed

15 files changed

+240
-22
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[![Ansible Galaxy](https://raw.githubusercontent.com/roles-ansible/ansible_role_gitea/main/.github/galaxy.svg?sanitize=true)](https://galaxy.ansible.com/do1jlr/gitea) [![MIT License](https://raw.githubusercontent.com/roles-ansible/ansible_role_gitea/main/.github/license.svg?sanitize=true)](https://github.com/roles-ansible/ansible_role_gitea/blob/main/LICENSE)
22

3-
ansible role gitea
4-
===================
3+
ansible role gitea/forgejo
4+
============================
55

6-
This role installs and manages [gitea](https://gitea.io) - Git with a cup of tea. A painless self-hosted Git service. Gitea is a community managed lightweight code hosting solution written in Go.
6+
This role installs and manages [gitea](https://gitea.io) or [forgejo](https://forgejo.org). A painless self-hosted Git service. Gitea is a community managed lightweight code hosting solution written in Go. Forgejo is a fork of it.
77
[Source code & screenshots](https://github.com/go-gitea/gitea).
8+
[Source code forgejo](https://codeberg.org/forgejo/forgejo)
89

910
## Sample example of use in a playbook
1011

@@ -20,17 +21,24 @@ The following code has been tested with the latest Debian Stable, it should work
2021
vars:
2122
# Here we assume we are behind a reverse proxy that will
2223
# handle https for us, so we bind on localhost:3000 using HTTP
24+
# see https://docs.gitea.io/en-us/reverse-proxies/#nginx
2325
gitea_fqdn: 'git.example.com'
2426
gitea_root_url: 'https://git.example.com'
2527
gitea_protocol: http
26-
2728
gitea_start_ssh: true
2829
```
2930
3031
Variables
3132
-----------
3233
Here is a deeper insight into the variables of this gitea role. For the exact function of some variables and the possibility to add more options we recommend a look at this [config cheat sheet](https://docs.gitea.io/en-us/config-cheat-sheet/).
3334
35+
### Chose between gitea and forgejo
36+
There is a fork of gitea called forgejo. Why? Read the [forgejo FAQ](https://forgejo.org/faq/).
37+
You have the option to choose between [gitea](https://gitea.io) and [forgejo](https://forgejo.org) by modifying the ``gitea_fork`` variable.
38+
| variable name | default value | description |
39+
| ------------- | ------------- | ----------- |
40+
| `gitea_fork` | `gitea` | optional choose to install forgejo instead of gitea by setting this value to `forgejo`. |
41+
3442
### gitea update mechanism
3543
To determine which gitea version to install, you can choose between two variants.
3644
Either you define exactly which release you install. Or you use the option ``latest`` to always install the latest release from the [gitea releases](https://github.com/go-gitea/gitea/releases/latest).
@@ -41,6 +49,7 @@ Either you define exactly which release you install. Or you use the option ``lat
4149
| `gitea_version` | `latest` | Define either the exact release to install *(eg. `1.16.0`)* or use ``latest`` *(default)* to install the latest release. |
4250
| `gitea_version_check` | `true` | Check if installed version != `gitea_version` before initiating binary download |
4351
| `gitea_gpg_key` | `7C9E68152594688862D62AF62D9AE806EC1592E2` | the gpg key the gitea binary is signed with |
52+
| `gitea_forgejo_gpg_key` | `EB114F5E6C0DC2BCDD183550A4B61A2DC5923710` | the gpg key the forgejo binary is signed with |
4453
| `gitea_gpg_server` | `hkps://keys.openpgp.org` | A gpg key server where this role can download the gpg key |
4554
| `gitea_backup_on_upgrade` | `false` | Optionally a backup can be created with every update of gitea. |
4655
| `gitea_backup_location` | `{{ gitea_home }}/backups/` | Where to store the gitea backup if one is created with this role. |
@@ -54,6 +63,7 @@ Either you define exactly which release you install. Or you use the option ``lat
5463
| `gitea_home` | `/var/lib/gitea` | Base directory to work |
5564
| `gitea_user_home` | `{{ gitea_home }}` | home of gitea user |
5665
| `gitea_executable_path` | `/usr/local/bin/gitea` | Path for gitea executable |
66+
| `gitea_forgejo_executable_path` | `/usr/local/bin/forgejo` | Path for forgejo executable |
5767
| `gitea_configuraion_path` | `/etc/gitea` | Where to put the gitea.ini config |
5868
| `gitea_shell` | `/bin/false` | UNIX shell used by gitea. Set it to `/bin/bash` if you don't use the gitea built-in ssh server. |
5969
| `gitea_systemd_cap_net_bind_service` | `false` | Adds `AmbientCapabilities=CAP_NET_BIND_SERVICE` to systemd service file |
@@ -97,6 +107,7 @@ Either you define exactly which release you install. Or you use the option ``lat
97107
| `gitea_enable_repo_signing_extra` | | you can use this variable to pass additional config parameters in the `[repository.signing]` section of the config. |
98108

99109
### CORS ([cors](https://docs.gitea.io/en-us/config-cheat-sheet/#cors-cors))
110+
| variable name | default value | description |
100111
| ------------- | ------------- | ----------- |
101112
| `gitea_enable_cors` | `false` | enable cors headers (disabled by default) |
102113
| `gitea_cors_scheme` | `http` | scheme of allowed requests |
@@ -133,7 +144,6 @@ Either you define exactly which release you install. Or you use the option ``lat
133144
| `gitea_root_url` | `http://localhost:3000` | Root URL used to access your web app (full URL) |
134145
| `gitea_http_listen` | `127.0.0.1` | HTTP listen address |
135146
| `gitea_http_port` | `3000` | Bind port *(redirect from `80` will be activated if value is `443`)* |
136-
137147
| `gitea_start_ssh` | `true` | When enabled, use the built-in SSH server. |
138148
| `gitea_ssh_domain` | `{{ gitea_http_domain ` | Domain name of this server, used for displayed clone URL |
139149
| `gitea_ssh_port` | `2222` | SSH port displayed in clone URL. |

defaults/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
---
2+
# Choose between https://forgejo.org/ and https://gitea.io/
3+
gitea_fork: 'gitea' # 'gitea' and 'forgejo' are valid options
4+
25
# gitea version
36
# Use 'latest' to auto-update; upgrading past role version may lead to errors.
47
gitea_version: 'latest'
58
gitea_version_check: true
69
gitea_gpg_key: '7C9E68152594688862D62AF62D9AE806EC1592E2'
10+
gitea_forgejo_gpg_key: 'EB114F5E6C0DC2BCDD183550A4B61A2DC5923710'
711
gitea_gpg_server: 'hkps://keys.openpgp.org'
812
gitea_backup_on_upgrade: false
913
gitea_backup_location: "{{ gitea_home }}/backups/"
@@ -15,6 +19,7 @@ gitea_group: 'gitea'
1519
gitea_home: '/var/lib/gitea'
1620
gitea_user_home: '{{ gitea_home }}'
1721
gitea_executable_path: '/usr/local/bin/gitea'
22+
gitea_forgejo_executable_path: '/usr/local/bin/forgejo'
1823
gitea_configuraion_path: '/etc/gitea'
1924
gitea_shell: '/bin/false'
2025
gitea_systemd_cap_net_bind_service: false

meta/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
galaxy_info:
33
role_name: gitea
44
author: do1jlr
5-
description: Ansible role to configure and deploy gitea, a painless self-hosted Git service.
5+
description: Ansible role to configure and deploy gitea and forgejo, a painless self-hosted Git service.
66
license: "BSD-3-Clause"
77
min_ansible_version: "2.11"
88
platforms:
@@ -20,6 +20,7 @@ galaxy_info:
2020
- all
2121
galaxy_tags:
2222
- gitea
23+
- forgejo
2324
- git
2425
- system
2526
- development

tasks/backup.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
- name: Backing up gitea before upgrade
3030
become: true
3131
ansible.builtin.command:
32-
cmd: "sudo -u {{ gitea_user }} {{ gitea_executable_path }} dump -c {{ gitea_configuraion_path }}/gitea.ini"
32+
cmd: "sudo -u {{ gitea_user }} {{ gitea_full_executable_path }} dump -c {{ gitea_configuraion_path }}/gitea.ini"
3333
chdir: "{{ gitea_backup_location }}"
3434
changed_when: true
3535
rescue:

tasks/gitea_secrets.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- name: Generate gitea SECRET_KEY if not provided
33
become: true
4-
ansible.builtin.shell: 'umask 077; {{ gitea_executable_path }} generate secret SECRET_KEY > {{ gitea_configuraion_path }}/gitea_secret_key'
4+
ansible.builtin.shell: 'umask 077; {{ gitea_full_executable_path }} generate secret SECRET_KEY > {{ gitea_configuraion_path }}/gitea_secret_key'
55
args:
66
creates: '{{ gitea_configuraion_path }}/gitea_secret_key'
77
when: gitea_secret_key | string | length == 0
@@ -20,7 +20,7 @@
2020

2121
- name: Generate gitea INTERNAL_TOKEN if not provided
2222
become: true
23-
ansible.builtin.shell: 'umask 077; {{ gitea_executable_path }} generate secret INTERNAL_TOKEN > {{ gitea_configuraion_path }}/gitea_internal_token'
23+
ansible.builtin.shell: 'umask 077; {{ gitea_full_executable_path }} generate secret INTERNAL_TOKEN > {{ gitea_configuraion_path }}/gitea_internal_token'
2424
args:
2525
creates: '{{ gitea_configuraion_path }}/gitea_internal_token'
2626
when: gitea_internal_token | string | length == 0

tasks/install_forgejo.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
- name: Dependency block
3+
block:
4+
- name: Update apt cache
5+
become: true
6+
ansible.builtin.apt:
7+
cache_valid_time: 3600
8+
update_cache: true
9+
register: _pre_update_apt_cache
10+
until: _pre_update_apt_cache is succeeded
11+
when:
12+
- ansible_pkg_mgr == "apt"
13+
14+
- name: Install dependencies
15+
become: true
16+
ansible.builtin.package:
17+
name: "{{ gitea_dependencies }}"
18+
state: present
19+
register: _install_dep_packages
20+
until: _install_dep_packages is succeeded
21+
retries: 5
22+
delay: 2
23+
24+
- name: Install forgejo block
25+
when: (not gitea_version_check | bool) or (not ansible_check_mode and (gitea_active_version.stdout != gitea_version_target))
26+
block:
27+
- name: Download forgejo archive
28+
ansible.builtin.get_url:
29+
url: "{{ gitea_forgejo_dl_url | first }}"
30+
dest: "/tmp/{{ gitea_filename }}"
31+
checksum: "sha256:{{ gitea_forgejo_checksum }}"
32+
mode: 0640
33+
register: _download_archive
34+
until: _download_archive is succeeded
35+
retries: 5
36+
delay: 2
37+
38+
- name: Download forgejo asc file
39+
ansible.builtin.get_url:
40+
url: "{{ gitea_forgejo_signed_url | first }}"
41+
dest: "/tmp/{{ gitea_filename }}.asc"
42+
mode: 0640
43+
register: _download_asc
44+
until: _download_asc is succeeded
45+
retries: 5
46+
delay: 2
47+
48+
- name: Check forgejo gpg key
49+
ansible.builtin.command: "gpg --list-keys 0x{{ gitea_forgejo_gpg_key }}"
50+
register: _gitea_gpg_key_status
51+
changed_when: false
52+
failed_when: _gitea_gpg_key_status.rc not in (0, 2)
53+
54+
- name: Print gpg key staus on verbosity
55+
ansible.builtin.debug:
56+
msg: "{{ _gitea_gpg_key_status.stdout }}"
57+
verbosity: 1
58+
59+
- name: Import forgejo gpg key
60+
ansible.builtin.command: "gpg --keyserver {{ gitea_gpg_server }} --recv {{ gitea_forgejo_gpg_key }}"
61+
register: _gitea_import_key
62+
changed_when: '"imported: 1" in _gitea_import_key.stderr'
63+
when: '_gitea_gpg_key_status.rc != 0 or "expired" in _gitea_gpg_key_status.stdout'
64+
65+
- name: Check archive signature
66+
ansible.builtin.command: "gpg --verify /tmp/{{ gitea_filename }}.asc /tmp/{{ gitea_filename }}"
67+
changed_when: false
68+
69+
- name: Propagate gitea binary
70+
become: true
71+
ansible.builtin.copy:
72+
src: "/tmp/{{ gitea_filename }}"
73+
remote_src: true
74+
dest: "{{ gitea_full_executable_path }}"
75+
mode: 0755
76+
owner: root
77+
group: root
78+
notify: "Restart gitea"

tasks/install.yml renamed to tasks/install_gitea.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
retries: 5
2222
delay: 2
2323

24-
- name: Install block
24+
- name: Install gitea block
2525
when: (not gitea_version_check | bool) or (not ansible_check_mode and (gitea_active_version.stdout != gitea_version_target))
2626
block:
2727
- name: Download gitea archive
@@ -51,7 +51,7 @@
5151
changed_when: false
5252
failed_when: _gitea_gpg_key_status.rc not in (0, 2)
5353

54-
- name: print gpg key staus on verbosity
54+
- name: Print gpg key staus on verbosity
5555
ansible.builtin.debug:
5656
msg: "{{ _gitea_gpg_key_status.stdout }}"
5757
verbosity: 1
@@ -76,7 +76,7 @@
7676
ansible.builtin.copy:
7777
src: "/tmp/{{ gitea_filename }}"
7878
remote_src: true
79-
dest: "{{ gitea_executable_path }}"
79+
dest: "{{ gitea_full_executable_path }}"
8080
mode: 0755
8181
owner: root
8282
group: root

tasks/jwt_secrets.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- name: Generate OAuth2 JWT_SECRET if not provided
33
become: true
4-
ansible.builtin.shell: 'umask 077; {{ gitea_executable_path }} generate secret JWT_SECRET > {{ gitea_configuraion_path }}/gitea_oauth_jwt_secret'
4+
ansible.builtin.shell: 'umask 077; {{ gitea_full_executable_path }} generate secret JWT_SECRET > {{ gitea_configuraion_path }}/gitea_oauth_jwt_secret'
55
args:
66
creates: '{{ gitea_configuraion_path }}/gitea_oauth_jwt_secret'
77
when: gitea_oauth2_jwt_secret | length == 0
@@ -20,7 +20,7 @@
2020

2121
- name: Generate LFS JWT_SECRET if not provided
2222
become: true
23-
ansible.builtin.shell: 'umask 077; {{ gitea_executable_path }} generate secret JWT_SECRET > {{ gitea_configuraion_path }}/gitea_lfs_jwt_secret'
23+
ansible.builtin.shell: 'umask 077; {{ gitea_full_executable_path }} generate secret JWT_SECRET > {{ gitea_configuraion_path }}/gitea_lfs_jwt_secret'
2424
args:
2525
creates: '{{ gitea_configuraion_path }}/gitea_lfs_jwt_secret'
2626
when: gitea_lfs_jwt_secret | length == 0

tasks/main.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
ansible.builtin.package_facts:
88
manager: auto
99

10+
- name: Prepare gitea/forgejo variable import
11+
block:
12+
- name: Gather variables for gitea or forgejo
13+
ansible.builtin.include_vars: "{{ lookup('first_found', gitea_fork_variables) }}"
14+
rescue:
15+
- name: Gitea/Forejo import info
16+
ansible.builtin.fail:
17+
msg: "Currently only {{ gitea_supported_forks }} are supported."
18+
1019
- name: Gather variables for each operating system
1120
ansible.builtin.include_vars: "{{ lookup('first_found', gitea_variables) }}"
1221

1322
- name: Gather versioning information
14-
ansible.builtin.include_tasks: set_version.yml
23+
ansible.builtin.include_tasks: "set_{{ gitea_fork | lower }}_version.yml"
1524

1625
- name: Backup gitea before update
1726
ansible.builtin.include_tasks: backup.yml
@@ -20,8 +29,8 @@
2029
- name: Create gitea user and role
2130
ansible.builtin.include_tasks: create_user.yml
2231

23-
- name: Install or update gitea
24-
ansible.builtin.include_tasks: install.yml
32+
- name: "Install or update {{ gitea_fork }}"
33+
ansible.builtin.include_tasks: "install_{{ gitea_fork | lower }}.yml"
2534

2635
- name: Create directories
2736
ansible.builtin.include_tasks: directory.yml

tasks/set_forgejo_version.yml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
- name: "Check forgejo installed version"
3+
ansible.builtin.shell: "set -eo pipefail; {{ gitea_full_executable_path }} -v | cut -d' ' -f 3"
4+
args:
5+
executable: /bin/bash
6+
register: gitea_active_version
7+
changed_when: false
8+
failed_when: false
9+
10+
- name: "Determine 'latest' version release"
11+
when: gitea_version == "latest"
12+
block:
13+
- name: "Get latest forgejo release metadata"
14+
ansible.builtin.uri:
15+
url: 'https://codeberg.org/api/v1/repos/forgejo/forgejo/releases?limit=1'
16+
return_content: true
17+
register: gitea_forgejo_remote_metadata
18+
when: not ansible_check_mode
19+
20+
- name: "Fail if running in check mode without versions set."
21+
ansible.builtin.fail:
22+
msg: |
23+
"You are running this playbook in check mode:
24+
Please set the Gitea version with the variable 'gitea_version', because the URI module cannot detect the latest version in this mode."
25+
when: ansible_check_mode and (gitea_version == 'latest' or gitea_version == 'present')
26+
27+
- name: "Set fact latest forgejo release"
28+
ansible.builtin.set_fact:
29+
gitea_remote_version: "{{ gitea_forgejo_remote_metadata.json.0.tag_name[1:] }}"
30+
when: not ansible_check_mode
31+
32+
- name: "Set forgejo version target (latest)"
33+
ansible.builtin.set_fact:
34+
gitea_version_target: "{{ gitea_remote_version }}"
35+
when: not ansible_check_mode
36+
37+
- name: "Set gitea version target {{ gitea_version }}"
38+
ansible.builtin.set_fact:
39+
gitea_version_target: "{{ gitea_version }}"
40+
when: gitea_version != "latest"
41+
42+
- name: "Get specific forgejo release metadata"
43+
ansible.builtin.uri:
44+
url: 'https://codeberg.org/api/v1/repos/forgejo/forgejo/releases/tags/v{{ gitea_version_target }}'
45+
return_content: true
46+
register: gitea_forgejo_remote_tags_metadata
47+
when: not ansible_check_mode
48+
49+
- name: "Generate forgejo download url"
50+
ansible.builtin.set_fact:
51+
gitea_forgejo_dl_url: "{{ gitea_forgejo_remote_tags_metadata.json | community.general.json_query(gitea_forgejo_query_download) }}"
52+
when: not ansible_check_mode
53+
54+
- name: "Generate forgejo download checksum url"
55+
ansible.builtin.set_fact:
56+
gitea_forgejo_checksum_url: "{{ gitea_forgejo_remote_tags_metadata.json | community.general.json_query(gitea_forgejo_query_checksum) }}"
57+
when: not ansible_check_mode
58+
59+
- name: Get forgejo checksum
60+
ansible.builtin.uri:
61+
url: "{{ gitea_forgejo_checksum_url | first }}"
62+
return_content: true
63+
register: _gitea_forgejo_dl_checksum
64+
when: not ansible_check_mode
65+
66+
- name: Set forjeo checksum
67+
ansible.builtin.set_fact:
68+
gitea_forgejo_checksum: "{{ _gitea_forgejo_dl_checksum.content.split(' ')[0] }}"
69+
when: not ansible_check_mode
70+
71+
- name: "Generate forgejo download signed url"
72+
ansible.builtin.set_fact:
73+
gitea_forgejo_signed_url: "{{ gitea_forgejo_remote_tags_metadata.json | community.general.json_query(gitea_forgejo_query_signed) }}"
74+
when: not ansible_check_mode
75+
76+
- name: "Set a example forgejo download link if in check mode"
77+
ansible.builtin.set_fact:
78+
gitea_forgejo_dl_url: ['https://codeberg.org/attachments/a00333ad-250a-4d30-a764-9a37fb24f419']
79+
when: ansible_check_mode
80+
81+
- name: "Set a example forgejo checksum link if in check mode"
82+
ansible.builtin.set_fact:
83+
gitea_forgejo_checksum: 'f8c71464d1b250bf022eaa3df270c810950904ceb71da5cefc7ec24a034a4c87'
84+
when: ansible_check_mode
85+
86+
- name: "Set a example forgejo checksum link if in check mode"
87+
ansible.builtin.set_fact:
88+
gitea_forgejo_signed_url: ['https://codeberg.org/attachments/ae5e50c6-e86e-4202-b95f-f142e8138e2f']
89+
when: ansible_check_mode
90+
91+
- name: Show Download URLs
92+
ansible.builtin.debug:
93+
msg: "{{ item }}"
94+
verbosity: 1
95+
with_items:
96+
- "gitea_forgejo_dl_url: {{ gitea_forgejo_dl_url | first }}"
97+
- "gitea_forgejo_checksum: {{ gitea_forgejo_checksum }}"
98+
- "gitea_forgejo_signed_url: {{ gitea_forgejo_signed_url | first }}"

0 commit comments

Comments
 (0)