From a454c3165f3e9156c2848f648842bbfa30aca480 Mon Sep 17 00:00:00 2001 From: cnouguier Date: Thu, 18 Apr 2024 20:11:09 +0200 Subject: [PATCH] wip: Refactor CI to use GitHub actions and generic bash scripts #27 --- .github/workflows/main.yaml | 4 +-- scripts/build.sh | 67 +++++++++++-------------------------- scripts/setup_workspace.sh | 24 +++---------- 3 files changed, 25 insertions(+), 70 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index fa46529..aa56823 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-22.04 strategy: matrix: - variant: [hydro-stations, hydro-observations, hydro-predictions, piezo-stations, piezo-observations] + variant: [ hydro-stations, hydro-observations, hydro-predictions, piezo-stations, piezo-observations ] steps: - name: Checkout repo uses: actions/checkout@v4 @@ -23,5 +23,5 @@ jobs: - name: Build job env: SOPS_AGE_KEY: ${{ secrets.SOPS_AGE_KEY }} - run: bash ./scripts/build.sh -p -r ${{ github.job }} ${{ matrix.variant }} + run: bash ./scripts/build.sh -p -r ${{ github.job }} ${{ matrix.variant }} -v ${{ matrix.variant }} \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh index dc526b3..8cbd876 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -13,69 +13,40 @@ WORKSPACE_DIR="$(dirname "$ROOT_DIR")" ## PUBLISH=false -while getopts "prv" option; do +JOB_VARIANT= +WORKFLOW_JOB_ID= +while getopts "pr:v:" option; do case $option in p) # publish PUBLISH=true ;; r) # report outcome to slack + WORKFLOW_JOB_ID=$OPTARG load_env_files "$WORKSPACE_DIR/development/common/SLACK_WEBHOOK_JOBS.enc.env" - trap 'slack_ci_report "$ROOT_DIR" "$JOB_ID $JOB_VARIANT" "$?" "$SLACK_WEBHOOK_JOBS"' EXIT + trap 'slack_ci_report "$ROOT_DIR" "$WORKFLOW_JOB_ID $JOB_VARIANT" "$?" "$SLACK_WEBHOOK_JOBS"' EXIT + ;; + v) # job variant + JOB_VARIANT=$OPTARG ;; *) ;; esac done -shift $((OPTIND-1)) -JOB_ID="$1" - -## Init workspace +## Init worksapce ## -init_job_infos "$ROOT_DIR" - -JOB=$(get_job_name) -VERSION=$(get_job_version) -KRAWLER_VERSION=$(get_job_krawler_version) -GIT_TAG=$(get_job_tag) - -if [[ -z "$GIT_TAG" ]]; then - echo "About to build ${JOB} development version based on krawler development version..." -else - echo "About to build ${JOB} v${VERSION} based on krawler ${KRAWLER_VERSION}..." -fi - -load_env_files "$WORKSPACE_DIR/development/common/kalisio_dockerhub.enc.env" "$WORKSPACE_DIR/development/common/SLACK_WEBHOOK_JOBS.enc.env" +load_env_files "$WORKSPACE_DIR/development/common/kalisio_dockerhub.enc.env" load_value_files "$WORKSPACE_DIR/development/common/KALISIO_DOCKERHUB_PASSWORD.enc.value" -## Build container +## Build job ## -JOB_VARIANT="$2" -IMAGE_NAME="kalisio/$JOB" -if [[ -z "$GIT_TAG" ]]; then - IMAGE_TAG="$JOB_VARIANT-latest" - KRAWLER_TAG=latest -else - IMAGE_TAG="$JOB_VARIANT-$VERSION" - KRAWLER_TAG=$KRAWLER_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 (job.Dockerfile.dockerignore) -DOCKER_BUILDKIT=1 docker build \ - --build-arg KRAWLER_TAG=$KRAWLER_TAG \ - -f dockerfile."$JOB_VARIANT" \ - -t "$IMAGE_NAME:$IMAGE_TAG" \ - "$WORKSPACE_DIR/$JOB" - -if [ "$PUBLISH" = true ]; then - docker push "$IMAGE_NAME:$IMAGE_TAG" -fi - -docker logout - -end_group "Building container ..." +build_job \ + "$ROOT_DIR" \ + "kalisio" \ + "$JOB_VARIANT" \ + "$KALISIO_DOCKERHUB_URL" \ + "$KALISIO_DOCKERHUB_USERNAME" \ + "$KALISIO_DOCKERHUB_PASSWORD" \ + "$PUBLISH" \ No newline at end of file diff --git a/scripts/setup_workspace.sh b/scripts/setup_workspace.sh index 8fe95a5..53c4912 100755 --- a/scripts/setup_workspace.sh +++ b/scripts/setup_workspace.sh @@ -8,12 +8,6 @@ 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 @@ -32,23 +26,13 @@ else done shift $((OPTIND-1)) WORKSPACE_DIR="$1" - - # NOTE: cloning krawler could be avoided if we could parse app_version from tag/branch name instead - # In this case, the kli would clone krawler - GIT_OPS="--recurse-submodules" - if [ -n "$WORKSPACE_TAG" ] || [ -n "$WORKSPACE_BRANCH" ]; then - GIT_OPS="$GIT_OPS --branch ${WORKSPACE_TAG:-$WORKSPACE_BRANCH}" - fi - git clone --depth 1 $GIT_OPS "$GITHUB_URL/kalisio/k-hubeau.git" "$WORKSPACE_DIR/k-hubeau" - DEVELOPMENT_REPO_URL="$GITHUB_URL/kalisio/development.git" - # unset KALISIO_DEVELOPMENT_DIR because we want kli to clone everything in $WORKSPACE_DIR - unset KALISIO_DEVELOPMENT_DIR + # Clone project in the workspace + git_shallow_clone "$GITHUB_URL/kalisio/k-hubeau.git" "$WORKSPACE_DIR/k-hubeau" "${WORKSPACE_TAG:-${WORKSPACE_BRANCH:-}}" fi -# clone development in $WORKSPACE_DIR -DEVELOPMENT_DIR="$WORKSPACE_DIR/development" -git clone --depth 1 "$DEVELOPMENT_REPO_URL" "$DEVELOPMENT_DIR" +setup_job_workspace "$WORKSPACE_DIR" "$DEVELOPMENT_REPO_URL" end_group "Setting up workspace ..." +