💚 Optimize github actions build

pull/548/head
微凉 2022-01-10 19:48:11 +08:00
parent c24894b5de
commit 7c7306bf96
4 changed files with 152 additions and 164 deletions

View File

@ -31,13 +31,6 @@ jobs:
ref: v2 ref: v2
path: alist path: alist
- name: Checkout web repo
uses: actions/checkout@v2
with:
repository: Xhofe/alist-web
ref: v2
path: alist-web
- name: Set up xgo - name: Set up xgo
run: | run: |
docker pull crazymax/xgo:latest docker pull crazymax/xgo:latest
@ -47,10 +40,49 @@ jobs:
- name: Build - name: Build
run: | run: |
mv alist/build.sh . mv alist/build.sh .
bash build.sh bash build.sh web
mv dist/* alist/public
bash build.sh build
- name: Upload artifact - name: Upload artifact
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
with: with:
name: artifact name: artifact
path: alist/build path: alist/build
docker:
name: Docker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: xhofe/alist
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Build web
run: |
bash build.sh web
mv dist/* public
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: xhofe
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386,linux/arm/v6,linux/s390x

View File

@ -1,48 +0,0 @@
name: docker
on:
push:
branches:
- 'v2'
tags:
- 'v*'
pull_request:
branches:
- 'v2'
jobs:
docker:
name: Docker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: xhofe/alist
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Build web
run: bash build.sh web
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: xhofe
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386,linux/arm/v6,linux/s390x

View File

@ -19,14 +19,6 @@ jobs:
with: with:
go-version: ${{ matrix.go-version }} go-version: ${{ matrix.go-version }}
# - name: Setup docker
# uses: docker-practice/actions-setup-docker@master
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:
@ -35,35 +27,57 @@ jobs:
persist-credentials: false persist-credentials: false
fetch-depth: 0 fetch-depth: 0
- name: Checkout web repo
uses: actions/checkout@v2
with:
repository: Xhofe/alist-web
ref: v2
path: alist-web
persist-credentials: false
fetch-depth: 0
- name: Set up xgo - name: Set up xgo
run: | run: |
docker pull techknowlogick/xgo:latest docker pull crazymax/xgo:latest
go install src.techknowlogick.com/xgo@latest go install github.com/crazy-max/xgo@latest
sudo apt install upx sudo apt install upx
- name: Build - name: Build
run: | run: |
mv alist/build.sh . mv alist/build.sh .
bash build.sh cdn
mv dist/* alist/public
bash build.sh release bash build.sh release
- name: Upload asserts files
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.MY_TOKEN }}
branch: cdn
directory: alist-web
repository: Xhofe/alist-web
- name: Release - name: Release
uses: softprops/action-gh-release@v1 uses: softprops/action-gh-release@v1
with: with:
files: alist/build/compress/* files: alist/build/compress/*
docker:
name: Docker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: xhofe/alist
- name: Set up Node
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Build web
run: |
bash build.sh cdn
mv dist/* public
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: xhofe
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386,linux/arm/v6,linux/s390x

150
build.sh
View File

@ -1,17 +1,24 @@
#!/bin/bash #!/bin/bash
if [ "$1" == "web" ]; then
git clone https://github.com/Xhofe/alist-web.git # 构建前端,在当前目录产生一个dist文件夹
cd alist-web || exit BUILD_WEB() {
git clone https://github.com/alist-org/alist-web.git
cd alist-web
yarn yarn
yarn build yarn build
mv dist/* ../public mv dist ..
cd .. cd ..
exit 0 rm -rf alist-web
fi }
go env -w GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/goproxy/,https://goproxy.io,direct CDN_WEB() {
curl -L https://github.com/alist-org/alist-web/releases/latest/download/dist.tar.gz -o dist.tar.gz
tar -zxvf dist.tar.gz
rm -f dist.tar.gz
}
if [ "$1" == "docker" ]; then # 在DOCKER中构建
BUILD_DOCKER() {
appName="alist" appName="alist"
builtAt="$(date +'%F %T %z')" builtAt="$(date +'%F %T %z')"
goVersion=$(go version | sed 's/go version //') goVersion=$(go version | sed 's/go version //')
@ -25,40 +32,20 @@ if [ "$1" == "docker" ]; then
-X 'github.com/Xhofe/alist/conf.GitAuthor=$gitAuthor' \ -X 'github.com/Xhofe/alist/conf.GitAuthor=$gitAuthor' \
-X 'github.com/Xhofe/alist/conf.GitCommit=$gitCommit' \ -X 'github.com/Xhofe/alist/conf.GitCommit=$gitCommit' \
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \ -X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
" "
go build -o ./bin/alist -ldflags="$ldflags" -tags=jsoniter alist.go go build -o ./bin/alist -ldflags="$ldflags" -tags=jsoniter alist.go
exit 0 }
fi
cd alist-web || exit BUILD() {
webCommit=$(git log --pretty=format:"%h" -1) cd alist
echo "web commit id: $webCommit" appName="alist"
yarn builtAt="$(date +'%F %T %z')"
if [ "$1" == "release" ]; then goVersion=$(go version | sed 's/go version //')
yarn build --base="https://cdn.jsdelivr.net/gh/Xhofe/alist-web@cdn/v2/$webCommit" gitAuthor=$(git show -s --format='format:%aN <%ae>' HEAD)
mv dist/assets .. gitCommit=$(git log --pretty=format:"%h" -1)
mv dist/index.html ../alist/public gitTag=$(git describe --long --tags --dirty --always)
# 构建local echo "build version: $gitTag"
yarn build ldflags="\
mv dist/index.html dist/local.html
mv dist/* ../alist/public
else
yarn build
mv dist/* ../alist/public
fi
cd ..
cd alist
appName="alist"
builtAt="$(date +'%F %T %z')"
goVersion=$(go version | sed 's/go version //')
gitAuthor=$(git show -s --format='format:%aN <%ae>' HEAD)
gitCommit=$(git log --pretty=format:"%h" -1)
gitTag=$(git describe --long --tags --dirty --always)
echo "build version: $gitTag"
ldflags="\
-w -s \ -w -s \
-X 'github.com/Xhofe/alist/conf.BuiltAt=$builtAt' \ -X 'github.com/Xhofe/alist/conf.BuiltAt=$builtAt' \
-X 'github.com/Xhofe/alist/conf.GoVersion=$goVersion' \ -X 'github.com/Xhofe/alist/conf.GoVersion=$goVersion' \
@ -67,44 +54,47 @@ ldflags="\
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \ -X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
" "
if [ "$1" == "release" ]; then if [ "$1" == "release" ]; then
xgo -out alist -ldflags="$ldflags" -tags=jsoniter . xgo -out "$appName" -ldflags="$ldflags" -tags=jsoniter .
else else
xgo -targets=linux/amd64,windows/amd64,darwin/amd64 -out alist -ldflags="$ldflags" -tags=jsoniter . xgo -targets=linux/amd64,windows/amd64 -out alist -ldflags="$ldflags" -tags=jsoniter .
fi fi
mkdir "build" mkdir "build"
mv alist-* build mv alist-* build
cd build || exit cd build
upx -9 ./* upx -9 ./*
find . -type f -print0 | xargs -0 md5sum > md5.txt find . -type f -print0 | xargs -0 md5sum >md5.txt
cat md5.txt cat md5.txt
# compress file (release) cd ../..
if [ "$1" == "release" ]; then }
mkdir compress
mv md5.txt compress
for i in `find . -type f -name "$appName-linux-*"`
do
tar -czvf compress/"$i".tar.gz "$i"
done
for i in `find . -type f -name "$appName-darwin-*"`
do
tar -czvf compress/"$i".tar.gz "$i"
done
for i in `find . -type f -name "$appName-windows-*"`
do
zip compress/$(echo $i | sed 's/\.[^.]*$//').zip "$i"
done
fi
cd ../..
if [ "$1" == "release" ]; then RELEASE() {
cd alist-web cd alist/build
git checkout cdn mkdir compress
mkdir "v2/$webCommit" mv md5.txt compress
mv ../assets/ v2/$webCommit for i in $(find . -type f -name "$appName-linux-*"); do
git add . tar -czvf compress/"$i".tar.gz "$i"
git config --local user.email "i@nn.ci" done
git config --local user.name "Xhofe" for i in $(find . -type f -name "$appName-darwin-*"); do
git commit --allow-empty -m "upload $webCommit assets files" -a tar -czvf compress/"$i".tar.gz "$i"
cd .. done
fi for i in $(find . -type f -name "$appName-windows-*"); do
zip compress/$(echo $i | sed 's/\.[^.]*$//').zip "$i"
done
cd ../..
}
if [ "$1" = "web" ]; then
BUILD_WEB
elif [ "$1" = "cdn" ]; then
CDN_WEB
elif [ "$1" = "docker" ]; then
BUILD_DOCKER
elif [ "$1" = "build" ]; then
BUILD
elif [ "$1" = "release" ]; then
BUILD
RELEASE
else
echo -e "${RED_COLOR} 错误的命令${RES}"
fi