Shared Libraries and Stashing

I am currently a jenkins user and is exploring the Teamcity.

In jenkins we have a concept of shared libraries, which basically extends a generic groovy code into different jenkins pipeline and avoid re-writing the same functionality in each jenkins file following the DRY (don't repeat yourself) , hide implementation complexity, keep pipelines short and easier to understand

Example:

There could be a repository having all the Groovy functions like:

Repo: http:://github.com/DEVOPS/Utilities.git (repo Utilities)

Sample Groovy Scipt ==>> GitUtils.groovy with below functions

public void setGitConfig(String userName, String email) {

sh "git config --global user.name ${userName}"

sh "git config --global user.mail ${email}"

}

public void gitPush(StringbranchName) {
  sh "git push origin ${branchName}"
  }
 
   

In jenkinsfile we can just call this function like below (of course we need to define config in jenkins for it to know the Repo url for Shared library and give it a name):

 

//name of shared library given in jenkins
@Library('utilities') _ pipeline { agent any stages { stage ('Example') { steps { // log.info 'Starting' script {
def gitutil = new GitUtils()
gitutils.setGitConfig("Ray", "Ray@rayban.com") } } } } }

 

 

And that's it anyone wanting the same function has to just include the library in jenkinsfile and use it in pipeline

 

Question:

Can we migrate over the same to Teamcity, if yes please suggest how it can be done.

We do not want to sped lot of time to re-writing

 

 

Question2

Jenkins also support Stashing and unstashing of workspace between stages, is the similar   concept present in teamcity?

pipeline{
    agent any
     stages{
        stage('Git checkout'){
            agent { label 'master' }
            steps{  
               stash includes: '/root/hello-world/*', name: 'mysrc'  
               }
}
        
        stage('maven build'){
            agent { label 'slave-1' }
            steps{
            
               unstash 'mysrc'
               sh label: '', script: 'mvn clean package'
            }
        }
    }
    
}
0
3 comments
Avatar
Fedor Rumyantsev

Hello!

Since the discussed functionality revolves around reusable scripts, I would like to bring up the notion of meta-runners and build templates. Former allows to encapsulate a build as a reusable build step (and you may use any runnable logic inside); latter allows to inherit build steps, settings and features from defined template(s).

The tricky part would be to run Groovy code you currently have in use. There is a third-party developed Groovy script runner, but it was not updated for quite a while, and I am not sure if it will run out of the box. However, you may use command line step to run Groovy scripts, for example, as described in this StackOverflow post. TeamCity also offers several other script runners out of the box - PowerShell, Python, C# and Kotlin.

Unfortunately, I do not think stashing is available as built-in option. However, you may run

git stash 

manually in a command line step (or run any other logic that will stash the workspace as needed) during the build.

0

Hi, what we are talking about here are not `git stash`

In Jenkins, the stash functionality is to save artifacts between steps and agent.

eg.
Step1: build project A on agent 1 -> Stash artifact A.
Step 2: On Agent 2 -> unstash Artifact A -> Execute Artifact A

 

Is it possible to pass artifact between build step in TC?

0

Hello!

In TeamCity, you can use artifact dependencies to pass artifacts between build configurations.

Please let me know if this helps.

0

Please sign in to leave a comment.