File size: 4,172 Bytes
ad38d4e
 
 
 
 
 
 
ed8540d
 
 
ad38d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42eccd
 
ad38d4e
 
b42eccd
 
45fb053
 
 
ad38d4e
 
 
 
45fb053
ad38d4e
 
 
 
 
 
 
 
45fb053
ad38d4e
 
 
 
 
2708dcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
090f728
 
a42ff54
090f728
 
 
 
 
 
 
 
fdd1450
c1b7606
 
 
1ef094e
c1b7606
090f728
 
 
 
 
36c27f3
090f728
 
 
 
 
36c27f3
090f728
 
2708dcd
090f728
2708dcd
 
090f728
e0bda3b
090f728
e0bda3b
2449c37
 
 
e0bda3b
090f728
e0bda3b
090f728
e0bda3b
 
090f728
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: Publish Release

on:
  push:
    branches:
      - main  # Trigger on push to main branch

permissions:
  contents: write  # Ensure write access to push tags

jobs:
  release:
    name: Create GitHub Release
    runs-on: ubuntu-latest

    steps:
    # Step 1: Checkout the code
    - name: Checkout code
      uses: actions/checkout@v3

    # Step 2: Set up Python
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.x'

    # Step 3: Install dependencies
    - name: Install dependencies
      run: pip install toml

    # Step 4: Extract version from pyproject.toml
    - name: Extract version
      id: get_version
      run: |
        VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
        echo "VERSION=$VERSION" >> $GITHUB_ENV

    # Step 5: Check if tag exists on remote
    - name: Check if tag exists on remote
      id: check_tag
      run: |
        if git ls-remote --tags origin | grep "refs/tags/v${{ env.VERSION }}"; then
          echo "Tag v${{ env.VERSION }} already exists on remote."
          echo "tag_exists=true" >> $GITHUB_ENV
        else
          echo "tag_exists=false" >> $GITHUB_ENV
        fi

    # Step 6: Create and push a new tag (if it doesn't exist)
    - name: Create Git tag
      if: env.tag_exists == 'false'
      run: |
        git config --global user.name "github-actions[bot]"
        git config --global user.email "github-actions[bot]@users.noreply.github.com"
        git tag -a "v${{ env.VERSION }}" -m "Release v${{ env.VERSION }}"
        git push origin "v${{ env.VERSION }}"

    # Step 7: Create GitHub release (if tag didn't exist)
    - name: Create GitHub Release
      if: env.tag_exists == 'false'
      uses: softprops/action-gh-release@v1
      with:
        tag_name: "v${{ env.VERSION }}"
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  
  pypi:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    needs: release  # This job runs after the release job

    steps:
    # Step 1: Checkout the code
    - name: Checkout code
      uses: actions/checkout@v3

    # Step 2: Set up Python
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.x'

    # Step 3: Install dependencies
    - name: Install dependencies
      run: pip install toml requests

    # Step 4: Extract current version from pyproject.toml
    - name: Extract current version
      id: get_version
      run: |
        VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
        echo "VERSION=$VERSION" >> $GITHUB_ENV

    # Step 5: Get latest version from PyPI
    - name: Get latest version from PyPI
      id: get_pypi_version
      run: |
        LATEST_PYPI_VERSION=$(python -c "import toml; import requests; PACKAGE_NAME = toml.load('pyproject.toml')['project']['name']; response = requests.get(f'https://pypi.org/pypi/{PACKAGE_NAME}/json'); print(response.json()['info']['version'])")
        echo "LATEST_PYPI_VERSION=$LATEST_PYPI_VERSION" >> $GITHUB_ENV

    # Step 6: Compare current version with the latest tag
    - name: Check if version is bumped
      id: check_version
      run: |
        if [ "${{ env.VERSION }}" = "${{ env.LATEST_PYPI_VERSION }}" ]; then
          echo "Version not bumped. Exiting."
          echo "version_bumped=false" >> $GITHUB_ENV
        else
          echo "Version bumped. Proceeding."
          echo "version_bumped=true" >> $GITHUB_ENV
        fi

    # Step 7: Install Flit (only if version bumped)
    - name: Install Flit
      if: env.version_bumped == 'true'
      run: pip install flit

    # Step 8: Create .pypirc file (only if version bumped)
    - name: Create .pypirc file
      if: env.version_bumped == 'true'
      run: |
        echo "[pypi]" > ~/.pypirc
        echo "username = __token__" >> ~/.pypirc
        echo "password = ${{ secrets.PYPI_API_TOKEN }}" >> ~/.pypirc

    # Step 9: Build and publish package (only if version bumped)
    - name: Build and Publish Package
      if: env.version_bumped == 'true'
      run: |
        flit build
        flit publish