From 40950130a51c3acb94fd858ab069e6c664d8f34a Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sat, 14 Oct 2023 21:59:11 +0200 Subject: [PATCH] automatically build from source if the version is @branch --- .forgejo/workflows/integration-nested.yml | 14 ++------ .forgejo/workflows/integration.yml | 8 ----- README.md | 5 --- RUNNER_REPOSITORY | 1 + RUNNER_VERSION | 1 + action.yml | 9 ++--- forgejo-runner-lib.sh | 42 +++++++++++++++++++++++ forgejo-runner.sh | 20 ++++++++--- forgejo-test-helper.sh | 36 ++----------------- utils/upgrade-runner.sh | 17 +++++++++ 10 files changed, 83 insertions(+), 70 deletions(-) create mode 100644 RUNNER_REPOSITORY create mode 100644 RUNNER_VERSION create mode 100644 forgejo-runner-lib.sh create mode 100755 utils/upgrade-runner.sh diff --git a/.forgejo/workflows/integration-nested.yml b/.forgejo/workflows/integration-nested.yml index de771a4..ca37fe0 100644 --- a/.forgejo/workflows/integration-nested.yml +++ b/.forgejo/workflows/integration-nested.yml @@ -9,16 +9,8 @@ jobs: LXC_IP_PREFIX=10.0.9 ./forgejo-dependencies.sh export PATH=$(pwd):$PATH forgejo.sh setup root admin1234 codeberg.org/forgejo/forgejo 1.20 - # - # Uncomment the following for a shortcut to debugging the Forgejo runner. - # It will build the runner from a designated repository and branch instead of - # downloading it from a canonical release. - # - # ./forgejo-test-helper.sh build_runner http://code.forgejo.org/forgejo/runner branch-under-debug - # export PATH=$(pwd)/forgejo-runner:$PATH - # - ./forgejo-runner.sh setup + forgejo-runner.sh setup export FORGEJO_RUNNER_LOGS=forgejo-runner.log echo "============================ sanity-check ===================" - ./forgejo-test-helper.sh push_self_action http://root:admin1234@$(cat forgejo-ip):3000 root setup-forgejo vTest - ./forgejo-test-helper.sh run_workflow testdata/sanity-checks http://root:admin1234@$(cat forgejo-ip):3000 root sanity-check setup-forgejo $(cat forgejo-token) + forgejo-test-helper.sh push_self_action http://root:admin1234@$(cat forgejo-ip):3000 root setup-forgejo vTest + forgejo-test-helper.sh run_workflow testdata/sanity-checks http://root:admin1234@$(cat forgejo-ip):3000 root sanity-check setup-forgejo $(cat forgejo-token) diff --git a/.forgejo/workflows/integration.yml b/.forgejo/workflows/integration.yml index 33013f5..360abe6 100644 --- a/.forgejo/workflows/integration.yml +++ b/.forgejo/workflows/integration.yml @@ -21,14 +21,6 @@ jobs: LXC_IP_PREFIX=10.0.10 ./forgejo-dependencies.sh export PATH=$(pwd):$PATH forgejo.sh setup root admin1234 ${{ matrix.info.image }} ${{ matrix.info.version }} - # - # Uncomment the following for a shortcut to debugging the Forgejo runner. - # It will build the runner from a designated repository and branch instead of - # downloading it from a canonical release. - # - #./forgejo-test-helper.sh build_runner http://code.forgejo.org/earl-warren/runner wip-sync - #export PATH=$(pwd)/forgejo-runner:$PATH - # forgejo-runner.sh setup export FORGEJO_RUNNER_LOGS=forgejo-runner.log url=http://root:admin1234@$(cat forgejo-ip):3000 diff --git a/README.md b/README.md index fc87acf..307be22 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,6 @@ The forgejo-test-helper.sh script is available to help test and debug actions. * `forgejo-test-helper.sh push testrepo $forgejo root testrepo` Creates the repository `$forgejo/root/testrepo` and populates it with the content of the `testrepo` directory. -* `forgejo-test-helper.sh build_runner $forgejo/forgejo/runner v3.0.1` - Builds the forgejo runner from source in `./forgejo-runner/forgejo-runner`. - `export PATH=$(pwd)/forgejo-runner:$PATH` will ensure that calling `forgejo-runner.sh` - will use this binary instead of downloading a released version of the runner. - If the version is not specified, build from the main branch. The combination of `push_self_action` and `run_workflow` allows to run Forgejo Actions workflows from `testrepo` that use the action diff --git a/RUNNER_REPOSITORY b/RUNNER_REPOSITORY new file mode 100644 index 0000000..b12db6c --- /dev/null +++ b/RUNNER_REPOSITORY @@ -0,0 +1 @@ +https://code.forgejo.org/forgejo/runner diff --git a/RUNNER_VERSION b/RUNNER_VERSION new file mode 100644 index 0000000..b105cea --- /dev/null +++ b/RUNNER_VERSION @@ -0,0 +1 @@ +v3.0.1 diff --git a/action.yml b/action.yml index a9bc3f4..7e2ef51 100644 --- a/action.yml +++ b/action.yml @@ -20,11 +20,6 @@ description: | * `forgejo-test-helper.sh push testrepo $forgejo root testrepo` Creates the repository `$forgejo/root/testrepo` and populates it with the content of the `testrepo` directory. - * `forgejo-test-helper.sh build_runner $forgejo/forgejo/runner v3.0.1` - Builds the forgejo runner from source in `./forgejo-runner/forgejo-runner`. - `export PATH=$(pwd)/forgejo-runner:$PATH` will ensure that calling `forgejo-runner.sh` - will use this binary instead of downloading a released version of the runner. - If the version is not specified, build from the main branch. The combination of `push_self_action` and `run_workflow` allows to run Forgejo Actions workflows from `testrepo` that use the action @@ -47,10 +42,10 @@ inputs: description: 'Administrator password' default: 'admin1234' runner: - description: 'Runner git repository' + description: 'Runner repository' default: 'https://code.forgejo.org/forgejo/runner' runner-version: - description: 'Runner version' + description: 'Runner version. If it starts with @ (for instance @featurebranch), the runner will be built from source using the specified branch.' default: 'v3.0.1' container: description: 'Name of the container running the Forgejo instance' diff --git a/forgejo-runner-lib.sh b/forgejo-runner-lib.sh new file mode 100644 index 0000000..ccdd83d --- /dev/null +++ b/forgejo-runner-lib.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +set -ex + +export DEBIAN_FRONTEND=noninteractive + +function dependency_go() { + go_version="1.21.3.linux-amd64" # Set the desired Go version here + + if ! which wget tar > /dev/null ; then + apt-get install -y -qq wget tar + fi + + if ! which go > /dev/null ; then + wget --quiet "https://go.dev/dl/go$go_version.tar.gz" + tar zxf "go$go_version.tar.gz" + export PATH="$(pwd)/go/bin:$PATH" + fi +} + +function checkout() { + local git="$1" + git clone --depth 1 $git forgejo-runner +} + +function build_runner() { + local git="$1" + local version="${2:-main}" + local dir=$(mktemp -d) + ( + cd $dir + checkout "$git" + dependency_go + cd forgejo-runner + git checkout "$version" + make build + ) + mv $dir/forgejo-runner/forgejo-runner . + ./forgejo-runner --version + rm -fr "$dir" +} diff --git a/forgejo-runner.sh b/forgejo-runner.sh index 21166bf..e2fb54e 100755 --- a/forgejo-runner.sh +++ b/forgejo-runner.sh @@ -3,12 +3,15 @@ set -ex +PS4='${BASH_SOURCE[0]}:$LINENO: ${FUNCNAME[0]}: ' SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $SELF_DIR/forgejo-runner-lib.sh + : ${FORGEJO_RUNNER_CONFIG:=$SELF_DIR/runner-config.yaml} function dependencies() { - if ! which curl daemon > /dev/null ; then - apt-get install -y -qq curl daemon + if ! which make curl daemon > /dev/null ; then + apt-get install -y -qq make curl daemon fi } @@ -49,12 +52,19 @@ function reload() { } function setup() { - local runner_repository="${1:-https://code.forgejo.org/forgejo/runner}" - local version="${2:-v3.0.1}" + local default_runner_repository="$(cat $SELF_DIR/RUNNER_REPOSITORY)" + local runner_repository="${1:-${default_runner_repository}}" + local default_version="$(cat $SELF_DIR/RUNNER_VERSION)" + local version="${2:-${default_version}}" local forgejo="${3:-http://$(cat forgejo-ip):3000/}" dependencies - download $runner_repository $version + if [[ "$version" =~ ^@ ]] ; then + local branch=${version##@} + build_runner $runner_repository $branch + else + download $runner_repository $version + fi register $forgejo run } diff --git a/forgejo-test-helper.sh b/forgejo-test-helper.sh index ccc4a62..7283edf 100755 --- a/forgejo-test-helper.sh +++ b/forgejo-test-helper.sh @@ -3,7 +3,8 @@ set -ex -export DEBIAN_FRONTEND=noninteractive +SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $SELF_DIR/forgejo-runner-lib.sh : ${LOOPS:=80} : ${LOOP_DELAY:=5} @@ -11,39 +12,6 @@ DIR=$(mktemp -d) trap "rm -fr $DIR" EXIT -function dependency_go() { - go_version="1.21.1.linux-amd64" # Set the desired Go version here - - if ! which go > /dev/null ; then - apt-get update - apt-get install -y -qq wget tar - wget --quiet "https://go.dev/dl/go$go_version.tar.gz" - tar zxf "go$go_version.tar.gz" - export PATH="$(pwd)/go/bin:$PATH" - fi -} - -function checkout() { - local git="$1" - rm -fr forgejo-runner - git clone $git forgejo-runner -} - -function build_runner() { - local git="$1" - local version="${2:-main}" - - ( - checkout "$git" - dependency_go - cd forgejo-runner - git checkout "$version" - make build - ) - export PATH=$PATH:$(pwd)/forgejo-runner - forgejo-runner --version -} - function branch_tip() { local url="$1" local repo="$2" diff --git a/utils/upgrade-runner.sh b/utils/upgrade-runner.sh new file mode 100755 index 0000000..ef25c0e --- /dev/null +++ b/utils/upgrade-runner.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +set -e + +ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )" + +REPOSITORY="$1" +VERSION="$2" + +echo $VERSION > $ROOT_DIR/RUNNER_VERSION +sed -i -e 's^| runner-version | Runner version |.*^| runner-version | Runner version | `false` | '$VERSION' |^' $ROOT_DIR/README.md +sed -i -e "/runner-version:/{n;n;s^.*default:.*^ default: '$VERSION'^}" $ROOT_DIR/action.yml + +echo $REPOSITORY > $ROOT_DIR/RUNNER_REPOSITORY +sed -i -e 's^| runner | Runner git repository |.*^| runner | Runner git repository | `false` | '$REPOSITORY' |^' $ROOT_DIR/README.md +sed -i -e "/runner:/{n;n;s^.*default:.*^ default: '$REPOSITORY'^}" $ROOT_DIR/action.yml