Skip to content

Commit

Permalink
Deploy RPM to OBS (#5)
Browse files Browse the repository at this point in the history
* Deploy RPM to OBS

* Add empty build section

* Run spec-cleaner

* Number source
  • Loading branch information
janvhs authored Aug 22, 2024
1 parent b4339c6 commit 19642c9
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 47 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches: [main]
pull_request:
release:
types: [published]

jobs:
tlint:
Expand All @@ -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/[email protected]
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 [email protected] -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 }}"
17 changes: 17 additions & 0 deletions hack/get_version_from_git.sh
Original file line number Diff line number Diff line change
@@ -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}"
79 changes: 79 additions & 0 deletions hack/gh_release_to_obs_changeset.py
Original file line number Diff line number Diff line change
@@ -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())
18 changes: 18 additions & 0 deletions packaging/suse/rpm/_service
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<services>
<service name="tar_scm" mode="manual">
<param name="url">https://github.com/%%REPOSITORY%%.git</param>
<param name="scm">git</param>
<param name="revision">%%REVISION%%</param>
<param name="exclude">.git</param>
<param name="exclude">.github</param>
<param name="versionformat">%%VERSION%%</param>
<param name="filename">trento-checks</param>
</service>
<service name="set_version" mode="manual">
<param name="file">trento-checks.spec</param>
</service>
<service name="recompress" mode="manual">
<param name="file">*.tar</param>
<param name="compression">gz</param>
</service>
</services>
99 changes: 52 additions & 47 deletions packaging/suse/rpm/trento-checks.spec
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 19642c9

Please sign in to comment.