From bf0ece92ab5fef1cf1c8f9481626305f7b208c9f Mon Sep 17 00:00:00 2001 From: Gamerboy59 Date: Sun, 8 Sep 2024 18:28:03 +0200 Subject: [PATCH] Add Jenkinsfile for build automation --- .build/.gitignore | 0 .build/DEB/control | 10 +++ .build/DEB/postinst | 9 ++ .build/DEB/prerm | 8 ++ .build/Jenkinsfile | 208 +++++++++++++++++++++++++++++++++++++++++++ .build/RPM/rpxy.spec | 46 ++++++++++ .build/rpxy-start.sh | 10 +++ .build/rpxy.service | 11 +++ 8 files changed, 302 insertions(+) create mode 100644 .build/.gitignore create mode 100644 .build/DEB/control create mode 100644 .build/DEB/postinst create mode 100644 .build/DEB/prerm create mode 100644 .build/Jenkinsfile create mode 100644 .build/RPM/rpxy.spec create mode 100644 .build/rpxy-start.sh create mode 100644 .build/rpxy.service diff --git a/.build/.gitignore b/.build/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.build/DEB/control b/.build/DEB/control new file mode 100644 index 0000000..759f84c --- /dev/null +++ b/.build/DEB/control @@ -0,0 +1,10 @@ +Package: rpxy +Version: @BUILD_VERSION@-1 +Maintainer: Jun Kurihara +Homepage: https://github.com/junkurihara/rust-rpxy +Architecture: amd64 +Depends: systemd +Recommends: rpxy-webui +Priority: optional +Section: base +Description: A simple and ultrafast reverse-proxy serving multiple domain names with TLS termination, written in Rust diff --git a/.build/DEB/postinst b/.build/DEB/postinst new file mode 100644 index 0000000..cf3206d --- /dev/null +++ b/.build/DEB/postinst @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e + +systemctl daemon-reload +systemctl enable rpxy +systemctl start rpxy + +exit 0 \ No newline at end of file diff --git a/.build/DEB/prerm b/.build/DEB/prerm new file mode 100644 index 0000000..889ae8c --- /dev/null +++ b/.build/DEB/prerm @@ -0,0 +1,8 @@ +#!/bin/bash + +set -e + +systemctl stop rpxy || true +systemctl disable rpxy || true + +exit 0 \ No newline at end of file diff --git a/.build/Jenkinsfile b/.build/Jenkinsfile new file mode 100644 index 0000000..f9aade4 --- /dev/null +++ b/.build/Jenkinsfile @@ -0,0 +1,208 @@ +pipeline { + agent none + parameters { + string(name: 'BUILD_VERSION', defaultValue: '', description: 'Build version for the distribution packages') + } + stages { + stage('Build Binary and Prepare Files') { + agent { + kubernetes { + inheritFrom 'default' + yaml """ + apiVersion: v1 + kind: Pod + spec: + containers: + - name: rust-cargo + image: rust:slim + command: + - cat + tty: true + """ + } + } + steps { + container('rust-cargo') { + script { + // Install git + sh 'apt-get update && apt-get -y install git --no-install-recommends' + // Clone git repo + sh 'git clone https://github.com/junkurihara/rust-rpxy.git' + dir('rust-rpxy') { + sh """ + sed -i 's|git@github.com:junkurihara/rusty-http-cache-semantics.git|https://github.com/junkurihara/rusty-http-cache-semantics.git|g' .gitmodules + sed -i 's|git@github.com:junkurihara/rustls-acme.git|https://github.com/junkurihara/rustls-acme.git|g' .gitmodules + """ + // Load required submodules + sh 'git submodule update --init' + // Compile rustc binary + sh 'cargo build --release' + + // Extract the version from Cargo.toml if not passed as parameter + if (params.BUILD_VERSION == "") { + def versionMatch = sh(script: 'grep "^version" Cargo.toml | sed \'s/version = "\\([0-9.]*\\)"/\\1/\'', returnStdout: true).trim() + if (versionMatch) { + env.BUILD_VERSION = versionMatch + echo "Using extracted version: ${env.BUILD_VERSION}" + } else { + error "Version not found in Cargo.toml" + } + } else { + env.BUILD_VERSION = params.BUILD_VERSION + echo "Using provided build version: ${env.BUILD_VERSION}" + } + } + // Stash the binary for later use in package building stages + sh 'mv rust-rpxy/target/release/rpxy .' + stash includes: "rpxy", name: "rpxy" + + // Stash the service, control, and spec files for later use + sh ''' + mv rust-rpxy/.build/DEB/control . + mv rust-rpxy/.build/DEB/postinst . + mv rust-rpxy/.build/DEB/prerm . + mv rust-rpxy/.build/RPM/rpxy.spec . + mv rust-rpxy/.build/rpxy-start.sh . + mv rust-rpxy/.build/rpxy.service . + ''' + stash includes: "control, postinst, prerm, rpxy-start.sh", name: "deb-control" + stash includes: "rpxy.spec", name: "rpm-spec" + stash includes: "rpxy.service", name: "service-file" + + // Stash LICENSE and README.md + sh 'mv rust-rpxy/LICENSE .' + sh 'mv rust-rpxy/README.md .' + stash includes: "LICENSE, README.md", name: "docs" + + // Archive the binary and create fingerprint + archiveArtifacts artifacts: "rpxy", allowEmptyArchive: false, fingerprint: true + } + } + } + } + stage('Build RPM Package') { + agent { + kubernetes { + inheritFrom 'default' + yaml """ + apiVersion: v1 + kind: Pod + spec: + containers: + - name: rpm-build + image: rockylinux:9 + command: + - cat + tty: true + """ + } + } + steps { + container('rpm-build') { + script { + // Unstash the necessary files for this stage + unstash 'rpxy' + unstash 'rpm-spec' + unstash 'service-file' + unstash 'docs' + + // Install required tools + sh 'dnf update -y && dnf install -y rpmdevtools tar' + + // Create a tar.gz archive containing all necessary files + sh """ + mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} + mkdir -p rpxy-${env.BUILD_VERSION} + mv rpxy rpxy.service LICENSE README.md rpxy-${env.BUILD_VERSION}/ + tar -czf rpmbuild/SOURCES/rpxy-${env.BUILD_VERSION}.tar.gz rpxy-${env.BUILD_VERSION}/ + rm -rf rpxy-${env.BUILD_VERSION} + """ + + // Move the RPM spec file + sh 'mv rpxy.spec rpmbuild/SPECS/' + + // Replace @BUILD_VERSION@ in control file with actual version + sh "sed -i 's/@BUILD_VERSION@/${env.BUILD_VERSION}/' rpmbuild/SPECS/rpxy.spec" + + // Replace @Source0@ in control file with actual version + sh "sed -i 's/@Source0@/rpxy-${env.BUILD_VERSION}.tar.gz/' rpmbuild/SPECS/rpxy.spec" + + // Build the RPM package + sh "rpmbuild --define '_topdir ${WORKSPACE}/rpmbuild' --define '_version ${env.BUILD_VERSION}' -bb rpmbuild/SPECS/rpxy.spec" + + // Archive the RPM package and create fingerprint + archiveArtifacts artifacts: "rpmbuild/RPMS/x86_64/rpxy-${env.BUILD_VERSION}-1.el9.x86_64.rpm", allowEmptyArchive: false, fingerprint: true + } + } + } + } + stage('Build DEB Package') { + agent { + kubernetes { + inheritFrom 'default' + yaml """ + apiVersion: v1 + kind: Pod + spec: + containers: + - name: debian-build + image: debian:stable-slim + command: + - cat + tty: true + """ + } + } + steps { + container('debian-build') { + script { + // Unstash the necessary files for this stage + unstash 'rpxy' + unstash 'deb-control' + unstash 'service-file' + unstash 'docs' + + // Install required tools + sh 'apt-get update && apt-get install --no-install-recommends -y dpkg-dev' + + // Create folder structure + sh """ + mkdir -p rpxy_${env.BUILD_VERSION}-1_amd64/usr/bin + mkdir -p rpxy_${env.BUILD_VERSION}-1_amd64/usr/local/bin + mkdir -p rpxy_${env.BUILD_VERSION}-1_amd64/etc/systemd/system + mkdir -p rpxy_${env.BUILD_VERSION}-1_amd64/usr/share/doc/rpxy + mkdir -p rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN + """ + + // Move postinstall, pre-removal and start-wrapper scripts + sh """ + mv postinst rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN/ + chmod 755 rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN/postinst + mv prerm rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN/ + chmod 755 rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN/prerm + mv rpxy-start.sh rpxy_${env.BUILD_VERSION}-1_amd64/usr/local/bin + chmod 0755 rpxy_${env.BUILD_VERSION}-1_amd64/usr/local/bin/rpxy-start.sh + """ + + // Move binary, service file, control file, LICENSE, and README.md + sh """ + mv rpxy rpxy_${env.BUILD_VERSION}-1_amd64/usr/bin/ + mv rpxy.service rpxy_${env.BUILD_VERSION}-1_amd64/etc/systemd/system/ + mv LICENSE README.md rpxy_${env.BUILD_VERSION}-1_amd64/usr/share/doc/rpxy/ + mv control rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN/ + """ + + // Replace @BUILD_VERSION@ in control file with actual version + sh "sed -i 's/@BUILD_VERSION@/${env.BUILD_VERSION}/' rpxy_${env.BUILD_VERSION}-1_amd64/DEBIAN/control" + + // Build the DEB package + sh "dpkg-deb --build --root-owner-group rpxy_${env.BUILD_VERSION}-1_amd64" + + // Archive the DEB package and create fingerprint + archiveArtifacts artifacts: "rpxy_${env.BUILD_VERSION}-1_amd64.deb", allowEmptyArchive: false, fingerprint: true + } + } + } + } + } +} diff --git a/.build/RPM/rpxy.spec b/.build/RPM/rpxy.spec new file mode 100644 index 0000000..b282345 --- /dev/null +++ b/.build/RPM/rpxy.spec @@ -0,0 +1,46 @@ +Name: rpxy +Version: @BUILD_VERSION@ +Release: 1%{?dist} +Summary: A simple and ultrafast reverse-proxy serving multiple domain names with TLS termination, written in Rust + +License: MIT +URL: https://github.com/junkurihara/rust-rpxy +Source0: @Source0@ +BuildArch: x86_64 + +Requires: systemd + +%description +This rpm installs rpxy into /usr/bin and sets up a systemd service. + +%prep +%autosetup + +%install +rm -rf %{buildroot} +mkdir -p %{buildroot}%{_bindir} +cp rpxy %{buildroot}%{_bindir}/ +mkdir -p %{buildroot}%{_sysconfdir}/systemd/system +cp rpxy.service %{buildroot}%{_sysconfdir}/systemd/system/ +mkdir -p %{buildroot}%{_docdir}/rpxy +cp LICENSE %{buildroot}%{_docdir}/rpxy/ +cp README.md %{buildroot}%{_docdir}/rpxy/ + +%clean +rm -rf %{buildroot} + +%files +%license %{_docdir}/rpxy/LICENSE +%doc %{_docdir}/rpxy/README.md +%{_bindir}/rpxy +%{_sysconfdir}/systemd/system/rpxy.service + +%post +systemctl daemon-reload +systemctl enable rpxy + +%preun +systemctl stop rpxy + +%postun +systemctl disable rpxy \ No newline at end of file diff --git a/.build/rpxy-start.sh b/.build/rpxy-start.sh new file mode 100644 index 0000000..b3ddae2 --- /dev/null +++ b/.build/rpxy-start.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Check if rpxy-webui is installed +if dpkg-query -W -f='${Status}' rpxy-webui 2>/dev/null | grep -q "install ok installed"; then + echo "rpxy-webui is installed. Starting rpxy with rpxy-webui" + exec /usr/local/bin/rpxy --enable-webui +else + echo "rpxy-webui is not installed. Starting with default config" + exec /usr/local/bin/rpxy +fi diff --git a/.build/rpxy.service b/.build/rpxy.service new file mode 100644 index 0000000..d8c880a --- /dev/null +++ b/.build/rpxy.service @@ -0,0 +1,11 @@ +[Unit] +Description=rpxy Service +After=network.target + +[Service] +ExecStart=/usr/local/bin/rpxy-start.sh +Restart=always +User=nobody + +[Install] +WantedBy=multi-user.target