name: CI on: push: branches: - Django workflow_dispatch: inputs: version: description: 版本号(如 0.2.2),为空则自动生成 required: false concurrency: group: ci-${{ github.ref }} cancel-in-progress: true jobs: docker-ci: if: github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && contains(github.event.head_commit.message, '[ci]')) runs-on: ubuntu-latest timeout-minutes: 40 env: DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }} DJANGO_DEBUG: "False" DJANGO_ALLOWED_HOSTS: "127.0.0.1,localhost" IMAGE_NAME: achievement_inputing_ci ALIST_URL: https://alist.spdis.top ALIST_USER: ${{ secrets.ALIST_USER }} ALIST_PASSWORD: ${{ secrets.ALIST_PASSWORD }} GITEA_SERVER: ${{ github.server_url }} GITEA_REPO: ${{ github.repository }} GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} steps: - name: Ensure source present env: SERVER: ${{ github.server_url }} REPO: ${{ github.repository }} REF: ${{ github.ref }} SHA: ${{ github.sha }} TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | if [ -f "$GITHUB_WORKSPACE/Dockerfile" ]; then exit 0; fi mkdir -p "$GITHUB_WORKSPACE" cd "$GITHUB_WORKSPACE" git init . if [ -z "$TOKEN" ]; then git fetch --depth=1 "$SERVER/$REPO.git" "$REF" else git -c http.extraHeader="Authorization: Bearer $TOKEN" fetch --depth=1 "$SERVER/$REPO.git" "$REF" fi git checkout FETCH_HEAD - name: Derive version run: | msg="${{ github.event.head_commit.message }}" ver_input="${{ github.event.inputs.version }}" ver="" if [ -n "$ver_input" ]; then ver="$ver_input" else ver=$(echo "$msg" | grep -Eo "\[[0-9]+(\.[0-9]+){1,}\]" | head -n1 | tr -d '[]') fi if [ -z "$ver" ]; then ver="$(date +%Y%m%d%H%M)-${GITHUB_SHA:0:7}" fi echo "VERSION=$ver" >> $GITHUB_ENV - name: Build application image run: | docker build -t "$IMAGE_NAME:$VERSION" -f "$GITHUB_WORKSPACE/Dockerfile" "$GITHUB_WORKSPACE" - name: Output image info run: | docker image inspect "$IMAGE_NAME:$VERSION" --format '{{.Id}} {{.Size}}' - name: Export image tar run: | ART="achievement_inputing_ci_${VERSION}.tar" docker save -o "$GITHUB_WORKSPACE/$ART" "$IMAGE_NAME:$VERSION" echo "$ART" > "$GITHUB_WORKSPACE/.artifact_name" - name: Upload to Alist run: | python3 - << 'PY' import os,requests,sys base=os.environ['ALIST_URL'].rstrip('/') user=os.environ['ALIST_USER'] pwd=os.environ['ALIST_PASSWORD'] ws=os.environ['GITHUB_WORKSPACE'] art=open(os.path.join(ws,'.artifact_name'),'r',encoding='utf-8').read().strip() fp=os.path.join(ws,art) r=requests.post(base+'/api/auth/login',json={'username':user,'password':pwd}) tok=(r.json().get('data') or {}).get('token') if not tok: sys.exit(1) requests.post(base+'/api/fs/mkdir',json={'path':'/ci'},headers={'Authorization':tok}) files={'file':(art,open(fp,'rb'),'application/x-tar')} s=requests.put(base+'/api/fs/form',data={'path':'/ci'},files=files,headers={'Authorization':tok}) sys.exit(0 if s.status_code<400 else 1) PY - name: Create release with download link if: env.GITEA_TOKEN != '' run: | ART=$(cat "$GITHUB_WORKSPACE/.artifact_name") BRANCH=${GITHUB_REF#refs/heads/} TAG="$VERSION" NAME="$VERSION" DL="$ALIST_URL/ci/$ART" JSON=$(printf '{"tag_name":"%s","target_commitish":"%s","name":"%s","body":"%s"}' "$TAG" "$BRANCH" "$NAME" "$DL") curl -sS -X POST "$GITEA_SERVER/api/v1/repos/$GITEA_REPO/releases" -H "Content-Type: application/json" -H "Authorization: token $GITEA_TOKEN" -d "$JSON"