diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000000000000000000000000000000000000..4f224d54bce5c6ff23b591bbdfd0a22c6e42f6f9 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,128 @@ +@Library('common-shared') _ + +pipeline { + agent { + kubernetes { + label 'kubedeploy-agent' + yaml ''' + apiVersion: v1 + kind: Pod + spec: + containers: + - name: kubectl + image: eclipsefdn/kubectl:1.9-alpine + command: + - cat + tty: true + ''' + } + } + + environment { + APP_NAME = 'marketplace-rest-api' + NAMESPACE = 'foundation-internal-webdev-apps' + IMAGE_NAME = 'eclipsefdn/marketplace-rest-api' + CONTAINER_NAME = 'app' + ENVIRONMENT = sh( + script: """ + if [ "${env.BRANCH_NAME}" = "master" ]; then + printf "production" + else + printf "${env.BRANCH_NAME}" + fi + """, + returnStdout: true + ) + TAG_NAME = sh( + script: """ + GIT_COMMIT_SHORT=\$(git rev-parse --short ${env.GIT_COMMIT}) + if [ "${env.ENVIRONMENT}" = "" ]; then + printf \${GIT_COMMIT_SHORT}-${env.BUILD_NUMBER} + else + printf ${env.ENVIRONMENT}-\${GIT_COMMIT_SHORT}-${env.BUILD_NUMBER} + fi + """, + returnStdout: true + ) + } + + options { + buildDiscarder(logRotator(numToKeepStr: '10')) + } + + triggers { + // build once a week to keep up with parents images updates + cron('H H * * H') + } + + stages { + stage('Build docker image') { + agent { + label 'docker-build' + } + steps { + sh ''' + ./mvnw package + docker build -f src/main/docker/Dockerfile.jvm --no-cache -t ${IMAGE_NAME}:${TAG_NAME} -t ${IMAGE_NAME}:latest . + ''' + } + } + + stage('Push docker image') { + agent { + label 'docker-build' + } + when { + anyOf { + environment name: 'ENVIRONMENT', value: 'production' + environment name: 'ENVIRONMENT', value: 'staging' + } + } + steps { + withDockerRegistry([credentialsId: '04264967-fea0-40c2-bf60-09af5aeba60f', url: 'https://index.docker.io/v1/']) { + sh ''' + docker push ${IMAGE_NAME}:${TAG_NAME} + docker push ${IMAGE_NAME}:latest + ''' + } + } + } + + stage('Deploy to cluster') { + when { + anyOf { + environment name: 'ENVIRONMENT', value: 'production' + environment name: 'ENVIRONMENT', value: 'staging' + } + } + steps { + container('kubectl') { + withKubeConfig([credentialsId: '1d8095ea-7e9d-4e94-b799-6dadddfdd18a', serverUrl: 'https://console-int.c1-ci.eclipse.org']) { + sh ''' + DEPLOYMENT="$(k8s getFirst deployment "${NAMESPACE}" "app=${APP_NAME},environment=${ENVIRONMENT}")" + if [[ $(echo "${DEPLOYMENT}" | jq -r 'length') -eq 0 ]]; then + echo "ERROR: Unable to find a deployment to patch matching 'app=${APP_NAME},environment=${ENVIRONMENT}' in namespace ${NAMESPACE}" + exit 1 + else + DEPLOYMENT_NAME="$(echo "${DEPLOYMENT}" | jq -r '.metadata.name')" + kubectl set image "deployment.v1.apps/${DEPLOYMENT_NAME}" -n "${NAMESPACE}" "${CONTAINER_NAME}=${IMAGE_NAME}:${TAG_NAME}" --record=true + if ! kubectl rollout status "deployment.v1.apps/${DEPLOYMENT_NAME}" -n "${NAMESPACE}"; then + # will fail if rollout does not succeed in less than .spec.progressDeadlineSeconds + kubectl rollout undo "deployment.v1.apps/${DEPLOYMENT_NAME}" -n "${NAMESPACE}" + exit 1 + fi + fi + ''' + } + } + } + } + } + + post { + always { + deleteDir() /* clean up workspace */ + sendNotifications currentBuild + } + } +} \ No newline at end of file diff --git a/src/k8s/production.yml b/src/k8s/production.yml new file mode 100644 index 0000000000000000000000000000000000000000..c26d30d3912dc5735581ca7abbc72bf00f86d13f --- /dev/null +++ b/src/k8s/production.yml @@ -0,0 +1,64 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: marketplace-rest-api + namespace: webapps +spec: + selector: + matchLabels: + app: marketplace-rest-api + replicas: 2 + template: + metadata: + labels: + app: marketplace-rest-api + environment: production + spec: + containers: + - name: app + image: eclipsefdn/marketplace-rest-api:latest + imagePullPolicy: Always + ports: + - containerPort: 8080 + resources: + limits: + cpu: '2' + memory: 2048Mi + requests: + cpu: 200m + memory: 2048Mi +--- +apiVersion: "v1" +kind: "Service" +metadata: + name: marketplace-rest-api + namespace: webapps +spec: + ports: + - name: "http" + port: 80 + protocol: "TCP" + targetPort: 8080 + selector: + app: marketplace-rest-api + environment: production +--- +apiVersion: "route.openshift.io/v1" +kind: "Route" +metadata: + name: marketplace-rest-api + namespace: webapps + annotations: + haproxy.router.openshift.io/timeout: 20s +spec: + host: "api.eclipse.org" + path: "/marketplace" + port: + targetPort: "http" + tls: + insecureEdgeTerminationPolicy: "Redirect" + termination: "edge" + to: + kind: "Service" + name: marketplace-rest-api + weight: 100 \ No newline at end of file diff --git a/src/k8s/staging.yml b/src/k8s/staging.yml new file mode 100644 index 0000000000000000000000000000000000000000..a67d1a4b9656158791aef0abd7d2b6fc1645df9a --- /dev/null +++ b/src/k8s/staging.yml @@ -0,0 +1,64 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: marketplace-rest-api + namespace: webapps +spec: + selector: + matchLabels: + app: marketplace-rest-api + replicas: 2 + template: + metadata: + labels: + app: marketplace-rest-api + environment: staging + spec: + containers: + - name: app + image: eclipsefdn/marketplace-rest-api:latest + imagePullPolicy: Always + ports: + - containerPort: 8080 + resources: + limits: + cpu: '2' + memory: 2048Mi + requests: + cpu: 200m + memory: 2048Mi +--- +apiVersion: "v1" +kind: "Service" +metadata: + name: marketplace-rest-api + namespace: webapps +spec: + ports: + - name: "http" + port: 80 + protocol: "TCP" + targetPort: 8080 + selector: + app: marketplace-rest-api + environment: staging +--- +apiVersion: "route.openshift.io/v1" +kind: "Route" +metadata: + name: marketplace-rest-api + namespace: webapps + annotations: + haproxy.router.openshift.io/timeout: 20s +spec: + host: "api-staging.eclipse.org" + path: "/marketplace" + port: + targetPort: "http" + tls: + insecureEdgeTerminationPolicy: "Redirect" + termination: "edge" + to: + kind: "Service" + name: marketplace-rest-api + weight: 100 \ No newline at end of file