release automation script

本文介绍了一个用于自动化构建项目的shell脚本示例。该脚本根据用户选择的项目(Project1或Project2),设置不同的JAVA_HOME环境变量,并通过CVS进行身份验证。此流程简化了项目的构建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/sh

if [ -z $PROJECT]; then
        PS3="Please choose:"
        PROJECTS="Project1 Project2"
        echo "Choose which project you want to build from the list below:"
        select PROJECT in $PROJECTS;
        do
                break
        done
fi

if [ -z $PROJECT ]; then
        echo "Error in select project!"
        exit
fi

echo "Release $PROJECT project now..."
case $PROJECT in
        Project1)
                export JAVA_HOME=/opt/jdk1.6.0_24
                xxx=aaa
                break;;
        Project2)
               export JAVA_HOME=/opt/jdk1.5.0_12
                xxx=bbb
                break;;
esac

echo "JAVA_HOME="$JAVA_HOME

read -p "Please enter your cvs user name: " yn
USER=`echo $yn|sed 's/ //g'`
if [ "$USER" = "" ]
then
        echo "Empty user name!"
        exit 1
fi

stty -echo
read -p "Please enter your cvs password:" PASS
stty echo
echo ""

 Release project is error-prone, so we should use scripts to make this progress automation. Here's an exmple for it (Only a part, not including mvn release process).

 

Tools can make our life easy!

 

