From 88ddef5c9c4081b012ce071869c1ff247f9c2218 Mon Sep 17 00:00:00 2001 From: Valentin Date: Wed, 18 Dec 2024 20:20:04 +0100 Subject: [PATCH] fix: app installation in sqa; add: docker build --- .sqa/config.yml | 2 +- .sqa/docker-compose.yml | 9 +++--- .sqa/dockerfile | 16 +++++----- Jenkinsfile | 68 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 79 insertions(+), 16 deletions(-) diff --git a/.sqa/config.yml b/.sqa/config.yml index 0bb53f0f..53b919f1 100644 --- a/.sqa/config.yml +++ b/.sqa/config.yml @@ -19,7 +19,7 @@ sqa_criteria: ai4life: container: testing commands: - - mkdir -p /srv/app/bandit + - mkdir -p ./bandit - bandit -r ai4life api -x tests -o ./bandit/index.html environment: diff --git a/.sqa/docker-compose.yml b/.sqa/docker-compose.yml index cd321d38..3e23cc1f 100644 --- a/.sqa/docker-compose.yml +++ b/.sqa/docker-compose.yml @@ -2,13 +2,14 @@ version: "3.6" services: testing: - image: "ai4oshub/ai4os-ai4life-loader:cicd" + #image: "ai4oshub/ai4os-ai4life-loader:cicd" container_name: "${BUILD_TAG}" hostname: "${BUILD_TAG}" working_dir: /srv/app - command: > - sh -c "pip3 install --no-cache-dir -e . && - sleep infinity" + build: + context: ./.sqa + args: + branch: ${GIT_BRANCH} volumes: - type: bind source: ./ diff --git a/.sqa/dockerfile b/.sqa/dockerfile index 95996645..2eadb78d 100644 --- a/.sqa/dockerfile +++ b/.sqa/dockerfile @@ -1,10 +1,10 @@ # Docker image for CI testing of the DEEPaaS API # Note there is probably no gpu in the CI server -#FROM ai4oshub/ai4os-ai4life-loader:cicd -## What user branch to clone [!] -#ARG branch=main -## install user app as python package -#RUN git clone -b $branch --depth 1 https://github.com/ai4os/ai4os-ai4life-loader.git app/ && \ -# cd app/ && pip3 install --no-cache-dir -e . && \ -# cd .. -#CMD ["sleep", "infinity"] +FROM ai4oshub/ai4os-ai4life-loader:cicd +# What user branch to clone [!] +ARG branch=main +# install user app as python package +RUN git clone -b $branch --depth 1 https://github.com/ai4os/ai4os-ai4life-loader.git app/ && \ + cd app/ && pip3 install --no-cache-dir -e . && \ + cd .. +CMD ["sleep", "infinity"] diff --git a/Jenkinsfile b/Jenkinsfile index 1319a16a..b6dc2ed8 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,14 +1,52 @@ @Library(['github.com/indigo-dc/jenkins-pipeline-library@release/2.1.1']) _ +// We choose to describe Docker image build directly in the Jenkinsfile instead of JePL2 +// since this gives better control on the building process +// (e.g. which branches are allowed for docker image build) + def projectConfig +// function to remove built images +def docker_clean() { + def dangling_images = sh( + returnStdout: true, + script: "docker images -f 'dangling=true' -q" + ) + if (dangling_images) { + sh(script: "docker rmi --force $dangling_images") + } +} + pipeline { - agent { - label 'docker' + agent any + + environment { + AI4OS_REGISTRY_CREDENTIALS = credentials('AIOS-registry-credentials') + APP_DOCKERFILE = "Dockerfile" } stages { - stage('SQA Model api testing') { + stage("Variable initialization") { + steps { + script { + checkout scm + withFolderProperties{ + env.DOCKER_REGISTRY = env.AI4OS_REGISTRY + env.DOCKER_REGISTRY_ORG = env.AI4OS_REGISTRY_REPOSITORY + env.DOCKER_REGISTRY_CREDENTIALS = env.AI4OS_REGISTRY_CREDENTIALS + } + // get docker image name from metadata.json + meta = readJSON file: "metadata.json" + image_name = meta["sources"]["docker_registry_repo"].split("/")[1] + // define tag based on branch + image_tag = "${env.BRANCH_NAME == 'main' ? 'latest' : env.BRANCH_NAME}" + env.DOCKER_REPO = env.DOCKER_REGISTRY_ORG + "/" + image_name + ":" + image_tag + env.DOCKER_REPO = env.DOCKER_REPO.toLowerCase() + println ("[DEBUG] Config for the Docker image build: $env.DOCKER_REPO, push to $env.DOCKER_REGISTRY") + } + } + } + stage('Application testing') { steps { script { projectConfig = pipelineConfig() @@ -21,6 +59,30 @@ pipeline { } } } + stage("Docker image building & delivery") { + when { + anyOf { + branch 'main' + branch 'release/*' + buildingTag() + } + } + steps { + script { + checkout scm + docker.withRegistry(env.DOCKER_REGISTRY, env.DOCKER_REGISTRY_CREDENTIALS){ + def app_image = docker.build(env.DOCKER_REPO, + "--no-cache --force-rm --build-arg branch=${env.BRANCH_NAME} -f ${env.APP_DOCKERFILE} .") + app_image.push() + } + } + } + post { + failure { + docker_clean() + } + } + } } post { // publish results and clean-up