Skip to content

Commit

Permalink
wip: Refactor CI to use GitHub actions and generic bash scripts (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
claustres committed Apr 23, 2024
1 parent cbd387d commit 6344871
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 33 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: CI
on: [ push, workflow_dispatch ]

jobs:
run_tests:
name: Run tests
if: ${{ !contains(github.event.head_commit.message, 'skip 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_PAT: ${{ secrets.GH_DEVELOPMENT_PAT }}
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:
fail-fast: false
matrix:
node: [ 16, 18, 20 ]
mongo: [ 4, 5, 6 ]
exclude:
- node: 16
mongo: 4
name: Additional tests (node ${{ matrix.node }}, mongo ${{ matrix.mongo }})
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_PAT: ${{ secrets.GH_DEVELOPMENT_PAT }}
run: bash ./scripts/setup_workspace.sh -n ${{ matrix.node }}
- name: Run tests
env:
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
run: bash ./scripts/run_tests.sh -n ${{ matrix.node }} -m ${{ matrix.mongo }}

build_service:
name: Build service
if: ${{ !contains(github.event.head_commit.message, 'skip build') }}
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_PAT: ${{ secrets.GH_DEVELOPMENT_PAT }}
run: bash ./scripts/setup_workspace.sh
- name: Build service
env:
SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }}
run: bash ./scripts/build_service.sh -p -r ${{ github.job }}
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "scripts/kash"]
path = scripts/kash
url = https://github.com/kalisio/kash
9 changes: 0 additions & 9 deletions .travis.build.sh

This file was deleted.

9 changes: 0 additions & 9 deletions .travis.env.sh

This file was deleted.

15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

70 changes: 70 additions & 0 deletions scripts/build_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/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
##

PUBLISH=false
CI_STEP_NAME="Build app"
while getopts "pr:" option; do
case $option in
p) # publish app
PUBLISH=true
;;
r) # report outcome to slack
CI_STEP_NAME=$OPTARG
trap 'slack_ci_report "$ROOT_DIR" "$CI_STEP_NAME" "$?" "$SLACK_WEBHOOK_SERVICES"' EXIT
;;
*)
;;
esac
done

## Init workspace
##

WORKSPACE_DIR="$(dirname "$ROOT_DIR")"
init_lib_infos "$ROOT_DIR"

LIB=$(get_lib_name)
VERSION=$(get_lib_version)
GIT_TAG=$(get_lib_tag)

echo "About to build ${LIB} v${VERSION}..."

load_env_files "$WORKSPACE_DIR/development/common/kalisio_dockerhub.enc.env" "$WORKSPACE_DIR/development/common/SLACK_WEBHOOK_SERVICES.enc.env"
load_value_files "$WORKSPACE_DIR/development/common/KALISIO_DOCKERHUB_PASSWORD.enc.value"

## Build container
##

IMAGE_NAME="kalisio/k2"
if [[ -z "$GIT_TAG" ]]; then
IMAGE_TAG=latest
else
IMAGE_TAG=$VERSION
fi

begin_group "Building container ..."

docker login --username "$KALISIO_DOCKERHUB_USERNAME" --password-stdin < "$KALISIO_DOCKERHUB_PASSWORD"
# DOCKER_BUILDKIT is here to be able to use Dockerfile specific dockerginore (app.Dockerfile.dockerignore)
DOCKER_BUILDKIT=1 docker build \
-f Dockerfile \
-t "$IMAGE_NAME:$IMAGE_TAG" \
"$WORKSPACE_DIR"

if [ "$PUBLISH" = true ]; then
docker push "$IMAGE_NAME:$IMAGE_TAG"
fi

docker logout

end_group "Building container ..."
30 changes: 30 additions & 0 deletions scripts/init_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/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_build_service() {
install_reqs age sops nvm node16
}

init_github_additional_tests() {
install_reqs age sops nvm node16 node18 node20
}

begin_group "Init $CI_ID for $JOB_ID"

init_"${CI_ID}_${JOB_ID}"

end_group "Init $CI_ID for $JOB_ID"
1 change: 1 addition & 0 deletions scripts/kash
Submodule kash added at 1d5763
45 changes: 45 additions & 0 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
@@ -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=4
CI_STEP_NAME="Run tests"
CODE_COVERAGE=false
while getopts "m: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
load_env_files "$WORKSPACE_DIR/development/common/SLACK_WEBHOOK_SERVICES.enc.env"
CI_STEP_NAME=$OPTARG
trap 'slack_ci_report "$ROOT_DIR" "$CI_STEP_NAME" "$?" "$SLACK_WEBHOOK_SERVICES"' EXIT
;;
*)
;;
esac
done

## Init workspace
##

. "$WORKSPACE_DIR/development/workspaces/services/services.sh" k2

## Run tests
##

run_lib_tests "$ROOT_DIR" "$CODE_COVERAGE" "$NODE_VER"
44 changes: 44 additions & 0 deletions scripts/setup_workspace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/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
##

WORKSPACE_BRANCH=
WORKSPACE_TAG=

begin_group "Setting up workspace ..."

if [ "$CI" = true ]; then
WORKSPACE_DIR="$(dirname "$ROOT_DIR")"
DEVELOPMENT_REPO_URL="https://$GITHUB_DEVELOPMENT_PAT@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/k2.git" "$WORKSPACE_DIR/k2" "${WORKSPACE_TAG:-${WORKSPACE_BRANCH:-}}"
fi

setup_lib_workspace "$WORKSPACE_DIR" "$DEVELOPMENT_REPO_URL"

end_group "Setting up workspace ..."

0 comments on commit 6344871

Please sign in to comment.