Search code examples
jenkinsdeploymentphing

Compute property arithmetically


In short I am not able to make a computation based on a property within my build file.

Let's say I have:

<property name="basedir" value="${project.basedir}" /> <--Current value 73

How can I know the previous 3 builds (72,71 and 70) OR how can I compute these values(based on the basedir property)?

I have tried (ignore the addition):

  1. <property name="basedir" value="${project.basedir}+1" /> <--But it concats the value: 73+1

  2. <property name="basedir" value="${project.basedir+1}" /> <--But it is just wrong: build_${env.BUILD_NUMBER+1}

Scenario: Remove old releases (keep some releases in case of a rollback)

P.S: The duplicate link is invalid because this is a deployment via PHING not ANT


Solution

  • You could use an adhoc-task for this:

      <?xml version="1.0"?>
      <project default="main" phingVersion="2.11.0">
      <property name="basedir" value = "73"/>
      <adhoc-task name="increment"><![CDATA[
      class increment extends Task {
          private $value;
    
          function setvalue($value) {
              $this->value = $value;
          }
         function setProperty($property) {
             $this->property = $property;
          }
    
         function main() {
             $this->project->setProperty($this->property, ((int) $this->value + 1));
          }
      }
      ]]></adhoc-task>
      <target name="main">
      <echo>${basedir}</echo>
      <increment value="${basedir}" property="basedir"/>
      <echo>${basedir}</echo>
      </target>
      </project>