Search in sources :

Example 1 with RunBuildException

use of jetbrains.buildServer.RunBuildException in project teamcity-powershell by JetBrains.

the class BasePowerShellService method makeProgramCommandLine.

@NotNull
@Override
public ProgramCommandLine makeProgramCommandLine() throws RunBuildException {
    final PowerShellInfo info = selectTool();
    final BuildProgressLogger buildLogger = getBuild().getBuildLogger();
    final String psExecutable = info.getExecutablePath();
    final String workDir = getWorkingDirectory().getPath();
    final PowerShellExecutionMode mode = PowerShellExecutionMode.fromString(getRunnerParameters().get(RUNNER_EXECUTION_MODE));
    buildLogger.message("PowerShell Executable: " + psExecutable);
    buildLogger.message("Working directory: " + workDir);
    if (PowerShellExecutionMode.STDIN == mode) {
        return getStdInCommandLine(info, getEnv(info), workDir, generateCommand(info));
    } else if (PowerShellExecutionMode.PS1 == mode) {
        return getFileCommandLine(info, getEnv(info), workDir, generateArguments(info));
    } else {
        throw new RunBuildException("Could not select PowerShell tool for mode [" + mode + "]");
    }
}
Also used : BuildProgressLogger(jetbrains.buildServer.agent.BuildProgressLogger) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) RunBuildException(jetbrains.buildServer.RunBuildException) PowerShellExecutionMode(jetbrains.buildServer.powershell.common.PowerShellExecutionMode) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with RunBuildException

use of jetbrains.buildServer.RunBuildException in project teamcity-powershell by JetBrains.

the class BasePowerShellService method selectTool.

private PowerShellInfo selectTool() throws RunBuildException {
    final BuildProgressLogger buildLogger = getBuild().getBuildLogger();
    PowerShellInfo result;
    if (getRunnerContext().isVirtualContext()) {
        if (SystemInfo.isWindows) {
            throw new RunBuildException("PowerShell is not supported on windows containers");
        }
        buildLogger.logMessage(internalize(createTextMessage("PowerShell is running in virtual agent context")));
        result = VirtualPowerShellSupport.getVirtualPowerShell();
    } else {
        buildLogger.logMessage(internalize(createTextMessage("PowerShell running in non-virtual agent context")));
        final PowerShellBitness bit = PowerShellBitness.fromString(getRunnerParameters().get(RUNNER_BITNESS));
        final String version = getRunnerParameters().get(RUNNER_MIN_VERSION);
        final PowerShellEdition edition = PowerShellEdition.fromString(getRunnerParameters().get(RUNNER_EDITION));
        result = myInfoProvider.selectTool(bit, version, edition);
        if (result == null) {
            throw new RunBuildException("Could not select PowerShell for given bitness " + (bit == null ? "<Auto>" : bit.getDisplayName() + " and version " + (version == null ? "<Any>" : version)));
        }
    }
    return result;
}
Also used : PowerShellEdition(jetbrains.buildServer.powershell.common.PowerShellEdition) BuildProgressLogger(jetbrains.buildServer.agent.BuildProgressLogger) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) RunBuildException(jetbrains.buildServer.RunBuildException) PowerShellBitness(jetbrains.buildServer.powershell.common.PowerShellBitness)

Example 3 with RunBuildException

use of jetbrains.buildServer.RunBuildException in project teamcity-powershell by JetBrains.

the class PowerShellServiceUnix method generateNixScriptFile.

@NotNull
private File generateNixScriptFile(@NotNull final String argumentsToGenerate) throws RunBuildException {
    final File script;
    try {
        script = FileUtil.createTempFile(getBuildTempDirectory(), "powershell_gen_" + System.currentTimeMillis(), ".sh", true);
        myFilesToRemove.add(script);
        FileUtil.writeFileAndReportErrors(script, argumentsToGenerate);
    } catch (IOException e) {
        throw new RunBuildException("Failed to generate .sh wrapper file");
    }
    return script;
}
Also used : RunBuildException(jetbrains.buildServer.RunBuildException) IOException(java.io.IOException) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with RunBuildException

use of jetbrains.buildServer.RunBuildException in project teamcity-powershell by JetBrains.

the class ScriptGenerator method writeToTempFile.

