Hi, environment variables like "env.BRANCH_NAME" are not part of Jenkins core itself ... they are injected by additional plugins. So, yes, you have to use "Multibranch" project type ... because it's that project that will inject that environment variables from what I know.
Look at the section "Additional Environment Variables" of the documentation https://jenkins.io/doc/book/pipeline/multibranch/ On Tuesday, 31 March 2020 15:42:51 UTC+1, kurusetra wrote: > > Hi, > > I'm using freestyle project type, it should be "Multibranch" project type? > > Yes, i'm sure that my project is configured to checkout "master" branch. > > > git show-ref --tags -d # timeout=10 >> Checking out Revision a57c28b1efb0749a9b89b72b540557e226b116bd >> (origin/master) >> > git config core.sparsecheckout # timeout=10 >> > git checkout -f a57c28b1efb0749a9b89b72b540557e226b116bd # timeout=10 >> Commit message: "edit jenkinsfile" >> >> > Thanks > Kurusetra > > On Tue, Mar 31, 2020 at 9:37 PM Gianluca <gma...@gmail.com <javascript:>> > wrote: > >> Hi, >> we are using >> >> if (env.BRANCH_NAME == 'master') >> >> in our pipeline and works great... so, if you tried with the line above >> ... this means that there is an error on how the pipeline is configured and >> run. >> Are you using Multibranch project type? >> >> Are you sure your project is configured to checkout "master" branch? >> >> >> On Tuesday, 31 March 2020 15:26:36 UTC+1, kurusetra wrote: >>> >>> Hi, >>> >>> Already changed from 'origin/master' to 'master' but still no luck, >>> pipeline still build the 'dev' image dan deploy to namespace 'dev', even i >>> commit in 'master' branch... >>> Yes i'm using github.com for codes repository... >>> >>> Thanks >>> Kurusetra >>> >>> On Tuesday, March 31, 2020 at 9:00:40 PM UTC+7, Gianluca wrote: >>>> >>>> We use similar conditionals ... and I think your error is in the branch >>>> name: >>>> >>>> env.BRANCH_NAME == 'origin/master' >>>> >>>> that should be instead: >>>> >>>> env.BRANCH_NAME == 'master' >>>> >>>> Because, if I'm guessing right, you are using Git ... and then the >>>> first part is the remote name, not the branch: <remote_name>/<branch_name> >>>> :-) >>>> >>>> So, in your case the BRANCH_NAME is "master" and "origin" is the remote >>>> ... that you don't have to specify in Jenkins pipeline. >>>> >>>> >>>> On Tuesday, 31 March 2020 14:44:06 UTC+1, kurusetra wrote: >>>>> >>>>> Hi, >>>>> >>>>> I just created jenkinsfile for deploy sample application to my k8s, >>>>> but the deployment always build "dev" image and deploy to dev namespace >>>>> in >>>>> k8s. i tried to commit in master branch but still build "dev" image dan >>>>> deploy to "dev" namespace in k8s. >>>>> >>>>> Please advice... whats wrong with my pipeline, fyi... i create the job >>>>> in jenkins with "freestyle". >>>>> >>>>> pipeline { >>>>>> agent any >>>>>> environment { >>>>>> project_id = 'kurusetra' >>>>>> app_name = 'nginx-hello' >>>>>> } >>>>>> >>>>>> stages { >>>>>> >>>>>> stage('Clone Repository') { >>>>>> steps { >>>>>> checkout scm >>>>>> } >>>>>> } >>>>>> >>>>>> stage('Build Docker Images') { >>>>>> steps { >>>>>> script { >>>>>> if (env.BRANCH_NAME == 'origin/master') { >>>>>> myapp = >>>>>> docker.build("${project_id}/${app_name}-prod:${env.BUILD_NUMBER}") >>>>>> } else { >>>>>> myapp = >>>>>> docker.build("${project_id}/${app_name}-dev:${env.BUILD_NUMBER}") >>>>>> } >>>>>> } >>>>>> } >>>>>> } >>>>>> >>>>>> stage('Push Images') { >>>>>> steps { >>>>>> script { >>>>>> docker.withRegistry(' >>>>>> https://docker.xxx.com/kurusetra', 'harbor') { >>>>>> myapp.push("${env.BUILD_NUMBER}") >>>>>> myapp.push("latest") >>>>>> } >>>>>> } >>>>>> } >>>>>> } >>>>>> >>>>>> stage('Deploy to K8s') { >>>>>> steps { >>>>>> script { >>>>>> if (env.BRANCH_NAME == 'origin/master') { >>>>>> sh "sed -i >>>>>> 's/${app_name}:latest/${app_name}-prod:${env.BUILD_NUMBER}/g' >>>>>> deployment.yaml" >>>>>> sh 'kubectl apply -f ./deployment.yaml -n >>>>>> prod' >>>>>> } else { >>>>>> sh "sed -i >>>>>> 's/${app_name}:latest/${app_name}-dev:${env.BUILD_NUMBER}/g' >>>>>> deployment.yaml" >>>>>> sh 'kubectl apply -f ./deployment.yaml -n dev' >>>>>> } >>>>>> } >>>>>> } >>>>>> } >>>>>> } >>>>>> } >>>>>> >>>>> >>>>> >>>>> Thanks >>>>> Kurusetra >>>>> >>>> -- >> You received this message because you are subscribed to the Google Groups >> "Jenkins Users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to jenkins...@googlegroups.com <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/jenkinsci-users/e06e3e0a-4d00-48df-a356-b024c3649a87%40googlegroups.com >> >> <https://groups.google.com/d/msgid/jenkinsci-users/e06e3e0a-4d00-48df-a356-b024c3649a87%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "Jenkins Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/17441df2-ad4e-46f7-874f-57e65e75b49c%40googlegroups.com.