-
Notifications
You must be signed in to change notification settings - Fork 0
159 lines (140 loc) · 6.27 KB
/
Copy pathpython-publish.yml
File metadata and controls
159 lines (140 loc) · 6.27 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Publishes the Python package (Crypto-Trader-Analysis) to PyPI when a GitHub Release is created
# Requires repository secret: PYPI_API_TOKEN (an API token from PyPI)
# You can also run this manually via the "Run workflow" button (workflow_dispatch).
name: Publish Python Package (Crypto-Trader-Analysis)
on:
release:
types: [created]
push:
tags:
- 'v*'
workflow_dispatch:
jobs:
build-and-publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
packages: write
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10.16'
- name: Install build tooling
run: |
python -m pip install --upgrade pip
python -m pip install build
- name: Build package
working-directory: Crypto-Trader-Analysis
run: |
python -m build
- name: Extract project version
id: get_version
shell: bash
run: |
V=$(python -c "import tomllib; d=tomllib.load(open('Crypto-Trader-Analysis/pyproject.toml','rb')); print(d['project']['version'])")
echo "version=$V" >> "$GITHUB_OUTPUT"
- name: Detect tag and compare to project version
id: tag_check
shell: bash
run: |
REF="${GITHUB_REF:-${{ github.ref }}}"
echo "github.ref=$REF"
if [[ "$REF" == refs/tags/* ]]; then
TAG="${REF#refs/tags/}"
TV="${TAG#v}"
echo "is_tag=true" >> "$GITHUB_OUTPUT"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
echo "tag_version=$TV" >> "$GITHUB_OUTPUT"
if [ "$TV" = "${{ steps.get_version.outputs.version }}" ]; then
echo "matches=true" >> "$GITHUB_OUTPUT"
else
echo "matches=false" >> "$GITHUB_OUTPUT"
echo "Warning: Tag version ($TV) does not match pyproject version (${{ steps.get_version.outputs.version }})."
fi
else
echo "is_tag=false" >> "$GITHUB_OUTPUT"
echo "tag=" >> "$GITHUB_OUTPUT"
echo "tag_version=" >> "$GITHUB_OUTPUT"
fi
- name: Check if version already exists on PyPI
id: check_pypi
shell: bash
run: |
NAME="crypto-trader-analysis"
V="${{ steps.get_version.outputs.version }}"
URL="https://pypi.org/pypi/${NAME}/${V}/json"
echo "Checking existence on PyPI: $URL"
code=$(curl -L -s -o /dev/null -w "%{http_code}" "$URL")
exists=false
if [ "$code" = "200" ]; then exists=true; fi
echo "exists=$exists" >> "$GITHUB_OUTPUT"
if [ "$exists" = true ]; then echo "Version $V already exists on PyPI. Skipping upload."; fi
- name: Check for PyPI API token
id: has_token
shell: bash
env:
PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
run: |
if [ -n "$PYPI_TOKEN" ]; then echo "present=true" >> "$GITHUB_OUTPUT"; else echo "present=false" >> "$GITHUB_OUTPUT"; fi
- name: Publish package to PyPI (token)
if: steps.check_pypi.outputs.exists == 'false' && steps.has_token.outputs.present == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
packages-dir: Crypto-Trader-Analysis/dist
- name: Publish package to PyPI (OIDC)
if: steps.check_pypi.outputs.exists == 'false' && steps.has_token.outputs.present != 'true' && startsWith(github.ref, 'refs/tags/')
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: Crypto-Trader-Analysis/dist
- name: Skip publish (already exists)
if: steps.check_pypi.outputs.exists == 'true'
run: echo "Skipped publish. Version ${{ steps.get_version.outputs.version }} already exists on PyPI."
- name: Skip publish (no token and not a tag)
if: steps.check_pypi.outputs.exists == 'false' && steps.has_token.outputs.present != 'true' && !startsWith(github.ref, 'refs/tags/')
run: |
echo "Skipping publish: No PYPI_API_TOKEN configured and this run is not on a tag (github.ref=${GITHUB_REF})."
echo "To use Trusted Publishing (OIDC), configure a Trusted Publisher for this project on PyPI and trigger this workflow from a tag or release."
- name: Check for GitHub Packages token
id: has_gh_packages_token
shell: bash
env:
GH_PACKAGES_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
run: |
if [ -n "$GH_PACKAGES_TOKEN" ]; then echo "present=true" >> "$GITHUB_OUTPUT"; else echo "present=false" >> "$GITHUB_OUTPUT"; fi
- name: Publish to GitHub Packages (Python registry via GH_PACKAGES_TOKEN)
id: publish_ghp_pat
continue-on-error: true
if: steps.has_gh_packages_token.outputs.present == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: ${{ github.repository_owner }}
password: ${{ secrets.GH_PACKAGES_TOKEN }}
repository-url: https://pypi.pkg.github.com/${{ github.repository }}/
packages-dir: Crypto-Trader-Analysis/dist
skip-existing: true
attestations: false
- name: Publish to GitHub Packages (Python registry via GITHUB_TOKEN)
if: always() && (steps.has_gh_packages_token.outputs.present != 'true' || steps.publish_ghp_pat.conclusion == 'failure')
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
repository-url: https://pypi.pkg.github.com/${{ github.repository }}/
packages-dir: Crypto-Trader-Analysis/dist
skip-existing: true
attestations: false
- name: Debug upload to GitHub Packages with twine (verbose)
if: always() && env.DEBUG_GH_PACKAGES == '1'
env:
TWINE_USERNAME: ${{ github.repository_owner }}
TWINE_PASSWORD: ${{ secrets.GH_PACKAGES_TOKEN }}
run: |
python -m pip install --upgrade twine
python -m twine upload --verbose \
--repository-url https://pypi.pkg.github.com/${{ github.repository_owner }}/ \
Crypto-Trader-Analysis/dist/*