gitlab 发布后在附件里会有打包好的源码,类似下图
笔者想把构建好的文件也打包放在这个附件里,经过研究可行,步骤分享如下
注:笔者用的gitlab版本为12.10.3
创建Access Token
登录gitlab,点击右上角图像,点击Settings
点击左侧菜单Access Tokens
输入token的名称,有效期不填即永不过期,勾选api,write_repository,点击 create personal access token, 将生成的token值另外保存起来。
当然,你也可以回收这个token,点击Revoke。
编写上传脚本
#!/bin/bash
if [ -z "$version" ]; then
echo "no version was given"
exit 1
fi
if [ -z "$access_token" ]; then
echo "no access_token was given"
exit 1
fi
if [ -z "$package" ]; then
echo "no release package path was given"
exit 1
fi
if [ -z "$projectId" ]; then
echo "no project Id was given"
exit 1
fi
#uploading binary to asset for release
binaries=($package)
baseUrl=http://192.168.1.110:8929
for i in "${!binaries[@]}"; do
binary=${binaries[i]}
name=$(echo "$binary" | sed -e "s/.*\/\([^/]*\)/\1/")
res=$(curl --request POST --header "PRIVATE-TOKEN: $access_token" --form "file=@$binary" "$baseUrl/api/v4/projects/$projectId/uploads")
fullpath=$(echo $res | sed -e "s/.*\"full_path\":\"\([^\"]*\)\".*/\1/")
if [ ! -z "$fullpath" ]; then
curl --header "Content-Type: application/json" --request POST --header "PRIVATE-TOKEN: $access_token" --data "{ \"id\": \"$name\", \"name\":\"$name\",\"url\":\"$baseUrl/$fullpath\"}" "$baseUrl/api/v4/projects/$projectId/releases/$version/assets/links"
fi
done
在.gitlab-ci.yml调用该脚本, 类似如下
stages:
- publish
publish:
stage: publish
script:
- export package=<your package file Path>
- export version=$CI_COMMIT_TAG
- export access_token=<access token>
- export projectId=<project id>
- chmod +x ./publish.sh
- ./publish.sh
only:
refs:
- tags
package 就是你要是上传的附件的文件路径
$CI_COMMIT_TAG 就是你进行release时填写的tag
access_token 就是刚才上面步骤创建的access token
project id 是你要上传的项目仓储的id (如下图所示)
最终效果如下