Jenkinsfile

Home   »   Jenkinsfile

pipeline {

    agent {
        node {
            label 'master'
        }
    }
    environment { 
        PATH = "/home/chamila/apictl:$PATH"
    }
    options {
        buildDiscarder logRotator( 
                    daysToKeepStr: '16', 
                    numToKeepStr: '10'
            )
    }

    stages {
        
        stage('Preparation') {
            steps{
                git branch: "master",
                url: 'https://github.com/chamilaadhi/cicd-poc-source-repo-apim.git'
            }
        }

        stage('Setup Environment for APICTL') {
            steps {
                sh '''
                #rm /var/lib/jenkins/workspace/gitconfig
                #touch /var/lib/jenkins/workspace/gitconfig
                apictl set --vcs-config-path /var/lib/jenkins/workspace/gitconfig
                apictl remove env dev
                apictl add env dev --apim https://xxxxx:9443 -k

                '''
            }
        }

        stage('Build api bundles') {
            steps {
                sh '''#!/bin/bash

                apictl login dev -u admin -p admin -k

                apis=$(apictl vcs status -e dev --format="{{ jsonPretty . }}" | jq -r '.API | .[] | .NickName')
                mkdir -p upload
                if [ -z "$apis" ]; 
                then 
                    echo "======== No API Changes detected =========="; 
                else 
                    echo "Updated APIs :"$apis
                    apiArray=($apis)
                    for i in "${apiArray[@]}"
                    do
                        echo "$i"
                        apictl bundle -s $i -d upload
                    done
                fi

                '''
            }
        }

        stage('Upload to repository') {
            steps {
                sh '''#!/bin/bash
                echo "Start uploading......"

                if [ -z "$(ls -A upload)" ]; then
                    echo "Nothing to upload"
                else
                    for file in upload/*; do
                        echo "Uploading "$file
                        curl -u repouser:xxxx -X PUT https://xxxxxx.jfrog.io/artifactory/myrepo/ -T $file
                    done
                fi

                rm -rf upload

                '''
            }
        }

        stage('Update local repo') {
            steps {
                sh '''#!/bin/bash
                idFull=$(cat vcs.yaml)
                arrId=(${idFull//: / })
                repoId=${arrId[1]}
                head=$(git rev-parse HEAD)

                rm /var/lib/jenkins/workspace/gitconfig
                echo "
repos:
  $repoId:
    environments:
       dev:
        lastAttemptedRev: $head" >> /var/lib/jenkins/workspace/gitconfig
                '''
            }
        }
    }   
}

Leave a Reply

Your email address will not be published. Required fields are marked *