1

I have been trying to set up a Jenkins pipeline to automate my deployment.The process that I am trying to implement is we will have four main branches(validate,develop,uat,master).When a merge request is opened in GitLab(From validate/* to develop, develop/* to uat etc),a job should kick off and deploy the changes to our specific sandboxes.Below is a testing pipeline that i have been trying to create:

echo "is gitlabSourceRepoSshUrl" ${gitlabSourceRepoSshUrl}
echo "is gitlabBranch" ${gitlabBranch}
echo "is gitlabTargetBranch" {$gitlabTargetBranch}
echo "is gitlabSourceBranch" {$gitlabSourceBranch}
echo "ls"

ls
git clone ${gitlabSourceRepoSshUrl}  .

#!/bin/bash


    if [[ ${gitlabSourceBranch} == "validate/"* ]]
    then
        export HOME=$WORKSPACE
        sfdx force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username niit.csingh@iberia.es.lightning --jwtkeyfile ${jwt_key_file} --setdefaultdevhubusername --instanceurl https://test.salesforce.com
        sfdx force:source:deploy -x manifest/package.xml -u niit.csingh@iberia.es.lightning --verbose
    else
    echo ${gitlabSourceBranch} 'is not equal to' ${SOURCE_TARGET_PATTERN}

    fi

Now my issues are:

  1. When the repository is cloned first,it is cloned with master branch (I suppose), so my workspace at all times has the code from master branch. So if my validate/* branch has a package.xml to deploy just one object,the job tries to pick the package.xml from my workspace (which is from master branch).How can my job pick the package.xml from the validate/* branch.
  2. Give me your feedback for the simple draft job that i have created.Will it be a cause of concern in future by not scaling up?

I hope I have communicated it well.If not, please ask me questions.I am new to Jenkins and GitLab and I am bit struggling but i will agree that it has been a learning curve.

2 Answers 2

3

On item 1), based on How to get just one file from another branch add this after your clone:

git checkout ${someBranch} -- package.xml 

On Item 2), I don't really follow what you are doing so can't comment.

(Our efforts on Jenkins pipelines are open-sourced here https://claimvantage.github.io/sfdx-jenkins-shared-library/; I don't suggest you use that, but you may find some items of interest in it.)

4
  • Thanks I fixed the first issue by including git checkout ${gitlabSourceBranch}.
    – Chetan
    Feb 5, 2020 at 15:36
  • And in the first first glance I could see the job is in groovy(I have not worked on groovy and we are working with strict deadlines) and it is trying to create a scratch org. We are not working with scratch org for now.
    – Chetan
    Feb 5, 2020 at 15:38
  • Hi @Chetan, Sure stick to the simplest approach that works for now. I do recommend scratch orgs and a pipeline build that builds every branch and pull request: makes our life simpler and safer where I work.
    – Keith C
    Feb 5, 2020 at 15:59
  • Hi @Keith C ,I agree working with scratch org would be the way forward, but the complexity of our Org and ergo the manpower involved in its untangling and creating the modular packages, would not be worth the benefit so we decided to stick with the happy soup.May be copying the shape of our org into scratch org feature, which is in pilot(I don't know since how long) could help us in future.
    – Chetan
    Feb 5, 2020 at 19:27
1

You can use Gitlab plugin to automatically generate jobs when there is a new PR ( https://github.com/jenkinsci/gitlab-plugin#pipeline-multibranch-jobs & https://github.com/jenkinsci/gitlab-plugin#pipeline-multibranch-jobs-1 ), so the appropriate branch will be fetched at build initialization

Then, in your JenkinsFile (I strongly suggest declarative pipeline), you can use configuration variables that will depend on the branch name and define them in .yml files.

ex:

Jenkinsfile:

...

environment {
    PACKAGE_XML = getEnvParam('PACKAGE_XML',false) 
}

...

def getEnvParam(String configVarName,Boolean mandatory) {
    // If Pull Request, use target branch config file
    def branchName = (env.GIT_BRANCH != null)?env.GIT_BRANCH:env.BRANCH_NAME
    if (branchName.startsWith('PR-'))
        branchName = env.CHANGE_TARGET

    // Get in branch config file
    def params = readYaml file: "./JenkinsFiles/jkf-config-${branchName}.yml"
    def returnValue = params[configVarName] 
    // If not found in config file, try in default config file
    if (returnValue == null ) {
        def paramsCommon = readYaml file: './JenkinsFiles/jkf-config-default.yml' 
        returnValue = paramsCommon[configVarName]
    }
    if (mandatory) {
        assert returnValue != null , "${configVarName} should be set in yml file !"
    }
    echo "Variable ${configVarName} : ${returnValue}"
    return returnValue 
}

jkf-config-default.yml

PACKAGE_XML : mydefaultvalue.xml

jkf-config-DevBranch.yml

PACKAGE_XML : myDevBranchValue.xml
3
  • Thanks Nicolas.Creating Jenkins file and deploying was a little complicated for me (as i just started with Jenkins)so I decided to go with bash script ,wrapping sfdx commands since I was little more comfortable with them.
    – Chetan
    Feb 5, 2020 at 19:30
  • From experience (but i can be wrong ^^), the use of declarative jenkinsfile (+ eventually shared library) is much more easier and maintainable, and you can of course use bash commands inside, you should try :) Feb 5, 2020 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.