From f95a5d6f2c3a7d9ff07ae68cacc08d94dd47ef38 Mon Sep 17 00:00:00 2001 From: cnouguier Date: Mon, 22 Apr 2024 21:41:18 +0200 Subject: [PATCH] wip: Refactor CI to use GitHub actions and generic bash scripts #846 --- .github/workflows/main.yaml | 49 +++++++++++++++++++++++++ .travis.doc.sh | 8 ----- .travis.test.sh | 72 ------------------------------------- .travis.yml | 48 ------------------------- scripts/init_runner.sh | 26 ++++++++++++++ scripts/run_tests.sh | 45 +++++++++++++++++++++++ scripts/setup_workspace.sh | 45 +++++++++++++++++++++++ 7 files changed, 165 insertions(+), 128 deletions(-) create mode 100644 .github/workflows/main.yaml delete mode 100644 .travis.doc.sh delete mode 100644 .travis.test.sh delete mode 100644 .travis.yml create mode 100755 scripts/init_runner.sh create mode 100755 scripts/run_tests.sh create mode 100755 scripts/setup_workspace.sh diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 000000000..a9935b519 --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,49 @@ +name: ci +on: [ push, workflow_dispatch ] + +jobs: + run_tests: + name: Run tests + runs-on: ubuntu-22.04 + steps: + - name: Checkout repo + uses: actions/checkout@v4 + with: + submodules: true + - name: Init runner + run: bash ./scripts/init_runner.sh ${{ github.job }} + - name: Setup workspace + env: + GITHUB_DEVELOPMENT_TOKEN: ${{ secrets.GH_DEVELOPMENT_TOKEN }} + run: bash ./scripts/setup_workspace.sh + - name: Run tests + env: + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} + run: bash ./scripts/run_tests.sh -c -r ${{ github.job }} + + additional_tests: + strategy: + max-parallel: 1 + fail-fast: false + matrix: + node: [ 18, 20 ] + mongo: [ 5, 6 ] + name: Additional tests (node ${{ matrix.node }}) + if: ${{ contains(github.event.head_commit.message, 'additional tests') }} + runs-on: ubuntu-22.04 + steps: + - name: Checkout repo + uses: actions/checkout@v4 + with: + submodules: true + - name: Init runner + run: bash ./scripts/init_runner.sh ${{ github.job }} + - name: Setup workspace + env: + GITHUB_DEVELOPMENT_TOKEN: ${{ secrets.GH_DEVELOPMENT_TOKEN }} + run: bash ./scripts/setup_workspace.sh + - name: Run tests + env: + SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} + run: bash ./scripts/run_tests.sh -n ${{ matrix.node }} -m ${{ matrix.mongo }} + diff --git a/.travis.doc.sh b/.travis.doc.sh deleted file mode 100644 index e5a44f158..000000000 --- a/.travis.doc.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -cd docs && yarn install && yarn build -RESULT_CODE=$? -if [ $RESULT_CODE -ne 0 ]; then - echo "Doc generation failed [error: $RESULT_CODE]" - exit 1 -fi diff --git a/.travis.test.sh b/.travis.test.sh deleted file mode 100644 index 425b9e403..000000000 --- a/.travis.test.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash - -check_code() -{ - if [ $1 -eq 1 ]; then - echo "$2 has failed [error: $1]" - exit 1 - fi -} - -AGE_VERSION=1.1.1 -SOPS_VERSION=3.8.1 -TMP_PATH="$(mktemp -d -p "${XDG_RUNTIME_DIR:-}" kalisio.XXXXXX)" - -install_age() { - local DL_PATH="$TMP_PATH/age" - mkdir "$DL_PATH" && pushd "$DL_PATH" || exit - wget -q https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-linux-amd64.tar.gz - # no checksum ... - tar xf age-v${AGE_VERSION}-linux-amd64.tar.gz - cp age/age "$HOME/.local/bin" - popd || exit -} - -install_sops() { - local DL_PATH="$TMP_PATH/sops" - mkdir "$DL_PATH" && pushd "$DL_PATH" || exit - wget -q https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.linux.amd64 - wget -q https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.checksums.txt - sha256sum --ignore-missing --quiet -c sops-v${SOPS_VERSION}.checksums.txt - cp sops-v${SOPS_VERSION}.linux.amd64 "$HOME/.local/bin/sops" - chmod a+x "$HOME/.local/bin/sops" - popd || exit -} - -# Make sure that folder exists, with Travis CI it's already in the PATH -mkdir -p "$HOME/.local/bin" -install_age -install_sops - -echo -e "machine github.com\n login $GITHUB_TOKEN" > ~/.netrc - -# Setup KDK -yarn install - -# Clone others direct dependencies we'd like to use for testing -git clone https://github.com/kalisio/feathers-distributed && cd feathers-distributed && yarn install && yarn link && cd .. -yarn link @kalisio/feathers-distributed - -git clone https://github.com/kalisio/feathers-s3 && cd feathers-s3 && yarn install && yarn link && cd .. -yarn link @kalisio/feathers-s3 - -git clone https://github.com/kalisio/feathers-import-export && cd feathers-import-export && yarn install && yarn link && yarn link @kalisio/feathers-s3 && cd .. -yarn link @kalisio/feathers-import-export - -git clone https://github.com/kalisio/feathers-webpush && cd feathers-webpush && yarn install && yarn link && cd .. -yarn link @kalisio/feathers-webpush - -git clone https://github.com/weacast/weacast && cd weacast && yarn install && cd packages -cd core && yarn link && cd .. && cd gfs && yarn link && cd .. && cd probe && yarn link && cd .. -cd ../.. -yarn link @weacast/core -yarn link @weacast/gfs -yarn link @weacast/probe - -# Clone the development project and configure the env -git clone https://oauth2:$GITHUB_TOKEN@github.com/kalisio/development.git "development" -source development/workspaces/libs/libs.sh kdk - -# Run the test -yarn test -check_code $? "Running tests" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 68e5380ef..000000000 --- a/.travis.yml +++ /dev/null @@ -1,48 +0,0 @@ -notifications: - email: false - slack: - rooms: - secure: u/atzAwEqJCX5i3aaIxEQ+E+4x+l9TUZYS+eN+Hkbi4lws+Jt4/UcuyM3L/UVigbfKOHp3OVTj+KQiznI2PGtwbEnpIsEgsAaciPiM5MpwKx2RZxQPgniO0+03N0gZhNlhInXWWNT5/ZTJoylFa4U8XrezCFxskuQojIImha8ZKOd8vYXvqAHExGCaztlBaFhoTH/Cu988GuyexIw06JkT2Djam47jET5QDPehGpXVgW3c8aHfChwURGTgk1oUeVAwfEI2YpouzgycAoldhEltJyz0V2N+XNme7T3O7prdm2mRmVXZUs6six65dvl91lcxLWHzTKFcJVqryCkRl2KMtgZpdtp4Am3T2bqCF2XoEzDipY0mIOhuiMsu+RUlMWgds0L9IeGjkt6GPiTc+9ZazunU//ukFrqMsEDdWkqIROXU7vysb3w1yqFFwMTwas0Kjb3E+wXKXCGTxyiO1i0yx4DQ1prMxL4ZIIeYHLMzIQ7gy4hFmBuN7qkkPC2g/gLY4qTrfqsBSYeELVggwqOjlwodC3bKLnr5urie1G9NGmBeBhLV22x2vblr8/PJ5MpVpqn1ZPXqZ5R/989VWbUYmhZ29mySjxNRNVOB2gqd3mewsTsMczQS4f6/ojib8+fe+Q1RNJ28SKZEGhFepkowcbCyNkZzhVMqbv637aDtU= - on_success: always - on_failure: always -stages: -- name: TEST - if: commit_message !~ /(skip[ _-]test)/ -- name: DOC - if: commit_message =~ /\[build doc\]/ -jobs: - include: - - stage: TEST - language: node_js - node_js: - - '16' - services: - - docker - - mongodb - env: - - CC_TEST_REPORTER_ID=42cc93bb51067210ce09b5b3b98a6d5cc4d9c432f51d5844d4e61c36fff160fc - install: - - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 - > ./cc-test-reporter - - chmod +x ./cc-test-reporter - - "./cc-test-reporter before-build" - script: - - bash .travis.test.sh - after_script: - - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT" - - stage: DOC - os: linux - dist: focal - language: node_js - node_js: - - '18' - install: true - script: bash .travis.doc.sh - deploy: - provider: pages - local-dir: docs/.vitepress/dist - skip-cleanup: true - github-token: "$GITHUB_TOKEN" - keep-history: true - on: - branch: master diff --git a/scripts/init_runner.sh b/scripts/init_runner.sh new file mode 100755 index 000000000..51cc2e2bf --- /dev/null +++ b/scripts/init_runner.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail +# set -x + +JOB_ID=$1 + +THIS_FILE=$(readlink -f "${BASH_SOURCE[0]}") +THIS_DIR=$(dirname "$THIS_FILE") + +. "$THIS_DIR/kash/kash.sh" + +### Github Actions + +init_github_run_tests() { + install_reqs age sops nvm node16 cc_test_reporter +} + +init_github_additional_tests() { + install_reqs age sops nvm node18 node20 mongo5 mongo6 #mongo7 +} + +begin_group "Init $CI_ID for $JOB_ID" + +init_"${CI_ID}_${JOB_ID}" + +end_group "Init $CI_ID for $JOB_ID" \ No newline at end of file diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh new file mode 100755 index 000000000..5d376c7c8 --- /dev/null +++ b/scripts/run_tests.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail +# set -x + +THIS_FILE=$(readlink -f "${BASH_SOURCE[0]}") +THIS_DIR=$(dirname "$THIS_FILE") +ROOT_DIR=$(dirname "$THIS_DIR") +WORKSPACE_DIR="$(dirname "$ROOT_DIR")" + +. "$THIS_DIR/kash/kash.sh" + +## Parse options +## + +NODE_VER=16 +MONGO_VER="" +CI_STEP_NAME="Run tests" +CODE_COVERAGE=false +while getopts "n:cr:" option; do + case $option in + n) # defines node version + NODE_VER=$OPTARG + ;; + c) # publish code coverage + CODE_COVERAGE=true + ;; + r) # report outcome to slack + CI_STEP_NAME=$OPTARG + load_env_files "$WORKSPACE_DIR/development/common/SLACK_WEBHOOK_LIBS.enc.env" + trap 'slack_ci_report "$ROOT_DIR" "$CI_STEP_NAME" "$?" "$SLACK_WEBHOOK_LIBS"' EXIT + ;; + *) + ;; + esac +done + +## Init workspace +## + +. "$WORKSPACE_DIR/development/workspaces/libs/libs.sh" kdk + +## Run tests +## + +run_lib_tests "$ROOT_DIR" "$CODE_COVERAGE" "$NODE_VER" "$MONGO_VER" diff --git a/scripts/setup_workspace.sh b/scripts/setup_workspace.sh new file mode 100755 index 000000000..af3539760 --- /dev/null +++ b/scripts/setup_workspace.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail +# set -x + +THIS_FILE=$(readlink -f "${BASH_SOURCE[0]}") +THIS_DIR=$(dirname "$THIS_FILE") +ROOT_DIR=$(dirname "$THIS_DIR") + +. "$THIS_DIR/kash/kash.sh" + +## Parse options +## + +begin_group "Setting up workspace ..." + +if [ "$CI" = true ]; then + WORKSPACE_DIR="$(dirname "$ROOT_DIR")" + DEVELOPMENT_REPO_URL="https://$GITHUB_DEVELOPMENT_TOKEN@github.com/kalisio/development.git" +else + while getopts "b:t" option; do + case $option in + b) # defines branch + WORKSPACE_BRANCH=$OPTARG;; + t) # defines tag + WORKSPACE_TAG=$OPTARG;; + *) + ;; + esac + done + + shift $((OPTIND-1)) + WORKSPACE_DIR="$1" + DEVELOPMENT_REPO_URL="$GITHUB_URL/kalisio/development.git" + + # Clone project in the workspace + git_shallow_clone "$GITHUB_URL/kalisio/kdk.git" "$WORKSPACE_DIR/kdk" "${WORKSPACE_TAG:-${WORKSPACE_BRANCH:-}}" +fi + +setup_lib_workspace "$WORKSPACE_DIR" "$DEVELOPMENT_REPO_URL" \ + "https://github.com/kalisio/feathers-distributed.git" \ + "https://github.com/kalisio/feathers-webpush.git" \ + "https://github.com/kalisio/feathers-s3.git" \ + "https://github.com/kalisio/feathers-import-export.git" + +end_group "Setting up workspace ..."