File forgejo.spec of Package forgejo

%define realname forgejo
%define srcext   tar.gz

%define forgejo_group git
%define forgejo_user  git
%define forgejo_home  %{_localstatedir}/lib/forgejo

%if %{defined suse_version}
%if ! %{defined _fillupdir}
%define _fillupdir /var/adm/fillup-templates
%endif
%define sysconffile %{_fillupdir}/sysconfig.forgejo
%else
%define sysconffile %{_sysconfdir}/sysconfig/forgejo
%endif

# Common info
Name:          %{realname}
Version:       13.0.1
Release:       wiz%{?extraver:0.}1%{?dist}
License:       GPL-3.0+
Group:         Development/Tools/Version Control
URL:           https://forgejo.org/
Summary:       Self-hosted lightweight software forge

# Install-time parameters
Requires:      git git-lfs
Requires(post): sudo

# Build-time parameters
BuildRequires: go >= 1.24.4
BuildRequires: nodejs(engine) >= 20.0.0
BuildRequires: npm
%if 0%{?suse_version}
BuildRequires: fdupes
%endif
Source0:       https://codeberg.org/forgejo/forgejo/archive/v%{version}.tar.gz#/forgejo-%{version}.tar.gz
# cd forgejo; go mod vendor -v && tar -Jcvf ../vendor.tar.xz vendor && rm -rf vendor/; cd ..
Source1:       vendor.tar.xz
# cd forgejo; npm install --no-save && tar -Jcf ../node_modules.tar.xz node_modules package-lock.json; rm -rf node_modules; cd ..
Source11:      node_modules.tar.xz

%description
Forgejo is a self-hosted lightweight software forge.
Easy to install and low maintenance, it just does the job.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname} -a1 -a11
# Fix application version string
%{__sed} -i '/^\s*Version\s*=/ s/".*"/"%{version}"/' main.go

# Build step (compile/build binaries, documentation, etc)
%build
_CFLAGS='%{optflags}'
_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro'
export GOPATH=$(pwd -P)/.gomodules
export GOCACHE=$(pwd -P)/.gocache
export GOTRACEBACK=crash
export CGO_ENABLED=1
export CGO_CFLAGS="$_CFLAGS -DSQLITE_MAX_VARIABLE_NUMBER=32766"
export CGO_LDFLAGS="$_LDFLAGS"
### %{?_smp_mflags}
%{__make} build \
  FORGEJO_VERSION=%{version} \
  EXTRA_GOFLAGS="-buildmode=pie -gcflags=all=-dwarf=false" \
  TAGS=sqlite,sqlite_unlock_notify,bindata \
  LDFLAGS="-linkmode=external -extldflags \"$_LDFLAGS\""

# Install built stuff
%install
# Install executable
%{__install} -d -m0755 %{buildroot}%{_bindir}
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/forgejo
%{__install} -d -m0750 %{buildroot}%{forgejo_home}
%{__install} -d -m0750 %{buildroot}%{forgejo_home}/data
%{__install} -d -m0750 %{buildroot}%{forgejo_home}/repositories
%{__install} -d -m0750 %{buildroot}%{forgejo_home}/custom
%{__install} -d -m0750 %{buildroot}%{forgejo_home}/.ssh
%{__install} -d -m0755 %{buildroot}/var/log/forgejo
%{__ln_s} ../../log/forgejo %{buildroot}%{forgejo_home}/log
%{__install} -m0755 gitea %{buildroot}%{_bindir}/forgejo
# Config
%{__sed} -r \
  -e 's|^;(\[.+\])$|\1|' \
  -e 's|^;?(RUN_USER\s*=).*$|\1 %{forgejo_user}|' \
  -e 's|^;?(RUN_MODE\s*=).*$|\1 prod|' \
  -e 's|^;?(WORK_PATH\s*=).*$|\1 %{forgejo_home}|' \
  -e 's|^(SECRET_KEY\s*=.*)$|;\1|' \
  -e 's|^;?(SECRET_KEY_URI\s*=).*$|\1 file:%{_sysconfdir}/forgejo/secret_key|' \
  -e 's|^(INTERNAL_TOKEN\s*=.*)$|;\1|' \
  -e 's|^;?(INTERNAL_TOKEN_URI\s*=).*$|\1 file:%{_sysconfdir}/forgejo/internal_token|' \
  -e 's|^(LFS_JWT_SECRET\s*=.*)$|;\1|' \
  -e 's|^;?(LFS_JWT_SECRET_URI\s*=).*$|\1 file:%{_sysconfdir}/forgejo/lfs_jwt_secret|' \
  -e 's|^;?(ROOT_PATH\s*=).*$|\1 /var/log/forgejo|' \
  -e 's|^;?(ROOT\s*=).*$|\1 repositories|' \
  custom/conf/app.example.ini \
  > %{buildroot}%{_sysconfdir}/forgejo/app.ini
