From 91b59ce417b925fc67a96b098d4bb4fb2deac3cb Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 10 Oct 2022 19:31:43 -0400 Subject: [PATCH] Adding jenkinsfile and docker build scripts for maps (#30) Signed-off-by: Peter Zhu --- jenkins/maps-server-release.jenkinsfile | 118 ++++++++++++++++++ opensearch-maps-server/Dockerfile | 13 ++ .../build-image-single-arch.sh | 96 ++++++++++++++ 3 files changed, 227 insertions(+) create mode 100644 jenkins/maps-server-release.jenkinsfile create mode 100755 opensearch-maps-server/build-image-single-arch.sh diff --git a/jenkins/maps-server-release.jenkinsfile b/jenkins/maps-server-release.jenkinsfile new file mode 100644 index 00000000..6804bb9d --- /dev/null +++ b/jenkins/maps-server-release.jenkinsfile @@ -0,0 +1,118 @@ +lib = library(identifier: 'jenkins@1.0.0', retriever: modernSCM([ + $class: 'GitSCMSource', + remote: 'https://github.com/opensearch-project/opensearch-build-libraries.git', +])) + +pipeline { + options { + timeout(time: 30, unit: 'MINUTES') + throttleJobProperty( + categories: [], + limitOneJobWithMatchingParams: false, + maxConcurrentPerNode: 0, + maxConcurrentTotal: 1, + paramsToUseForLimit: '', + throttleEnabled: true, + throttleOption: 'project', + ) + } + agent { + docker { + label 'Jenkins-Agent-AL2-X64-C54xlarge-Docker-Host' + image 'opensearchstaging/ci-runner:ci-runner-centos7-opensearch-build-v2' + args '-e JAVA_HOME=/opt/java/openjdk-11' + alwaysPull true + } + } + parameters { + string( + name: 'GIT_REFERENCE', + description: 'Git branch, tag, commitid for reference to checkout commit of OpenSearch core before running the gradle check.', + defaultValue: 'main', + trim: true + ) + } + triggers { + GenericTrigger( + genericVariables: [ + [key: 'ref', value: '$.ref'], + ], + tokenCredentialId: 'jenkins-opensearch-maps-server-generic-webhook-token', + causeString: 'A commit was pushed on opensearch-project/maps repository main branch causing this workflow to run', + printContributedVariables: false, + printPostContent: false, + regexpFilterText: '$ref', + regexpFilterExpression: '^(maps-server-release)-[0-9]+$' + ) + } + environment { + REPO_URL="https://github.com/opensearch-project/maps" + USER_BUILD_CAUSE = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause') + TIMER_BUILD_CAUSE = currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause') + } + stages { + stage('Verifications and Descriptions') { + steps { + script { + def ref_final = "${GIT_REFERENCE}" + def ref_url = "${REPO_URL}/commit/${GIT_REFERENCE}" + if (env.USER_BUILD_CAUSE.equals('[]') && env.TIMER_BUILD_CAUSE.equals('[]')) { + ref_final = ${ref} + ref_url = "${REPO_URL}/releases/tag/${ref}" + println("Triggered by GitHub: ${ref_url}") + + currentBuild.description = """GitHub: ${ref_url}""" + } + else { + println("Triggered by User/Timer: ${ref_url}") + currentBuild.description = """User/Timer: ${ref_url}""" + } + + def MAPS_PRODUCT = "opensearch-maps-server" + def MAPS_VERSION = sh ( + script: "cat ${MAPS_PRODUCT}/package.json | grep version | cut -d: -f2 | grep -Eo '[0-9.]+'", + returnStdout: true + ).trim() + + echo("${MAPS_PRODUCT}: ${MAPS_VERSION}") + + dockerBuild: { + build job: 'docker-build', + parameters: [ + string(name: 'DOCKER_BUILD_GIT_REPOSITORY', value: "${REPO_URL}"), + string(name: 'DOCKER_BUILD_GIT_REPOSITORY_REFERENCE', value: "${ref_final}"), + string(name: 'DOCKER_BUILD_SCRIPT_WITH_COMMANDS', value: [ + 'id', + 'pwd', + "cd ${MAPS_PRODUCT}/", + 'ls -l', + [ + 'bash', + 'build-image-single-arch.sh', + "-v ${MAPS_VERSION}", + "-a x64", + "-f ./Dockerfile", + "-p ${MAPS_PRODUCT}" + ].join(' ') + ].join(' && ')), + ] + } + dockerCopy: { + build job: 'docker-copy', + parameters: [ + string(name: 'SOURCE_IMAGE_REGISTRY', value: 'opensearchstaging'), + string(name: 'SOURCE_IMAGE', value: "${MAPS_PRODUCT}:${MAPS_VERSION}"), + string(name: 'DESTINATION_IMAGE_REGISTRY', value: 'opensearchproject'), + string(name: 'DESTINATION_IMAGE', value: "${MAPS_PRODUCT}:${MAPS_VERSION}") + ] + } + } + } + post { + always { + cleanWs disableDeferredWipeout: true, deleteDirs: true + } + } + } + } +} diff --git a/opensearch-maps-server/Dockerfile b/opensearch-maps-server/Dockerfile index e079d2e9..5af69b59 100644 --- a/opensearch-maps-server/Dockerfile +++ b/opensearch-maps-server/Dockerfile @@ -31,6 +31,19 @@ COPY --from=stage_0 /src/oss_compliance . RUN curl -SL https://rpm.nodesource.com/setup_16.x | bash && \ yum install -y nodejs-16.17.1 python3 tar gzip && pip3 install requests==2.28.1 && yum clean all +ARG VERSION +ARG BUILD_DATE +ARG PRODUCT + +LABEL org.label-schema.schema-version="1.0" \ + org.label-schema.name="$PRODUCT" \ + org.label-schema.version="$VERSION" \ + org.label-schema.url="https://opensearch.org" \ + org.label-schema.vcs-url="https://github.com/opensearch-project/maps" \ + org.label-schema.license="Apache-2.0" \ + org.label-schema.vendor="OpenSearch Project" \ + org.label-schema.build-date="$BUILD_DATE" + COPY package*.json ./ RUN npm install COPY . . diff --git a/opensearch-maps-server/build-image-single-arch.sh b/opensearch-maps-server/build-image-single-arch.sh new file mode 100755 index 00000000..613a2dc0 --- /dev/null +++ b/opensearch-maps-server/build-image-single-arch.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Copyright OpenSearch Contributors +# SPDX-License-Identifier: Apache-2.0 +# +# The OpenSearch Contributors require contributions made to +# this file be licensed under the Apache-2.0 license or a +# compatible open source license. + +# This script is to automate the docker image creation process of OpenSearch and OpenSearch-Dashboards + +set -e + +function usage() { + echo "" + echo "This script is used to build the OpenSearch Docker image with single architecture (x64 or arm64). It prepares the files required by the Dockerfile in a temporary directory, then builds and tags the Docker image." + echo "--------------------------------------------------------------------------" + echo "Usage: $0 [args]" + echo "" + echo "Required arguments:" + echo -e "-v VERSION\tSpecify the OpenSearch version number that you are building, e.g. '1.0.0' or '1.0.0-beta1'. This will be used to label the Docker image. If you do not use the '-o' option then this tool will download a public OPENSEARCH release matching this version." + echo -e "-f DOCKERFILE\tSpecify the dockerfile full path, e.g. dockerfile/opensearch.al2.dockerfile." + echo -e "-p PRODUCT\tSpecify the product, e.g. opensearch or opensearch-dashboards, make sure this is the name of your config folder and the name of your .tgz defined in dockerfile." + echo -e "-a ARCHITECTURE\tSpecify one and only one architecture, e.g. x64 or arm64." + echo "" + echo "Optional arguments:" + echo -e "-h\t\tPrint this message." + echo "--------------------------------------------------------------------------" +} + +while getopts ":hv:f:p:a:" arg; do + case $arg in + h) + usage + exit 1 + ;; + v) + VERSION=$OPTARG + ;; + f) + DOCKERFILE=$OPTARG + ;; + p) + PRODUCT=$OPTARG + ;; + a) + ARCHITECTURE=$OPTARG + ;; + :) + echo "-${OPTARG} requires an argument" + usage + exit 1 + ;; + ?) + echo "Invalid option: -${OPTARG}" + exit 1 + ;; + esac +done + +# Validate the required parameters to present +if [ -z "$VERSION" ] || [ -z "$DOCKERFILE" ] || [ -z "$PRODUCT" ] || [ -z "$ARCHITECTURE" ]; then + echo "You must specify '-v VERSION', '-f DOCKERFILE', '-p PRODUCT', '-a ARCHITECTURE'" + usage + exit 1 +else + echo $VERSION $DOCKERFILE $PRODUCT $ARCHITECTURE +fi + +if [ "$PRODUCT" != "opensearch-maps-server" ] +then + echo "Enter either 'opensearch-maps-server' as product name for -p parameter" + exit 1 +else + PRODUCT_ALT=`echo $PRODUCT | sed 's@-@_@g'` + echo $PRODUCT $PRODUCT_ALT.yml +fi + +if [ "$ARCHITECTURE" != "x64" ] && [ "$ARCHITECTURE" != "arm64" ] +then + echo "We only support 'x64' and 'arm64' as architecture name for -a parameter" + exit 1 +fi + +if [ -z "$NOTES" ] +then + NOTES="None" +fi + + + +# Docker build +docker build --build-arg VERSION=$VERSION --build-arg BUILD_DATE=`date -u +%Y-%m-%dT%H:%M:%SZ` --build-arg PRODUCT=$PRODUCT -f $DOCKERFILE ./ -t opensearchstaging/$PRODUCT:$VERSION +docker tag opensearchstaging/$PRODUCT:$VERSION opensearchstaging/$PRODUCT:latest +docker push opensearchstaging/$PRODUCT:$VERSION +docker push opensearchstaging/$PRODUCT:latest