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_PATH: /lanzouyunyouxiang/ci ALIST_USER: ${{ secrets.ALIST_USER }} ALIST_PASSWORD: ${{ secrets.ALIST_PASSWORD }} GITEA_SERVER: ${{ github.server_url }} GITEA_REPO: ${{ github.repository }} RELEASE_TOKEN: ${{ secrets.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: | set -e ART=$(cat "$GITHUB_WORKSPACE/.artifact_name") BASE="${ALIST_URL%/}" curl -sS -o "$GITHUB_WORKSPACE/login.json" -w "%{http_code}" -X POST "$BASE/api/auth/login" -H "Content-Type: application/json" -d "{\"username\":\"$ALIST_USER\",\"password\":\"$ALIST_PASSWORD\"}" > "$GITHUB_WORKSPACE/login.code" if [ "$(cat "$GITHUB_WORKSPACE/login.code")" != "200" ]; then echo login_failed cat "$GITHUB_WORKSPACE/login.json" exit 1 fi TOKEN=$(sed -n 's/.*"token":"\([^"]*\)".*/\1/p' "$GITHUB_WORKSPACE/login.json") if [ -z "$TOKEN" ]; then TOKEN=$(sed -n 's/.*"auth":"\([^"]*\)".*/\1/p' "$GITHUB_WORKSPACE/login.json") fi if [ -z "$TOKEN" ]; then echo token_not_found cat "$GITHUB_WORKSPACE/login.json" exit 1 fi curl -sS -X POST "$BASE/api/fs/mkdir" -H "Authorization: $TOKEN" -H "Content-Type: application/json" -d "{\"path\":\"$ALIST_PATH\"}" >/dev/null 2>&1 || true UPLOAD_STATUS=$(curl -sS -w "%{http_code}" -o /dev/null -X PUT "$BASE/api/fs/form" -H "Authorization: $TOKEN" -F "path=$ALIST_PATH" -F "file=@$GITHUB_WORKSPACE/$ART;type=application/x-tar") if [ "$UPLOAD_STATUS" -ge 400 ]; then UPLOAD_STATUS=$(curl -sS -w "%{http_code}" -o /dev/null -X PUT "$BASE/api/fs/form" -H "Authorization: Bearer $TOKEN" -F "path=$ALIST_PATH" -F "file=@$GITHUB_WORKSPACE/$ART;type=application/x-tar") fi if [ "$UPLOAD_STATUS" -ge 400 ]; then echo upload_failed exit 1 fi - name: Create release with download link if: env.RELEASE_TOKEN != '' run: | ART=$(cat "$GITHUB_WORKSPACE/.artifact_name") BRANCH=${GITHUB_REF#refs/heads/} TAG="$VERSION" NAME="$VERSION" DL="${ALIST_URL%/}${ALIST_PATH}/$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 $RELEASE_TOKEN" -d "$JSON"