# sysystemd unit/init script
%{__install} -d -m0755 %{buildroot}%{_unitdir}
%{__sed} -r \
  -e '/^ExecStart/ s|/usr/local/bin|%{_bindir}|' \
  -e '/^Environment/ s|HOME=\S+|HOME=%{forgejo_home}|' \
  contrib/systemd/forgejo.service \
  > %{buildroot}%{_unitdir}/forgejo.service
# File duplicates
%if 0%{?suse_version}
%fdupes %{buildroot}%{forgejo_home}
%endif

# Files list
%files
%defattr(-,root,root)
%license LICENSE
%doc CONTRIBUTING.md DCO README.md RELEASE-NOTES.md
%doc custom/conf/app.example.ini contrib/legal/*
%{_bindir}/forgejo
%dir %{_sysconfdir}/forgejo
%config(noreplace) %attr(0600,%{forgejo_user},%{forgejo_group}) %{_sysconfdir}/forgejo/app.ini
%ghost %{_sysconfdir}/forgejo/secret_key
%ghost %{_sysconfdir}/forgejo/internal_token
%ghost %{_sysconfdir}/forgejo/lfs_jwt_secret
%dir %attr(0750,%{forgejo_user},%{forgejo_group}) %{forgejo_home}
%attr(-,%{forgejo_user},%{forgejo_group}) %{forgejo_home}/*
%dir %attr(0755,%{forgejo_user},%{forgejo_group}) /var/log/forgejo
%{_unitdir}/forgejo.service

%pre
/usr/sbin/groupadd -r %{forgejo_group} &> /dev/null ||:
/usr/sbin/useradd  -g %{forgejo_group} -s /bin/bash -r -M \
  -c "Git repos owner" -d %{forgejo_home} %{forgejo_user} &> /dev/null ||:
%if "%{expand:%_vendor}" == "suse"
%{service_add_pre forgejo.service}
%endif

%post
for SECRET in secret_key internal_token lfs_jwt_secret; do
    if [ ! -f %{_sysconfdir}/forgejo/$SECRET ]; then
        forgejo generate secret $(tr '[:lower:]' '[:upper:]' <<< $SECRET) > %{_sysconfdir}/forgejo/$SECRET
        chown %{forgejo_user}:%{forgejo_group} %{_sysconfdir}/forgejo/$SECRET
        chmod 0600 %{_sysconfdir}/forgejo/$SECRET
    fi
done
sudo -u %{forgejo_user} forgejo -c /etc/forgejo/app.ini admin regenerate keys
%if "%{expand:%_vendor}" == "suse"
%{fillup_only}
%{service_add_post forgejo.service}
%endif
%if "%{expand:%_vendor}" == "redhat"
%{systemd_post forgejo.service}
%endif

%preun
%if "%{expand:%_vendor}" == "suse"
%{service_del_preun forgejo.service}
%endif
%if "%{expand:%_vendor}" == "redhat"
%{systemd_preun forgejo.service}
%endif

%postun
%if "%{expand:%_vendor}" == "suse"
%{service_del_postun forgejo.service}
%endif
%if "%{expand:%_vendor}" == "redhat"
%{systemd_postun forgejo.service}
%endif

%changelog
openSUSE Build Service is sponsored by