All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.bamboo.plugins.variables.stamper.BuildVariablesStamper Maven / Gradle / Ivy

Go to download

Simple Task plugin that places all Variables of the BuildContext in a properties file

There is a newer version: 2.0.0
Show newest version
/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.bamboo.plugins.variables.stamper;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.atlassian.bamboo.build.CustomPreBuildAction;
import com.atlassian.bamboo.plan.PlanKeys;
import com.atlassian.bamboo.repository.RepositoryException;
import com.atlassian.bamboo.utils.error.ErrorCollection;
import com.atlassian.bamboo.v2.build.BuildContext;
import com.atlassian.bamboo.v2.build.repository.RepositoryV2;
import com.atlassian.bamboo.variable.CustomVariableContext;
import com.atlassian.bamboo.ww2.actions.build.admin.create.BuildConfiguration;

/**
 * Simple {@link CustomPreBuildAction} implementation to store all the variables in a properties file
 * 
 * @author Mark Rekveld
 */
public class BuildVariablesStamper implements CustomPreBuildAction {

	static final String FILENAME = "bamboo-variables.properties";
	static final String DISABLE_STAMPER_KEY = "disable.variables.stamper";

	private static final Logger LOGGER = Logger.getLogger(BuildVariablesStamper.class);
	private static final String CREATED_BY_COMMENT = "Created by the Marvelution Bamboo Variables Stamper plugin";

	private BuildContext buildContext;
	private CustomVariableContext variableContext;

	/**
	 * {@inheritDoc}
	 */
	public void init(BuildContext buildContext) {
		this.buildContext = buildContext;
	}

	/**
	 * {@inheritDoc}
	 */
	public BuildContext call() throws InterruptedException, Exception {
		Properties variables = new Properties();
		variables.putAll(variableContext.getVariables(buildContext));
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("Saving following variables to " + FILENAME);
			for (Entry variable : variables.entrySet()) {
				LOGGER.debug("Variable key: " + variable.getKey() + " with value: " + variable.getValue());
			}
		}
		if (variables.containsKey(DISABLE_STAMPER_KEY)
			&& Boolean.parseBoolean(variables.getProperty(DISABLE_STAMPER_KEY))) {
			LOGGER.info("Skipping the Bamboo Variables Stamper as per '" + DISABLE_STAMPER_KEY + "' setting");
		} else {
			FileOutputStream outputStream = null;
			try {
				outputStream = new FileOutputStream(new File(getWorkingDirectory(), FILENAME));
				variables.store(outputStream, CREATED_BY_COMMENT);
				LOGGER.info("Stored the Build variables in " + FILENAME);
			} catch (Exception e) {
				LOGGER.error("Failed to store the Build variables in " + FILENAME);
				if (LOGGER.isDebugEnabled()) {
					LOGGER.error("Store failure exception: " + e.getMessage(), e);
				}
			}
		}
		return buildContext;
	}

	/**
	 * Internal method to get the Source root working directory
	 * 
	 * @return the Source root directory
	 * @throws RepositoryException in case of repository errors
	 */
	private File getWorkingDirectory() throws RepositoryException {
		RepositoryV2 repository = buildContext.getBuildDefinition().getRepositoryV2();
		return repository.getSourceCodeDirectory(PlanKeys.getPlanKey(buildContext.getPlanKey()));
	}

	/**
	 * {@inheritDoc}
	 */
	public ErrorCollection validate(BuildConfiguration buildConfiguration) {
		// This CustomPreBuildAction is not configurable so this method is not needed
		return null;
	}

	/**
	 * Setter for variableContext
	 * 
	 * @param variableContext the variableContext to set
	 */
	public void setVariableContext(CustomVariableContext variableContext) {
		this.variableContext = variableContext;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy