From 9cc1f18da89276baf018bc271bfdcb33bfa284fd Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Fri, 20 Jan 2023 10:43:14 +0100 Subject: [PATCH 1/4] Add hack directory with pipeline scripts Co-authored-by: rtorrero --- hack/get_version_from_git.sh | 17 +++++++ hack/gh_release_to_obs_changeset.py | 79 +++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 hack/get_version_from_git.sh create mode 100755 hack/gh_release_to_obs_changeset.py diff --git a/hack/get_version_from_git.sh b/hack/get_version_from_git.sh new file mode 100755 index 00000000..c811cd1d --- /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 00000000..12305ffd --- /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()) From 12fe492cf8036919873fd83db5b21638b94c174b Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Fri, 20 Jan 2023 10:44:38 +0100 Subject: [PATCH 2/4] Add suse packaging Dockerfile Co-authored-by: rtorrero --- packaging/suse/Dockerfile | 32 ++++++++++++++++++++++++++++++++ packaging/suse/_constraints | 7 +++++++ packaging/suse/_service | 4 ++++ 3 files changed, 43 insertions(+) create mode 100644 packaging/suse/Dockerfile create mode 100644 packaging/suse/_constraints create mode 100644 packaging/suse/_service diff --git a/packaging/suse/Dockerfile b/packaging/suse/Dockerfile new file mode 100644 index 00000000..de04be39 --- /dev/null +++ b/packaging/suse/Dockerfile @@ -0,0 +1,32 @@ +# SPDX-License-Identifier: Apache-2.0 +#!BuildTag: trento/trento-wanda:latest +#!BuildTag: trento/trento-wanda:%%VERSION%% +#!BuildTag: trento/trento-wanda:%%VERSION%%-build%RELEASE% +#!UseOBSRepositories + +FROM bci/rust:1.66 AS release +ADD wanda.tar.gz /build/ +# Workaround for https://github.com/openSUSE/obs-build/issues/487 +RUN zypper --non-interactive in sles-release +RUN zypper -n in elixir elixir-hex erlang-rebar3 +WORKDIR /build/wanda/ +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 +ENV MIX_ENV=prod +ENV MIX_HOME=/usr/bin +ENV VERSION=%%VERSION%% +RUN mix phx.digest +RUN mix release + +FROM bci/rust:1.66 AS wanda +# Define labels according to https://en.opensuse.org/Building_derived_containers +# labelprefix=com.suse.trento +LABEL org.opencontainers.image.source="https://github.com/trento-project/wanda" +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 +WORKDIR /app +COPY --from=release /build/wanda/_build/prod/rel/wanda . +EXPOSE 4000/tcp +ENTRYPOINT ["/app/bin/wanda"] diff --git a/packaging/suse/_constraints b/packaging/suse/_constraints new file mode 100644 index 00000000..3cab4e22 --- /dev/null +++ b/packaging/suse/_constraints @@ -0,0 +1,7 @@ + + + + 8 + + + \ No newline at end of file diff --git a/packaging/suse/_service b/packaging/suse/_service new file mode 100644 index 00000000..56ac46b6 --- /dev/null +++ b/packaging/suse/_service @@ -0,0 +1,4 @@ + + + + From df0c6d9c038b9292f35fc7f9456470b385812cb6 Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Fri, 20 Jan 2023 10:46:28 +0100 Subject: [PATCH 3/4] Add OBS commit step in pipeline Co-authored-by: rtorrero --- .github/workflows/ci.yaml | 90 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 96176697..fa0af195 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,6 +20,10 @@ jobs: name: Elixir dependencies runs-on: ubuntu-latest steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.11.0 + with: + access_token: ${{ github.token }} - name: Checkout uses: actions/checkout@v3 with: @@ -70,6 +74,10 @@ jobs: needs: elixir-deps runs-on: ubuntu-latest steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.11.0 + with: + access_token: ${{ github.token }} - name: Checkout uses: actions/checkout@v3 with: @@ -126,6 +134,10 @@ jobs: ports: - 5674:5672 steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.11.0 + with: + access_token: ${{ github.token }} - name: Checkout uses: actions/checkout@v3 with: @@ -167,14 +179,14 @@ jobs: fetch-depth: 0 - uses: docker/setup-buildx-action@v2 - name: Log in to the Container registry - uses: docker/login-action@49ed152c8eca782a232dede0303416e8f356c37b + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@69f6fc9d46f2f8bf0d5491e4aabe0bb8c6a4678a + uses: docker/metadata-action@507c2f2dc502c992ad446e3d7a5dfbe311567a96 with: images: ${{ env.IMAGE_REPOSITORY }} - name: Build and push container image @@ -221,3 +233,77 @@ jobs: with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./doc + + obs-commit: + name: Commit the project on OBS + runs-on: ubuntu-latest + if: github.event_name == 'release' || (github.event_name == 'push' && github.ref_name == 'main') || github.event_name == 'workflow_dispatch' + needs: [static-code-analysis, test] + container: + image: ghcr.io/trento-project/continuous-delivery:main + env: + GITHUB_OAUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + DEST_FOLDER: "/tmp/osc_project" + NAME: trento-wanda-image + OBS_USER: ${{ secrets.OBS_USER }} + OBS_PASS: ${{ secrets.OBS_PASS }} + OBS_PROJECT: ${{ secrets.OBS_PROJECT }} + FOLDER: packaging/suse + REPOSITORY: ${{ github.repository }} + options: -u 0:0 + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.11.0 + with: + access_token: ${{ github.token }} + - name: Checkout + uses: actions/checkout@v3 + - uses: actions-ecosystem/action-get-latest-tag@v1 + id: latest-tag + with: + semver_only: true + initial_version: 0.0.1 + - name: Setup rust + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.66 + - name: Get mix deps + run: mix local.hex --force && mix local.rebar --force && mix deps.clean --all && mix deps.get + - name: cargo vendor + run: | + cd deps/rhai_rustler/native/rhai_rustler + cargo vendor + printf '\n\n[source.crates-io]\nreplace-with = "vendored-sources"\n\n[source.vendored-sources]\ndirectory = "vendor"\n' >> .cargo/config + - name: Configure OSC + # OSC credentials must be configured beforehand as the HOME variables cannot be changed from /github/home + # that is used to run osc commands + run: | + mkdir -p $HOME/.config/osc + cp /home/osc/.config/osc/oscrc $HOME/.config/osc + /scripts/init_osc_creds.sh + - name: Prepare .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: | + CHANGES_FILE=$NAME.changes + osc checkout $OBS_PROJECT $NAME $CHANGES_FILE + mv $CHANGES_FILE $FOLDER + VERSION=${{ steps.latest-tag.outputs.tag }} + hack/gh_release_to_obs_changeset.py $REPOSITORY -a shap-staff@suse.de -t $VERSION -f $FOLDER/$CHANGES_FILE + - name: Set version + run: | + git config --global --add safe.directory /__w/wanda/wanda + VERSION=$(./hack/get_version_from_git.sh) + # "+" character is not allowed in OBS dockerfile version strings + VERSION=${VERSION//[+]/-} + sed -i 's~%%VERSION%%~'"${VERSION}"'~' packaging/suse/Dockerfile + - name: Commit on OBS + run: | + OBS_PACKAGE=$OBS_PROJECT/$NAME + osc checkout $OBS_PACKAGE -o $DEST_FOLDER + cp -r packaging/suse/* $DEST_FOLDER + tar --transform 's,^./,/wanda/,' -zcvf $DEST_FOLDER/wanda.tar.gz --exclude=./.git ./* + cd $DEST_FOLDER + osc ar + osc commit -m "New development version of $NAME released" From 8a1590b339ad85aaee969d809d395871535e8d85 Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Mon, 23 Jan 2023 11:34:19 +0100 Subject: [PATCH 4/4] Rename gh docker image from `wanda` to `trento-wanda` for consistency with web --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fa0af195..57c0b9fd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -171,7 +171,7 @@ jobs: packages: write env: REGISTRY: ghcr.io - IMAGE_REPOSITORY: ghcr.io/${{ github.repository_owner }}/wanda + IMAGE_REPOSITORY: ghcr.io/${{ github.repository_owner }}/trento-wanda IMAGE_TAG: "${{ (github.event_name == 'release' && github.event.release.tag_name) || (github.event_name == 'push' && github.ref_name == 'main' && 'rolling') || github.sha }}" steps: - uses: actions/checkout@v3