Creating environment specific pre-conditions for job steps in Anthill Pro
When creating deploy workflows in Anthill Pro, it’s best practice to try and keep the deploying in 1 job if possible and have your environment specific steps inside that job function using prerequisites to those jobs. For example, if you wanted to deploy configuration artifact sets as part of your deploy job and have the proper artifact sets deploy to the chosen environment, you would want to program a pre-condition step for each of those environments, then tag those jobs with these pre-condition steps.
For example, lets say we’re dealing with “dev”, “qa”, “stage” and “prod” artifact sets. We would make 4 pre-condition scripts:
is_dev:
Logic.and( JobStatus.allAncestorsIn( new JobStatusEnum[]{ JobStatusEnum.SUCCESS, JobStatusEnum.NOT_NEEDED } ), new Criteria() { public boolean matches(Object obj) throws Exception { return EnvironmentLookup.getCurrent().getShortName().equals("dev"); } } );
is_qa:
Logic.and( JobStatus.allAncestorsIn( new JobStatusEnum[]{ JobStatusEnum.SUCCESS, JobStatusEnum.NOT_NEEDED } ), new Criteria() { public boolean matches(Object obj) throws Exception { return EnvironmentLookup.getCurrent().getShortName().equals("qa"); } } );
is_stage:
Logic.and( JobStatus.allAncestorsIn( new JobStatusEnum[]{ JobStatusEnum.SUCCESS, JobStatusEnum.NOT_NEEDED } ), new Criteria() { public boolean matches(Object obj) throws Exception { return EnvironmentLookup.getCurrent().getShortName().equals("stage"); } } );
is_prod:
Logic.and( JobStatus.allAncestorsIn( new JobStatusEnum[]{ JobStatusEnum.SUCCESS, JobStatusEnum.NOT_NEEDED } ), new Criteria() { public boolean matches(Object obj) throws Exception { return EnvironmentLookup.getCurrent().getShortName().equals("prod"); } } );
Using this method requires that the environment shortname be named exactly “dev”, “qa”, “stage” and “prod”. This is defined in the environments section under the systems tab. It’s also especially useful to make your environment shortnames as statuses in your life cycle model, but that is another topic. Also, take note of the part of the script:
--> JobStatus.allAncestorsIn(new JobStatusEnum[] { JobStatusEnum.SUCCESS, JobStatusEnum.NOT_NEEDED })
This portion of the script ensures that not only are we executing in a specific environment, but all previous jobs executed successfully, or “not needed” meaning, a job in the past may not have executed because it was not needed.