From 19642c987370dc9d70c57ad46921a1b4587cc7e5 Mon Sep 17 00:00:00 2001 From: Jan Fooken Date: Thu, 22 Aug 2024 14:47:17 +0200 Subject: [PATCH] Deploy RPM to OBS (#5) * Deploy RPM to OBS * Add empty build section * Run spec-cleaner * Number source --- .github/workflows/ci.yaml | 61 +++++++++++++++++ hack/get_version_from_git.sh | 17 +++++ hack/gh_release_to_obs_changeset.py | 79 +++++++++++++++++++++ packaging/suse/rpm/_service | 18 +++++ packaging/suse/rpm/trento-checks.spec | 99 ++++++++++++++------------- 5 files changed, 227 insertions(+), 47 deletions(-) create mode 100755 hack/get_version_from_git.sh create mode 100755 hack/gh_release_to_obs_changeset.py create mode 100644 packaging/suse/rpm/_service diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 533a824..ef8f77d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -4,6 +4,8 @@ on: push: branches: [main] pull_request: + release: + types: [published] jobs: tlint: @@ -20,3 +22,62 @@ jobs: fetch-depth: 0 - name: Run TLint run: "/home/tlint/tlint lint -f /data/checks" + + obs-commit-rpm: + name: Commit to OBS to generate a RPM package + needs: [tlint] + runs-on: ubuntu-20.04 + if: github.ref == 'refs/heads/main' || github.event_name == 'release' + container: + image: ghcr.io/trento-project/continuous-delivery:main + env: + OBS_USER: ${{ secrets.OBS_USER }} + OBS_PASS: ${{ secrets.OBS_PASS }} + OBS_PROJECT: ${{ secrets.OBS_PROJECT }} + OSC_CHECKOUT_DIR: /tmp/trento-checks-package + REPOSITORY: ${{ github.repository }} + FOLDER: packaging/suse/rpm + options: -u 0:0 + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.12.1 + with: + access_token: ${{ github.token }} + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Configure OSC + run: | + mkdir -p $HOME/.config/osc + cp /home/osc/.config/osc/oscrc $HOME/.config/osc + /scripts/init_osc_creds.sh + - name: Prepare _service file + run: | + git config --global --add safe.directory /__w/checks/checks + VERSION=$(./hack/get_version_from_git.sh) + sed -i 's~%%REVISION%%~${{ github.sha }}~' $FOLDER/_service && \ + sed -i 's~%%REPOSITORY%%~'"${REPOSITORY}"'~' $FOLDER/_service && \ + sed -i 's~%%VERSION%%~'"${VERSION}"'~' $FOLDER/_service + - name: Checkout and prepare OBS package + run: | + osc checkout $OBS_PROJECT trento-checks -o $OSC_CHECKOUT_DIR + cp $FOLDER/_service $OSC_CHECKOUT_DIR + cp $FOLDER/trento-checks.spec $OSC_CHECKOUT_DIR + rm -vf $OSC_CHECKOUT_DIR/*.tar.gz + pushd $OSC_CHECKOUT_DIR + osc service manualrun + rm -vf $OSC_CHECKOUT_DIR/*.tgz + - name: Prepare trento-checks.changes file + # The .changes file is updated only in release creation. This current task should be improved + # in order to add the current rolling release notes + if: github.event_name == 'release' + run: | + git config --global --add safe.directory /__w/checks/checks + VERSION=$(./hack/get_version_from_git.sh) + TAG=$(echo $VERSION | cut -f1 -d+) + hack/gh_release_to_obs_changeset.py $REPOSITORY -a shap-staff@suse.de -t $TAG -f $OSC_CHECKOUT_DIR/trento-checks.changes + - name: Commit changes into OBS + run: | + pushd $OSC_CHECKOUT_DIR + osc ar + osc commit -m "GitHub Actions automated update to reference ${{ github.sha }}" diff --git a/hack/get_version_from_git.sh b/hack/get_version_from_git.sh new file mode 100755 index 0000000..c811cd1 --- /dev/null +++ b/hack/get_version_from_git.sh @@ -0,0 +1,17 @@ +#!/bin/sh +set -e +set -o pipefail + +TAG=$( git tag | grep -E "[0-9]\.[0-9]\.[0-9]" | sort -rn | head -n1 ) + +if [ -n "${TAG}" ]; then + COMMITS_SINCE_TAG=$(git rev-list "${TAG}".. --count) + if [ "${COMMITS_SINCE_TAG}" -gt 0 ]; then + COMMIT_SHA=$(git show -s --format=%ct.%h HEAD) + SUFFIX="+git.dev${COMMITS_SINCE_TAG}.${COMMIT_SHA}" + fi +else + TAG="0" +fi + +echo "${TAG}${SUFFIX}" diff --git a/hack/gh_release_to_obs_changeset.py b/hack/gh_release_to_obs_changeset.py new file mode 100755 index 0000000..12305ff --- /dev/null +++ b/hack/gh_release_to_obs_changeset.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import argparse +import json +import os +import sys +import textwrap +import urllib.request +import urllib.error +from datetime import datetime +from datetime import timezone +import tempfile + +parser = argparse.ArgumentParser(description="Add a GitHub release to an RPM changelog", usage=argparse.SUPPRESS) +parser.add_argument("repo", help="GitHub repository (owner/name)") +parser.add_argument("-t", "--tag", help="A specific Git tag to get; if none, latest will be used") +parser.add_argument("-a", "--author", help="The author of the RPM changelog entry") +parser.add_argument("-f", "--file", help="Prepend the new changelog entry to file instead of printing in stdout") + +if len(sys.argv) == 1: + parser.print_help(sys.stderr) + sys.exit(1) + +args = parser.parse_args() + +releaseSegment = f"/tags/{args.tag}" if args.tag else "/latest" +url = f'https://api.github.com/repos/{args.repo}/releases{releaseSegment}' + +request = urllib.request.Request(url) + +githubToken = os.getenv("GITHUB_OAUTH_TOKEN") +if githubToken: + request.add_header("Authorization", "token " + githubToken) + +try: + response = urllib.request.urlopen(request) +except urllib.error.HTTPError as error: + if error.code == 404: + print(f"Release {args.tag} not found in {args.repo}. Skipping changelog generation.") + sys.exit(0) + print(f"GitHub API responded with a {error.code} error!", file=sys.stderr) + print("Url:", url, file=sys.stderr) + print("Response:", json.dumps(json.load(error), indent=4), file=sys.stderr, sep="\n") + sys.exit(1) + +release = json.load(response) + +releaseDate = datetime.strptime(release['published_at'], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) + +with tempfile.TemporaryFile("r+") as temp: + print("-------------------------------------------------------------------", file=temp) + + print(f"{releaseDate.strftime('%a %b %d %H:%M:%S %Z %Y')}", end="", file=temp) + if args.author: + print(f" - {args.author}", end="", file=temp) + print("\n", file=temp) + + print(f"- Release {args.tag}", end="", file=temp) + if release['name'] and release['name'] != args.tag: + print(f" - {release['name']}", end="", file=temp) + print("\n", file=temp) + + if release['body']: + print(textwrap.indent(release['body'], " "), file=temp, end="\n\n") + temp.seek(0) + + if args.file: + try: + with open(args.file, "r") as prev: + old = prev.read() + except FileNotFoundError: + old = "" + with open(args.file, "w") as new: + for line in temp: + new.write(line) + new.write(old) + sys.exit(0) + + print(temp.read()) diff --git a/packaging/suse/rpm/_service b/packaging/suse/rpm/_service new file mode 100644 index 0000000..e88be49 --- /dev/null +++ b/packaging/suse/rpm/_service @@ -0,0 +1,18 @@ + + + https://github.com/%%REPOSITORY%%.git + git + %%REVISION%% + .git + .github + %%VERSION%% + trento-checks + + + trento-checks.spec + + + *.tar + gz + + diff --git a/packaging/suse/rpm/trento-checks.spec b/packaging/suse/rpm/trento-checks.spec index 3d872c1..3d61ff9 100644 --- a/packaging/suse/rpm/trento-checks.spec +++ b/packaging/suse/rpm/trento-checks.spec @@ -1,7 +1,7 @@ # # spec file for package trento-checks # -# Copyright (c) 2024 SUSE LCC +# Copyright (c) 2024 SUSE LLC # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -11,72 +11,77 @@ # case the license is the MIT License). An "Open Source License" is a # license that conforms to the Open Source Definition (Version 1.9) # published by the Open Source Initiative. -# + # Please submit bugfixes or comments via https://bugs.opensuse.org/ # + +%define trento_dir %{_datadir}/trento +%define trento_checks_dir %{trento_dir}/checks Name: trento-checks +# Set by _service via OBS and GitHub action "CI" Version: 0 Release: 0 Summary: Checks for the Trento checks engine License: Apache-2.0 -URL: https://github.com/trento-project/checks -Source: https://github.com/trento-project/checks/archive/refs/heads/main.tar.gz Group: System/Monitoring +URL: https://github.com/trento-project/checks +# Added by _service via OBS +Source0: %{name}-%{version}.tar.gz BuildArch: noarch -%define trento_dir %{_datadir}/trento -%define trento_checks_dir %trento_dir/checks - %description %{summary} %prep -%autosetup -p1 -n checks-main +%autosetup -p1 + +%build +# empty on purpose %install -install -d -m 0755 %buildroot%trento_dir -install -d -m 0755 %buildroot%trento_checks_dir -install -p -m 0644 checks/* %buildroot%trento_checks_dir +install -d -m 0755 %{buildroot}%{trento_dir} +install -d -m 0755 %{buildroot}%{trento_checks_dir} +install -p -m 0644 checks/* %{buildroot}%{trento_checks_dir} %files %license LICENSE -%dir %trento_dir -%dir %trento_checks_dir +%dir %{trento_dir} +%dir %{trento_checks_dir} -%trento_checks_dir/00081D.yaml -%trento_checks_dir/0B6DB2.yaml -%trento_checks_dir/156F64.yaml -%trento_checks_dir/15F7A8.yaml -%trento_checks_dir/205AF7.yaml -%trento_checks_dir/21FCA6.yaml -%trento_checks_dir/222A57.yaml -%trento_checks_dir/24ABCB.yaml -%trento_checks_dir/32CFC6.yaml -%trento_checks_dir/33403D.yaml -%trento_checks_dir/373DB8.yaml -%trento_checks_dir/49591F.yaml -%trento_checks_dir/53D035.yaml -%trento_checks_dir/61451E.yaml -%trento_checks_dir/68626E.yaml -%trento_checks_dir/6E9B82.yaml -%trento_checks_dir/790926.yaml -%trento_checks_dir/7E0221.yaml -%trento_checks_dir/816815.yaml -%trento_checks_dir/822E47.yaml -%trento_checks_dir/845CC9.yaml -%trento_checks_dir/9FAAD0.yaml -%trento_checks_dir/9FEFB0.yaml -%trento_checks_dir/A1244C.yaml -%trento_checks_dir/B089BE.yaml -%trento_checks_dir/C3166E.yaml -%trento_checks_dir/C620DC.yaml -%trento_checks_dir/CAEFF1.yaml -%trento_checks_dir/D028B9.yaml -%trento_checks_dir/D78671.yaml -%trento_checks_dir/DA114A.yaml -%trento_checks_dir/DC5429.yaml -%trento_checks_dir/F50AF5.yaml -%trento_checks_dir/FB0E0D.yaml +%{trento_checks_dir}/00081D.yaml +%{trento_checks_dir}/0B6DB2.yaml +%{trento_checks_dir}/156F64.yaml +%{trento_checks_dir}/15F7A8.yaml +%{trento_checks_dir}/205AF7.yaml +%{trento_checks_dir}/21FCA6.yaml +%{trento_checks_dir}/222A57.yaml +%{trento_checks_dir}/24ABCB.yaml +%{trento_checks_dir}/32CFC6.yaml +%{trento_checks_dir}/33403D.yaml +%{trento_checks_dir}/373DB8.yaml +%{trento_checks_dir}/49591F.yaml +%{trento_checks_dir}/53D035.yaml +%{trento_checks_dir}/61451E.yaml +%{trento_checks_dir}/68626E.yaml +%{trento_checks_dir}/6E9B82.yaml +%{trento_checks_dir}/790926.yaml +%{trento_checks_dir}/7E0221.yaml +%{trento_checks_dir}/816815.yaml +%{trento_checks_dir}/822E47.yaml +%{trento_checks_dir}/845CC9.yaml +%{trento_checks_dir}/9FAAD0.yaml +%{trento_checks_dir}/9FEFB0.yaml +%{trento_checks_dir}/A1244C.yaml +%{trento_checks_dir}/B089BE.yaml +%{trento_checks_dir}/C3166E.yaml +%{trento_checks_dir}/C620DC.yaml +%{trento_checks_dir}/CAEFF1.yaml +%{trento_checks_dir}/D028B9.yaml +%{trento_checks_dir}/D78671.yaml +%{trento_checks_dir}/DA114A.yaml +%{trento_checks_dir}/DC5429.yaml +%{trento_checks_dir}/F50AF5.yaml +%{trento_checks_dir}/FB0E0D.yaml %changelog