@NotNull
private File writeToTempFile(@NotNull final File buildTempDir, @NotNull final String text, @NotNull final Map<String, String> runnerParameters) throws RunBuildException {
    Closeable handle = null;
    File file;
    try {
        file = FileUtil.createTempFile(buildTempDir, "powershell", ".ps1", true);
        OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
        handle = w;
        if (PowerShellExecutionMode.PS1 == PowerShellExecutionMode.fromString(runnerParameters.get(RUNNER_EXECUTION_MODE))) {
            w.write(BOM);
        }
        w.write(text);
        return file;
    } catch (IOException e) {
        LOG.error("Error occurred while processing file for PowerShell script", e);
        throw new RunBuildException("Failed to generate temporary resulting PowerShell script due to exception", e);
    } finally {
        FileUtil.close(handle);
    }
}
Also used : RunBuildException(jetbrains.buildServer.RunBuildException) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with RunBuildException

use of jetbrains.buildServer.RunBuildException in project teamcity-powershell by JetBrains.

the class ScriptGenerator method generateScript.

/**
 * Gets script source file either from parameter by dumping it to temp file
 * or from file, specified in parameters
 *
 * @param runnerParameters runner parameters
 * @param buildCheckoutDir checkout directory
 * @param buildTempDir temp directory
 * @return if {@code PowerShellScriptMode.FILE} is used - file, that corresponds to {@code RUNNER_SCRIPT_FILE} param,
 * if {@code PowerShellScriptMode.CODE} is used - temp file, containing code from {@code RUNNER_SCRIPT_CODE} param
 *
 * @throws RunBuildException if value if {@code RUNNER_SCRIPT_CODE} param is empty, or file handling error occurred
 */
@NotNull
public File generateScript(@NotNull final Map<String, String> runnerParameters, @NotNull final File buildCheckoutDir, @NotNull final File buildTempDir) throws RunBuildException {
    final PowerShellScriptMode scriptMode = PowerShellScriptMode.fromString(runnerParameters.get(RUNNER_SCRIPT_MODE));
    File scriptFile;
    if (PowerShellScriptMode.FILE == scriptMode) {
        scriptFile = FileUtil.resolvePath(buildCheckoutDir, runnerParameters.get(RUNNER_SCRIPT_FILE));
    } else {
        String sourceScript = runnerParameters.get(RUNNER_SCRIPT_CODE);
        if (isEmptyOrSpaces(sourceScript)) {
            throw new RunBuildException("Empty build script");
        }
        sourceScript = convertLineSeparators(sourceScript, "\r\n");
        /*if (PowerShellExecutionMode.STDIN.equals(PowerShellExecutionMode.fromString(runnerParameters.get(RUNNER_EXECUTION_MODE)))) {
        //some newlines are necessary to workaround -Command - issues, like TW-19771
        sourceScript = "  \r\n  \r\n  \r\n" + sourceScript + "\r\n  \r\n   \r\n   ";
      }*/
        scriptFile = writeToTempFile(buildTempDir, sourceScript, runnerParameters);
    }
    if (!scriptFile.isFile()) {
        throw new RunBuildException("Cannot find PowerShell script by path specified in build configuration settings: " + scriptFile.getAbsolutePath() + " (absolute path on agent). Please check that the specified path is correct.");
    }
    return scriptFile;
}
Also used : PowerShellScriptMode(jetbrains.buildServer.powershell.common.PowerShellScriptMode) RunBuildException(jetbrains.buildServer.RunBuildException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RunBuildException (jetbrains.buildServer.RunBuildException)7 NotNull (org.jetbrains.annotations.NotNull)6 File (java.io.File)2 IOException (java.io.IOException)2 BuildProgressLogger (jetbrains.buildServer.agent.BuildProgressLogger)2 PowerShellInfo (jetbrains.buildServer.powershell.agent.detect.PowerShellInfo)2 PowerShellExecutionMode (jetbrains.buildServer.powershell.common.PowerShellExecutionMode)2 ArrayList (java.util.ArrayList)1 PowerShellBitness (jetbrains.buildServer.powershell.common.PowerShellBitness)1 PowerShellEdition (jetbrains.buildServer.powershell.common.PowerShellEdition)1 PowerShellScriptMode (jetbrains.buildServer.powershell.common.PowerShellScriptMode)1