Is there any way to get the build number in the environment or at build-time for a php instance?

63 Views Asked by At

I am trying to get the build number available in the php instance. I will use it as a type of "cache-buster" for the static assets (css, js). Is there any way to get it at run time? If not, is there a way to get it at build time in the postinstall script?

1

There are 1 best solutions below

1
On BEST ANSWER

I see a couple of ways you could do this.

  1. In /home/dotcloud/ the code directory is a symlink to the build version, so for example my code directory points to git-16ae997. In your postinstall script if you get the name of that directory and store it in a temp file, env variable, or maybe inject it into one of your config scripts, you could reference it from your program.

Putting this in your postinstall script will add the BUILD_VERSION variable to your .profile and be available to you from $BUILD_VERSION in a shell, or other languages. You might need to re-source the .profile after you make the change to be sure it gets set.

$ echo "export BUILD_VERSION=`readlink /home/dotcloud/code`" >> /home/dotcloud/.profile

$ echo $BUILD_VERSION

With PHP you should be able to get it at runtime with the following variable.

$_ENV["BUILD_VERSION"]
  1. Another approach I have used myself on other projects is to create .version file in the postinstall that just includes the timestamp of the build, and reference that file for the build_version and use it as your cache-buster. The problem with this approach is because it is created at postinstall if you scale above 1 instance, you will most likely end up with different timestamps for each instance, and I'm guessing this isn't what you want.

    $ date '+%s' > /home/dotcloud/.version

I would stick with #1 since it works when you scale, and it is more strait forward.

Hope that helps.