-
Notifications
You must be signed in to change notification settings - Fork 0
176 lines (167 loc) · 5.72 KB
/
main.yml
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
name: ci
on:
push:
branches:
- master
- renovate/*
- dev/*
tags:
- v*
pull_request:
branches:
- master
- v0.8.x
jobs:
test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
#----------------------------------------#
# Install & configure Poetry #
#----------------------------------------#
- name: Load cached Poetry Binary
id: cached-poetry-binary
uses: actions/cache@v3
with:
path: ~/.local
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-1.6.1
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: "1.6.1"
virtualenvs-create: true
virtualenvs-in-project: true
#----------------------------------------------#
# Load cached venv if cache exists #
#----------------------------------------------#
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
# Restore cache with this prefix if not exact match with key
# Note cache-hit returns false in this case, so the below step will run
restore-keys: |
venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-
#----------------------------------------------------------#
# Install dependencies if cache does not exist #
#----------------------------------------------------------#
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
shell: bash
run: poetry install --no-interaction --no-root --all-extras
- name: Install root
run: poetry run pip install -e . --no-deps
- run: poetry run pytest --cov=ampel
mypy:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies (with all extras)
run: |
python -m pip install --upgrade poetry
poetry install --all-extras
- run: poetry run mypy -p ampel
check_version:
name: Tag version bump
runs-on: ubuntu-22.04
# run only on pushes, not PRs
if: ${{ github.event_name == 'push' && github.base_ref == null }}
needs: [test, mypy]
outputs:
should_publish: ${{ steps.check.outputs.result }}
steps:
- uses: actions/setup-node@v3
with:
node-version: 14
- run: npm install [email protected]
- name: Ensure tag for version bump
id: check
uses: actions/github-script@v6
with:
script: |
const toml = require('toml')
async function getVersion(ref) {
try {
response = await github.rest.repos.getContent({
repo: context.repo.repo,
owner: context.repo.owner,
path: 'pyproject.toml',
ref: ref
});
return toml.parse(Buffer.from(response.data.content, 'base64').toString())
.tool
.poetry
.version;
} catch (exc) {
if (exc.name == 'HttpError' && exc.status == 404) {
return null;
} else {
throw exc;
}
}
}
after = await getVersion(context.payload.after);
ref = `refs/tags/v${after}`
is_main = context.payload.ref === `refs/heads/${context.payload.repository.default_branch}`
// a tag matching the version was just pushed
let release = context.payload.ref == ref;
if (release) {
console.log(`Tag v${after} pushed (${context.sha})`);
}
// the version on the default branch changed; create a tag
if (!release && is_main) {
before = await getVersion(context.payload.before);
if (before !== after) {
console.log(`Version bumped: ${before} -> ${after}`);
try {
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref,
sha: context.sha
});
console.log(`Tag v${after} created (${context.sha})`);
release = true;
} catch (exc) {
// tag already existed
if (exc.name == 'HttpError' && exc.status == 422) {
console.log(`Skipping publish (tag v${after} already exists)`);
release = false;
} else {
throw exc;
}
}
} else {
console.log(`Skipping publish (version is still ${before})`);
}
} else if (!is_main) {
console.log(`Skipping publish (push to ${context.payload.ref} != refs/heads/${context.payload.repository.default_branch})`);
}
return release;
pypi:
name: Publish to PyPI
runs-on: ubuntu-22.04
needs: [check_version]
# NB: outputs are always strings; explicitly parse to get a boolean
if: ${{ fromJSON(needs.check_version.outputs.should_publish) }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install poetry
run: |
python -m pip install --upgrade poetry
- name: Publish
run: |
poetry publish -n --build
env:
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_API_TOKEN }}