使用场景:由于特殊原因,前端文件构建必须使用阿里云的云效流水线进行构建,但是构建后的文件,只能存储到 oss 里面或者自身的流水线中,这里以存储到 oss 里面为例。使用 go 写一个获取文件名和文件的命令。
// go 获取 oss 文件名称和文件
package main
import (
"flag"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
"strings"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
accessKeyId := "xxxx"
accessKeySecret := "xxxx"
endpoint := flag.String("ep", "http://oss-cn-shanghai.aliyuncs.com", "aliyun OSS endpoint")
bucketName := flag.String("bn", "", "aliyun OSS BucketName")
objectName := flag.String("on", "", "aliyun oss objectName")
downloadedFileName := flag.String("df", "", "downloadedFileName")
ossPrefix := flag.String("op", "", "ossPrefix")
action := flag.String("ac", "getFile", "download or getFile")
client, err := oss.New(*endpoint, accessKeyId, accessKeySecret)
flag.Parse()
if err != nil {
handleError(err)
}
bucket, err := client.Bucket(*bucketName)
if err != nil {
handleError(err)
}
if *action == "download" {
err = bucket.GetObjectToFile(*objectName, *downloadedFileName)
if err != nil {
handleError(err)
}
}
if *action == "getFile" {
lsRes, err := bucket.ListObjects(oss.MaxKeys(10), oss.Prefix(*ossPrefix))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
var test string
for _, object := range lsRes.Objects {
arr := strings.Split(object.Key, "/")
test = test + arr[len(arr)-1] + "\n"
}
fmt.Println(test)
}
}
// jenkinsfile
pipeline {
agent {
label 'host235'
}
stages {
stage('选择发布版本') {
agent {
label 'host235'
}
steps {
script {
LIST = sh (script: 'getAliyunOss -bn="xxx-xxx-xxx-log" -op="A-build-package/xxx-xxx-xxx-stag" -ac="getFile"', returnStdout: true)
env.RELEASE_SCOPE = input message: '请选择发布版本', ok: '发布',
parameters: [choice(name: 'RELEASE_SCOPE', choices: LIST, description: '是否发布到 Stag 环境?')]
}
}
}
stage('部署到 Stag 环境') {
agent {
label 'host235'
}
steps {
script {
sh "rm -rf ./*.tar.gz"
sh "echo $RELEASE_SCOPE"
RELEASE = sh (script: "echo $RELEASE_SCOPE | cut -d '-' -f5-13 | cut -d '.' -f1-5", returnStdout: true).trim()
sh "echo $RELEASE"
sh """ getAliyunOss -bn="xxx-xxx-xxx-log" -on="A-build-package/xxx-xxx-xxx-stag/${RELEASE_SCOPE}" -df="${RELEASE_SCOPE}" -ac="download" """
sh "rm -rf /data/www/xxx-xxx-xxx/* && tar zxvf ${RELEASE_SCOPE} --strip-components 2 -C /data/www/xxx-xxx-xxx && ls -al /data/www/xxx-xxx-xxx"
}
}
}
}
}
作者:jackzang 创建时间:2022-12-28 16:19
最后编辑:jackzang 更新时间:2022-12-28 16:26
最后编辑:jackzang 更新时间:2022-12-28 16:26