first commit

This commit is contained in:
Roberto Cirillo 2022-04-14 16:26:13 +02:00
parent e8ad467d19
commit a01f74c667
3 changed files with 125 additions and 55 deletions

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
/.idea

165
Jenkinsfile vendored
View File

@ -1,66 +1,137 @@
// REMEMBER TO FILL THE environment section with your values.
// the following filed should be filled: imagename, git_url
// REMEMBER to put your Dockerfile in the root folder of your project
// The related jenkinsjob template is here:
#!groovy
/**
* Deploy gCube components by Ansible
*
* Roberto Cirillo (ISTI-CNR)
*/
def agent_root_folder = '/var/lib/jenkins/.m2'
//locate the deploy file
def agent_deploy_file = // "${action_root}/${action_file}"
//locate the ansible home
def agent_ansible_home= '${agent_root_folder}/path/to/ansible';
//println "Querying ${actionURL}"
pipeline {
agent {
label 'docker'
}
environment {
imagename = "" //TO FILL WITH THE RIGHT VALUE (RepositoryName) e.g. d4science/RepositoryName
registryCredential = 'e348bfab-5580-4db6-b0e0-d854966bde08'
dockerImage = ''
git_url='' // SET HERE THE URL OF YOUR NEW GIT PROJECT
}
stages {
stage('Cloning Git') {
steps {
git([url: git_url, branch: 'master', credentialsId: '88b54962-1c0e-49cb-8155-22276860f346'])
}
}
stage('Building image') {
steps{
script {
dockerImage = docker.build imagename
agent {
label 'CD'
triggers {
cron('H */4 * * 1-5')
}
}
}
stage('Deploy Image') {
steps{
script {
docker.withRegistry( '', registryCredential ) {
dockerImage.push("$BUILD_NUMBER")
dockerImage.push('latest')
}
environment {
AGENT_ROOT_FOLDER = "${agent_root_folder}"
AGENT_DEPLOY_FILE = "${agent_deploy_file}"
}
parameters {
string(name: 'git_root',
defaultValue: 'https://code-repo.d4science.org/TestActions/',
description: 'The root URL of the repositories')
string(name: 'repo_list',
defaultValue: 'https://code-repo.d4science.org/gCubeCI/gCubeActions/raw/branch/master/repos/TestActions_all_sorted.txt',
description: 'The file with the list of repositories to update')
string(name: 'action_root',
defaultValue: 'https://code-repo.d4science.org/gCubeCI/gCubeActions/raw/branch/master/',
description: 'The root URL of the Bash fragment to execute.')
string(name: 'action_file',
defaultValue: '',
description: 'The relative path of the Bash fragment to execute.')
string(name: 'filter_with',
defaultValue: '',
description: 'If not empty, only actions including this filter in their output will be reported.')
}
stages {
stage('Initialize env') {
steps {
sh '''
date=`date`
echo "#Build ${PIPELINE_BUILD_NUMBER},," > $ACTION_REPORT
echo "#StartTime ${date},," >> $ACTION_REPORT
echo "Project,Repo,Result" >> $ACTION_REPORT
'''
}
}
stage('Deploy from system') {
when{
beforeAgent true
allOf{
triggeredBy 'TimeTrigger'
environment name: 'IS_CRON', value: 'True'
}
}
steps {
echo 'Deploy from system properly triggered'
}
}
stage('Deploy from system disabled') {
when{
beforeAgent true
allOf{
triggeredBy 'TimeTrigger'
environment name: 'IS_CRON', value: 'False'
}
}
steps {
echo 'Deploy from system disabled. Do nothing.'
}
}
stage('Deploy from Job') {
beforeAgent true
when{
environment name: 'IS_CRON', value: 'False'
}
steps {
echo 'Deploy from Job started'
}
}
stage('Planning new deploy') {
beforeAgent true
when{
environment name: 'IS_CRON', value: 'True'
}
steps {
echo 'New deploy of ${job} appended to the deploy file'
}
}
}
}
stage('Remove Unused docker image') {
steps{
sh "docker rmi $imagename:$BUILD_NUMBER"
sh "docker rmi $imagename:latest"
}
}
}
// post-build actions
// post-build actions
post {
always {
script {
sh '''
echo ' jobs currently appended:'
cat ./${ACTION_DEPLOY_FILE}.csv
'''
}
}
success {
echo 'The docker pipeline worked!'
emailext to: 'jenkinsbuilds@d4science.org',
subject: "[Jenkins DockerPipeline D4S] build ${currentBuild.fullDisplayName} worked",
body: "Build time: ${currentBuild.durationString}. See ${env.BUILD_URL}"
echo 'The deploy pipeline worked!'
emailext attachmentsPattern: "**/${ACTION_DEPLOY_FILE}.csv",
to: 'roberto.cirillo@isti.cnr.it',
subject: "Deploy report (build #${PIPELINE_BUILD_NUMBER})",
body: "${currentBuild.fullDisplayName}. Build time: ${currentBuild.durationString}. See ${env.BUILD_URL}. "
}
failure {
echo 'The docker pipeline has failed'
echo 'The deploy pipeline has failed'
emailext attachLog: true,
to: 'jenkinsbuilds@d4science.org',
subject: "[Jenkins DockerPipeline D4S] build ${currentBuild.fullDisplayName} failed for image ${imagename}",
to: 'roberto.cirillo@isti.cnr.it',
subject: "[Jenkins deploy D4S] deploy ${currentBuild.fullDisplayName} failed",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
}

View File

@ -1,12 +1,6 @@
# Pipeline-Docker-Template
This is a template useful for building docker images and push to dockerhub.
There is a jenkinsjob template on our jenkins with the same name.
# Pipeline-gCubeDeploy
This is a test: try to implements a deply pipeline integrated with ansible.
## USAGE TIPS:
* REMEMBER TO FILL the jenkinsfile environment section with your values. The following field should be properly filled: imagename, git_url
* REMEMBER TO PUT your Dockerfile in the root folder of your new project
* You can find the related jenkinsjob template here: https://jenkins.d4science.org/job/Pipeline-Docker-Template/