Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVOPS-203 add Jenkinsfile #97

Open
wants to merge 5 commits into
base: develoment
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
label = "${UUID.randomUUID().toString()}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can write: UUID.randomUUID().toString()
also the variable name is in correct as its label_suffix
but i think you should have a var label = "${git_project}-${label_suffix}" - as you do this twice in the code

BUILD_FOLDER = "/go"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you using absolute path instead of workspace subdir?
i know its a container but still

docker_user = "iguaziodocker"
docker_credentials = "iguazio-prod-docker-credentials"
git_project = "iguazio_api_examples"
git_project_user = "v3io"
git_deploy_user = "iguazio-prod-git-user"
git_deploy_user_token = "iguazio-prod-git-user-token"

properties([pipelineTriggers([[$class: 'PeriodicFolderTrigger', interval: '2m']])])
podTemplate(label: "${git_project}-${label}", yaml: """

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the yaml should be read from a file
as this is code & should not contain too much
for the variable inside you can have something like <git_project> and then do replace('<git_project>', var)
or even better the name can be configured through the params (not sure if yaml and params work together)

apiVersion: v1
kind: Pod
metadata:
name: "${git_project}-${label}"
labels:
jenkins/kube-default: "true"
app: "jenkins"
component: "agent"
spec:
shareProcessNamespace: true
containers:
- name: jnlp
image: jenkinsci/jnlp-slave
resources:
limits:
cpu: 1
memory: 2Gi
requests:
cpu: 1
memory: 2Gi
volumeMounts:
- name: go-shared
mountPath: /go

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mountPath value is a duplication of the BUILD_FOLDER

- name: docker-cmd
image: docker
command: [ "/bin/sh", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
volumeMounts:
- name: docker-sock
mountPath: /var/run
- name: go-shared
mountPath: /go

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mount path is duplication of the var

volumes:
- name: docker-sock
hostPath:
path: /var/run
- name: go-shared
emptyDir: {}
"""
) {
node("${git_project}-${label}") {
withCredentials([
usernamePassword(credentialsId: git_deploy_user, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME'),
string(credentialsId: git_deploy_user_token, variable: 'GIT_TOKEN')
]) {
def AUTO_TAG
def TAG_VERSION

stage('get tag data') {
container('jnlp') {
TAG_VERSION = sh(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its better to use common.shell in pipelinex library to avoid escaping errors

script: "echo ${TAG_NAME} | tr -d '\\n' | egrep '^v[\\.0-9]*.*\$' | sed 's/v//'",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember that you are using groovy - a full capable programming language so instead of doing stuff in shell, you can do:
trimmed = env.TAG_NAME.trim()
and then also the remove of the chars done with egrep can also be done in groovy, & sed as well

p.s from where TAG_NAME arrived to the env? i don't see it in params, if it is maybe you can just get the number and add the regex logic ...

returnStdout: true
).trim()

sh "curl -v -H \"Authorization: token ${GIT_TOKEN}\" https://api.github.com/repos/${git_project_user}/${git_project}/releases/tags/v${TAG_VERSION} > ~/tag_version"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.shell instead of sh
but also check if maybe there is a plugin that do some github integration and help with this
since i am not sure from where TAG_NAME is coming, then need to make sure that tag_VERSION is indeed a result of TAG_NAME & not to identical by accident or pure luck


AUTO_TAG = sh(
script: "cat ~/tag_version | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[\"body\"]'",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do all that in groovy no need sh, and then python

returnStdout: true
).trim()

PUBLISHED_BEFORE = sh(
script: "tag_published_at=\$(cat ~/tag_version | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[\"published_at\"]'); SECONDS=\$(expr \$(date +%s) - \$(date -d \"\$tag_published_at\" +%s)); expr \$SECONDS / 60 + 1",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again probably you can do that in groovy
also since its in one line not readable
also usually you don't want to much code/logic in the jenkinsfile, usually people create a python script and the jenkins call it with all needed arguments

returnStdout: true
).trim()

echo "$AUTO_TAG"
echo "$TAG_VERSION"
echo "$PUBLISHED_BEFORE"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its better to write echo env.AUTO_TAG
or even better echo """AUTO_TAG: ${env.AUTO_TAG}
TAG_VERSION: ${env. TAG_VERSION}
PUBLISHED_BEFORE: env. PUBLISHED_BEFORE"""

}
}

if ( TAG_VERSION && PUBLISHED_BEFORE < 240 ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what the 240 mean

stage('prepare sources') {
container('jnlp') {
sh """
cd ${BUILD_FOLDER}
git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${git_project_user}/${git_project}.git src/github.com/v3io/${git_project}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for git clone there is a plugin that do that for you

"""
}
}

stage('build in dood') {
container('docker-cmd') {
sh """
cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/golang/src/github.com/v3io/demos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of cd you can use the pipeline dir(path) {
}

docker build . --tag netops-demo-golang:latest --tag ${docker_user}/netops-demo-golang:${TAG_VERSION} --build-arg NUCLIO_BUILD_OFFLINE=true --build-arg NUCLIO_BUILD_IMAGE_HANDLER_DIR=github.com/v3io/demos

cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/py
docker build . --tag netops-demo-py:latest --tag ${docker_user}/netops-demo-py:${TAG_VERSION}
"""
}
}

stage('push to hub') {
container('docker-cmd') {
withDockerRegistry([credentialsId: docker_credentials, url: ""]) {
sh "docker push ${docker_user}/netops-demo-golang:${TAG_VERSION}"
sh "docker push ${docker_user}/netops-demo-py:${TAG_VERSION}"
}
}
}
} else {
stage('warning') {
if (PUBLISHED_BEFORE >= 240) {
echo "Tag too old, published before $PUBLISHED_BEFORE minutes."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure you want a warning that no one will see instead of failing the job
or atleast mark the stage skipped - you can use the conditional_Stage in pipelinex

} else if (AUTO_TAG.startsWith("Autorelease")) {
echo "Autorelease does not trigger this job."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again .. check it in the start iand maybe fail it, although i don't understand something in the logic, i guess i need some explanations

} else {
echo "${TAG_VERSION} is not release tag."
}
}
}
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n in eof is missing