Newer
Older
thu-learn-downloader-optimized / .github / workflows / ci.yaml
@Qin Li Qin Li on 27 Feb 2023 5 KB fix: rename release assets
name: CI/CD

on:
  push:
    branches:
      - main

# - [x] Settings > General > Pull Requests > Automatically delete head branches
# - [x] Settings > Actions > General > Workflow permissions > Allow GitHub Actions to create and approve pull requests
permissions:
  contents: write
  pull-requests: write

env:
  PYTHON_VERSION: "3.11"

jobs:
  detect-build:
    name: Detect Build Script
    runs-on: ubuntu-latest
    outputs:
      build: ${{ steps.detect.outputs.build }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - id: detect
        name: Detect Build Script
        run: |
          if [[ -f scripts/build.sh ]]; then
            echo "build=true" >> "${GITHUB_OUTPUT}"
            echo ":heavy_check_mark: Build Script Detected" >> "${GITHUB_STEP_SUMMARY}"
          else
            echo "build=false" >> "${GITHUB_OUTPUT}"
            echo ":x: Build Script Not Found" >> "${GITHUB_STEP_SUMMARY}"
          fi

  detect-publish:
    name: Detect PyPI Credential
    runs-on: ubuntu-latest
    outputs:
      publish: ${{ steps.detect.outputs.publish }}
    steps:
      - id: detect
        name: Detect PyPI Credential
        run: |
          if [[ -n "${PYPI_USERNAME}" && -n "${PYPI_PASSWORD}" ]]; then
            echo "publish=true" >> "${GITHUB_OUTPUT}"
            echo ":heavy_check_mark: PyPI Credential Detected" >> "${GITHUB_STEP_SUMMARY}"
          else
            echo "publish=false" >> "${GITHUB_OUTPUT}"
            echo ":x: PyPI Credential Not Found" >> "${GITHUB_STEP_SUMMARY}"
          fi
        env:
          PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
          PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}

  build-pkg:
    name: Build Package
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install Poetry
        run: pipx install poetry
      - id: python
        name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: poetry
      - name: Install Dependencies
        run: poetry install --no-interaction
      - name: Build Package
        run: poetry build --no-interaction
      - name: Upload Build Artifact
        uses: actions/upload-artifact@v3
        with:
          name: package
          path: dist/*

  build-exe:
    name: Build Executable
    needs:
      - detect-build
    if: needs.detect-build.outputs.build == 'true'
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install Poetry
        run: pipx install poetry
      - id: python
        name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: poetry
      - name: Install Dependencies
        run: poetry install --no-interaction
      - name: Build Executable
        run: poetry run bash scripts/build.sh
      - if: runner.os != 'Windows'
        name: Rename Build Artifact
        run: mv dist/* "dist/${{ github.event.repository.name }}-${{ runner.os }}-${{ runner.arch }}"
      - if: runner.os == 'Windows'
        name: Rename Build Artifact
        run: mv dist/* "dist/${{ github.event.repository.name }}-${{ runner.os }}-${{ runner.arch }}.exe"
      - name: Upload Build Artifact
        uses: actions/upload-artifact@v3
        with:
          name: ${{ runner.os }}-${{ runner.arch }}
          path: dist/*
    strategy:
      matrix:
        os:
          - macos-latest
          - ubuntu-latest
          - windows-latest

  release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    outputs:
      releases-created: ${{ steps.release.outputs.releases_created }}
      tag-name: ${{ steps.release.outputs.tag_name }}
    steps:
      - id: release
        name: Create GitHub Release
        uses: google-github-actions/release-please-action@v3
        with:
          release-type: python

  upload:
    name: Upload Release Assets
    needs:
      - build-exe
      - build-pkg
      - release
    if: always() && needs.release.outputs.releases-created == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v3
        with:
          path: artifacts
      - name: Upload Release Assets
        uses: svenstaro/upload-release-action@master
        with:
          file: artifacts/**/*
          tag: ${{ needs.release.outputs.tag-name }}
          file_glob: true
          overwrite: true

  publish:
    name: Publish to PyPI
    needs:
      - detect-publish
      - release
    if: needs.detect-publish.outputs.publish == 'true' && needs.release.outputs.releases-created == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install Poetry
        run: pipx install poetry
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: poetry
      - name: Install Dependencies
        run: poetry install --no-interaction
      - name: Publish to PyPI
        run: poetry publish --username "${{ secrets.PYPI_USERNAME }}" --password "${{ secrets.PYPI_PASSWORD }}" --build --no-interaction