PS C:\Users\Administrator> # CurlTools.psm1 >> # ============== >> # 完整修复的模块代码 >> >> # 模块初始化块 >> $script:Config = $null >> $script:CurlPath = $null >> $script:ConfigDir = $null >> $script:ConfigPath = $null >> $script:ModuleInitialized = $false >> >> # 检测操作系统 >> function Get-Platform { >> if ($PSVersionTable.PSEdition -eq "Core") { >> if ($IsWindows) { "Windows" } >> elseif ($IsLinux) { "Linux" } >> elseif ($IsMacOS) { "macOS" } >> } else { >> if ($env:OS -like "Windows*") { "Windows" } >> elseif (Test-Path "/etc/os-release") { "Linux" } >> else { "macOS" } >> } >> } >> >> # 获取配置目录 >> function Get-ConfigDir { >> $platform = Get-Platform >> switch ($platform) { >> "Linux" { "$HOME/.config/curltools" } >> "macOS" { "$HOME/Library/Application Support/CurlTools" } >> default { "$env:APPDATA\CurlTools" } >> } >> } >> >> # 核心初始化函数 >> function Initialize-Module { >> try { >> # 设置配置目录 >> $script:ConfigDir = Get-ConfigDir >> if (-not (Test-Path $script:ConfigDir)) { >> New-Item -ItemType Directory -Path $script:ConfigDir -Force | Out-Null >> } >> $script:ConfigPath = Join-Path $script:ConfigDir "config.json" >> >> # 加载或创建配置 >> if (Test-Path $script:ConfigPath) { >> $script:Config = Get-Content $script:ConfigPath | ConvertFrom-Json >> } >> >> if (-not $script:Config) { >> $script:Config = [PSCustomObject]@{ >> CurlPath = $null >> LastUpdate = (Get-Date).ToString("o") >> AutoUpdate = $true >> } >> } >> >> # 查找或安装curl >> if (-not $script:Config.CurlPath -or -not (Test-Path $script:Config.CurlPath)) { >> $script:Config.CurlPath = Find-CurlPath >> } >> >> # 保存配置 >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> $script:CurlPath = $script:Config.CurlPath >> $script:ModuleInitialized = $true >> >> return $true >> } catch { >> Write-Warning "模块初始化失败: $_" >> return $false >> } >> } >> >> # 查找curl路径 >> function Find-CurlPath { >> $platform = Get-Platform >> $exeName = if ($platform -eq "Windows") { "curl.exe" } else { "curl" } >> >> # 检查系统PATH >> $pathCurl = Get-Command $exeName -ErrorAction SilentlyContinue >> if ($pathCurl) { >> return $pathCurl.Source | Split-Path >> } >> >> # 平台特定路径 >> $searchPaths = switch ($platform) { >> "Windows" { >> @( >> "C:\Windows\System32", >> "C:\Program Files\curl\bin", >> "${env:ProgramFiles}\Git\usr\bin" >> ) >> } >> "Linux" { @("/usr/bin", "/usr/local/bin") } >> "macOS" { @("/usr/local/bin", "/opt/homebrew/bin") } >> } >> >> foreach ($path in $searchPaths) { >> $fullPath = Join-Path $path $exeName >> if (Test-Path $fullPath) { >> return $path >> } >> } >> >> # 终极方案:自动安装 >> return Install-Curl >> } >> >> # curl安装函数 >> function Install-Curl { >> $platform = Get-Platform >> try { >> if ($platform -eq "Windows") { >> $tempDir = [System.IO.Path]::GetTempPath() >> $curlZip = Join-Path $tempDir "curl.zip" >> $curlUrl = "https://curl.se/windows/dl-8.8.0_5/curl-8.8.0_5-win64-mingw.zip" >> >> # 使用安全下载函数 >> Invoke-SecureDownload -Url $curlUrl -OutputPath $curlZip >> >> Expand-Archive $curlZip -DestinationPath $tempDir -Force >> return Join-Path $tempDir "curl-8.8.0_5-win64-mingw\bin" >> } >> else { >> if ($platform -eq "Linux") { >> if (Get-Command apt -ErrorAction SilentlyContinue) { >> Start-Process -Wait -FilePath "sudo" -ArgumentList "apt", "update" >> Start-Process -Wait -FilePath "sudo" -ArgumentList "apt", "install", "-y", "curl" >> } elseif (Get-Command yum -ErrorAction SilentlyContinue) { >> Start-Process -Wait -FilePath "sudo" -ArgumentList "yum", "install", "-y", "curl" >> } >> return "/usr/bin" >> } >> else { >> if (-not (Get-Command brew -ErrorAction SilentlyContinue)) { >> # 使用安全下载安装Homebrew >> $installScript = Join-Path $env:TEMP "brew_install.sh" >> Invoke-SecureDownload -Url "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh" -OutputPath $installScript >> & "/bin/bash" $installScript >> } >> Start-Process -Wait -FilePath "brew" -ArgumentList "install", "curl" >> return "/usr/local/bin" >> } >> } >> } catch { >> throw "无法自动安装curl: $_" >> } >> } >> >> # ========== 公共函数 ========== >> function Get-CurlPath { >> Ensure-ModuleInitialized >> return $script:CurlPath >> } >> >> function Set-CurlPath { >> param( >> [Parameter(Mandatory=$true)] >> [ValidateScript({ >> # 允许设置不存在的路径(创建前) >> if (-not (Test-Path $_)) { >> Write-Warning "路径不存在,将在设置后创建: $_" >> $true >> } else { >> $true >> } >> })] >> [string]$Path >> ) >> >> Ensure-ModuleInitialized >> >> # 确保路径存在 >> if (-not (Test-Path $Path)) { >> New-Item -ItemType Directory -Path $Path -Force | Out-Null >> } >> >> $script:CurlPath = $Path >> $script:Config.CurlPath = $Path >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> Write-Host "已设置curl路径: $Path" -ForegroundColor Green >> } >> >> function Get-CurlVersion { >> Ensure-ModuleInitialized >> $exe = if ((Get-Platform) -eq "Windows") { "curl.exe" } else { "curl" } >> $curlExe = Join-Path $script:CurlPath $exe >> & $curlExe --version | Select-Object -First 1 >> } >> >> function Invoke-SecureDownload { >> param( >> [Parameter(Mandatory=$true)] >> [string]$Url, >> >> [Parameter(Mandatory=$true)] >> [string]$OutputPath, >> >> [string]$ExpectedHash, >> [string]$HashAlgorithm = "SHA256" >> ) >> >> # 添加重试机制 >> $maxRetries = 3 >> $retryCount = 0 >> $success = $false >> >> do { >> try { >> Write-Verbose "下载文件: $Url (尝试 #$($retryCount + 1))" >> $ProgressPreference = 'SilentlyContinue' >> >> # 创建输出目录(如果不存在) >> $outputDir = [System.IO.Path]::GetDirectoryName($OutputPath) >> if (-not [string]::IsNullOrWhiteSpace($outputDir) -and -not (Test-Path $outputDir)) { >> New-Item -ItemType Directory -Path $outputDir -Force | Out-Null >> } >> >> # 下载文件 >> Invoke-WebRequest -Uri $Url -OutFile $OutputPath -UseBasicParsing -ErrorAction Stop >> >> # 验证文件是否成功下载 >> if (-not (Test-Path $OutputPath)) { >> throw "文件下载后未找到: $OutputPath" >> } >> >> # 验证哈希(如果提供) >> if (-not [string]::IsNullOrWhiteSpace($ExpectedHash)) { >> $actualHash = (Get-FileHash -Path $OutputPath -Algorithm $HashAlgorithm).Hash >> if ($actualHash -ne $ExpectedHash) { >> Remove-Item -Path $OutputPath -Force >> throw "文件哈希不匹配! 期望: $ExpectedHash, 实际: $actualHash" >> } >> } >> >> $success = $true >> return $OutputPath >> } >> catch { >> $retryCount++ >> if ($retryCount -ge $maxRetries) { >> # 清理部分下载的文件 >> if (Test-Path $OutputPath) { >> Remove-Item -Path $OutputPath -Force -ErrorAction SilentlyContinue >> } >> throw "下载失败: $($_.Exception.Message)" >> } >> >> # 随机延迟后重试 >> $retryDelay = Get-Random -Minimum 1 -Maximum 5 >> Write-Warning "下载失败,$retryDelay 秒后重试: $($_.Exception.Message)" >> Start-Sleep -Seconds $retryDelay >> } >> } while (-not $success) >> } >> >> # ========== 辅助函数 ========== >> function Ensure-ModuleInitialized { >> if (-not $script:ModuleInitialized) { >> if (-not (Initialize-Module)) { >> throw "模块未正确初始化" >> } >> } >> } >> >> # ========== 模块初始化 ========== >> # 尝试初始化但不强制 >> try { >> Initialize-Module | Out-Null >> Write-Verbose "CurlTools 模块初始化成功" >> } catch { >> Write-Warning "模块初始化错误: $_" >> } >> >> # ========== 函数导出 ========== >> # 导出公共函数 >> Export-ModuleMember -Function Get-CurlPath, Set-CurlPath, Get-CurlVersion, Invoke-SecureDownload >> Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 行:278 字符: 1 + Export-ModuleMember -Function Get-CurlPath, Set-CurlPath, Get-CurlVer ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand PS C:\Users\Administrator> $moduleDir = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\CurlTools" >> New-Item -ItemType Directory -Path $moduleDir -Force | Out-Null >> >> # 保存修复后的模块代码 >> $fixedModuleCode = @" >> <上面完整的修复代码> >> "@ >> >> Set-Content -Path "$moduleDir\CurlTools.psm1" -Value $fixedModuleCode -Force >> PS C:\Users\Administrator> $manifest = @" >> @{ >> ModuleVersion = '1.4.0' >> RootModule = 'CurlTools.psm1' >> FunctionsToExport = @( >> 'Get-CurlPath', >> 'Set-CurlPath', >> 'Get-CurlVersion', >> 'Invoke-SecureDownload' >> ) >> CompatiblePSEditions = @('Desktop', 'Core') >> GUID = 'c0d1b1e1-1a2b-4c3d-8e4f-9a0b1c2d3e4f' >> Author = 'Your Name' >> Description = 'Powerful curl tools for PowerShell' >> } >> "@ >> >> Set-Content -Path "$moduleDir\CurlTools.psd1" -Value $manifest -Force >> PS C:\Users\Administrator> function Update-CurlTools { >> param([switch]$Force) >> >> try { >> $updateUrl = "https://api.github.com/repos/yourname/curltools/releases/latest" >> $response = Invoke-RestMethod $updateUrl >> $latestVersion = [Version]$response.tag_name >> $currentVersion = [Version](Get-Module CurlTools).Version >> >> if ($latestVersion -gt $currentVersion -or $Force) { >> Write-Host "正在更新模块到版本 $latestVersion" >> $installScript = Join-Path $env:TEMP "install.ps1" >> >> # 安全下载安装脚本 >> Invoke-SecureDownload -Url $response.assets[0].browser_download_url ` >> -OutputPath $installScript >> >> # 执行更新 >> & $installScript >> >> # 重新加载模块 >> Remove-Module CurlTools -ErrorAction SilentlyContinue >> Import-Module CurlTools -Force >> >> Write-Host "模块已成功更新到版本 $latestVersion" -ForegroundColor Green >> } >> else { >> Write-Host "当前已是最新版本 ($currentVersion)" -ForegroundColor Cyan >> } >> } >> catch { >> Write-Error "更新失败: $_" >> } >> } >> PS C:\Users\Administrator> # 强制重新加载模块 >> Remove-Module CurlTools -ErrorAction SilentlyContinue >> Import-Module CurlTools -Force -Verbose >> >> # 验证函数是否可用 >> Get-Command -Module CurlTools >> >> # 测试获取curl路径 >> $curlPath = Get-CurlPath >> Write-Host "Curl 路径: $curlPath" -ForegroundColor Green >> >> # 测试设置新路径(会自动创建) >> $newPath = "C:\Custom\Curl\Path" >> Set-CurlPath -Path $newPath >> >> # 验证新路径设置成功 >> if ((Get-CurlPath) -eq $newPath) { >> Write-Host "路径设置成功: $newPath" -ForegroundColor Green >> } >> >> # 测试获取curl版本 >> $curlVersion = Get-CurlVersion >> Write-Host "Curl 版本: $curlVersion" -ForegroundColor Green >> >> # 测试安全下载 >> $testUrl = "https://raw.githubusercontent.com/PowerShell/PowerShell/master/README.md" >> $outputPath = "$env:TEMP\PowerShell_README.md" >> $downloadedFile = Invoke-SecureDownload -Url $testUrl -OutputPath $outputPath >> Write-Host "下载成功: $downloadedFile" -ForegroundColor Green >> >> # 验证文件下载成功 >> if (Test-Path $outputPath) { >> Write-Host "测试成功! 文件已下载到: $outputPath" -ForegroundColor Green >> Get-Content $outputPath -TotalCount 5 >> } >> else { >> Write-Host "测试失败: 文件未找到" -ForegroundColor Red >> } >> >> # 测试带哈希验证的下载 >> $expectedHash = "2a5f3c8d9e1b4a7c6d9f0e1a2b3c4d5e6f7a8b9c0" # 示例哈希,实际使用时替换 >> try { >> $secureDownload = Invoke-SecureDownload -Url $testUrl ` >> -OutputPath "$env:TEMP\Secure_README.md" ` >> -ExpectedHash $expectedHash ` >> -HashAlgorithm SHA256 >> Write-Host "带哈希验证的下载成功: $secureDownload" -ForegroundColor Green >> } >> catch { >> Write-Host "哈希验证失败: $_" -ForegroundColor Red >> } >> 详细信息: 正在从路径“C:\Users\Administrator\Documents\WindowsPowerShell\Modules\CurlTools\CurlTools.psd1”加载模块。 详细信息: 正在从路径“C:\Users\Administrator\Documents\WindowsPowerShell\Modules\CurlTools\CurlTools.psm1”加载模块。 < : 无法将“<”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 C:\Users\Administrator\Documents\WindowsPowerShell\Modules\CurlTools\CurlTools.psm1:1 字符: 1 + <上面完整的修复代码> + ~ + CategoryInfo : ObjectNotFound: (<:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Curl 路径: E:\curl-8.15.0_4-win64-mingw\bin 警告: 路径不存在,将在设置后创建: C:\Custom\Curl\Path 已设置curl路径: C:\Custom\Curl\Path 路径设置成功: C:\Custom\Curl\Path & : 无法将“C:\Custom\Curl\Path\curl.exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括 路径,请确保路径正确,然后再试一次。 所在位置 行:189 字符: 7 + & $curlExe --version | Select-Object -First 1 + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Custom\Curl\Path\curl.exe:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Curl 版本: 下载成功: E:\ai_temp\PowerShell_README.md 测试成功! 文件已下载到: E:\ai_temp\PowerShell_README.md # ![logo][] PowerShell Welcome to the PowerShell GitHub Community! [PowerShell](https://learn.microsoft.com/powershell/scripting/overview) is a cross-platform (Windows, Linux, and macOS) automation and configuration tool/framework that works well with your existing tools and is optimized for dealing with structured data (e.g. JSON, CSV, XML, etc.), REST APIs, and object models. 警告: 下载失败,4 秒后重试: 文件哈希不匹配! 期望: 2a5f3c8d9e1b4a7c6d9f0e1a2b3c4d5e6f7a8b9c0, 实际: 019AB8BA5AA2EEAF49E1A7BC4F1D6E8D2F27AE526ABAEDE989A0E5D07D0C0ECE 警告: 下载失败,2 秒后重试: 文件哈希不匹配! 期望: 2a5f3c8d9e1b4a7c6d9f0e1a2b3c4d5e6f7a8b9c0, 实际: 019AB8BA5AA2EEAF49E1A7BC4F1D6E8D2F27AE526ABAEDE989A0E5D07D0C0ECE 哈希验证失败: 下载失败: 文件哈希不匹配! 期望: 2a5f3c8d9e1b4a7c6d9f0e1a2b3c4d5e6f7a8b9c0, 实际: 019AB8BA5AA2EEAF49E1A7BC4F1D6E8D2F27AE526ABAEDE989A0E5D07D0C0ECE
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值