generated from br3ndonland/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (175 loc) · 7.25 KB
/
ci.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
177
178
179
180
181
182
183
184
name: ci
on:
pull_request:
push:
branches: [develop, main, wip]
tags: ["[0-9]+.[0-9]+.[0-9]+*"]
workflow_dispatch:
inputs:
environment:
description: GitHub Actions deployment environment
required: false
type: environment
jobs:
setup:
runs-on: ubuntu-latest
outputs:
environment-name: ${{ steps.set-env.outputs.environment-name }}
environment-url: ${{ steps.set-env.outputs.environment-url }}
steps:
- uses: actions/checkout@v4
- name: Set GitHub Actions deployment environment
id: set-env
run: |
if ${{ github.event_name == 'workflow_dispatch' }}; then
environment_name=${{ inputs.environment }}
elif ${{ github.ref_type == 'tag' }}; then
environment_name="PyPI"
else
environment_name=""
fi
timestamp="$(date -Iseconds)"
url="https://api.github.com/repos/${{ github.repository }}/deployments"
environment_url="$url?timestamp=$timestamp"
echo "environment-name=$environment_name" >>"$GITHUB_OUTPUT"
echo "environment-url=$environment_url" >>"$GITHUB_OUTPUT"
- name: Create annotation for deployment environment
if: steps.set-env.outputs.environment != ''
run: echo "::notice::Deployment environment ${{ steps.set-env.outputs.environment }}"
ci:
runs-on: ubuntu-latest
needs: [setup]
environment:
name: ${{ needs.setup.outputs.environment-name }}
url: ${{ needs.setup.outputs.environment-url }}&python=${{ matrix.python-version }}
permissions:
id-token: write
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
env:
HATCH_ENV: "ci"
HATCH_VERSION: "1.9.4"
PIPX_VERSION: "1.5.0"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Set up pip cache
if: runner.os == 'Linux'
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install pipx for Python ${{ matrix.python-version }}
run: python -m pip install "pipx==$PIPX_VERSION"
- name: Install Hatch
run: pipx install "hatch==$HATCH_VERSION"
- name: Test Hatch version
run: |
HATCH_VERSION_INSTALLED=$(hatch --version)
echo "The HATCH_VERSION environment variable is set to $HATCH_VERSION."
echo "The installed Hatch version is ${HATCH_VERSION_INSTALLED##Hatch, version }."
case $HATCH_VERSION_INSTALLED in
*$HATCH_VERSION) echo "Hatch version correct." ;;
*) echo "Hatch version incorrect." && exit 1 ;;
esac
- name: Install dependencies
run: hatch env create ${{ env.HATCH_ENV }}
- name: Test virtualenv location
run: |
EXPECTED_VIRTUALENV_PATH=$GITHUB_WORKSPACE/.venv
INSTALLED_VIRTUALENV_PATH=$(hatch env find)
echo "The virtualenv should be at $EXPECTED_VIRTUALENV_PATH."
echo "Hatch is using a virtualenv at $INSTALLED_VIRTUALENV_PATH."
case "$INSTALLED_VIRTUALENV_PATH" in
"$EXPECTED_VIRTUALENV_PATH") echo "Correct Hatch virtualenv." ;;
*) echo "Incorrect Hatch virtualenv." && exit 1 ;;
esac
- name: Test that Git tag version and Python package version match
if: github.ref_type == 'tag' && matrix.python-version == '3.11'
run: |
GIT_TAG_VERSION=$GITHUB_REF_NAME
PACKAGE_VERSION=$(hatch version)
echo "The Python package version is $PACKAGE_VERSION."
echo "The Git tag version is $GIT_TAG_VERSION."
if [ "$PACKAGE_VERSION" = "$GIT_TAG_VERSION" ]; then
echo "Versions match."
else
echo "Versions do not match." && exit 1
fi
- name: Run Hatch script for code quality checks
run: hatch run ${{ env.HATCH_ENV }}:check
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_S3_BUCKET_REGION }}
role-to-assume: ${{ secrets.AWS_IAM_ROLE_ARN }}
role-duration-seconds: 1200
- name: Run tests
run: hatch run ${{ env.HATCH_ENV }}:coverage run -m pytest --durations=0 --durations-min=0.1
timeout-minutes: 5
env:
AWS_IAM_ACCESS_KEY_FASTENV: ${{ secrets.AWS_IAM_ACCESS_KEY_FASTENV }}
AWS_IAM_ACCESS_KEY_SESSION: ${{ env.AWS_ACCESS_KEY_ID }}
AWS_IAM_SECRET_KEY_FASTENV: ${{ secrets.AWS_IAM_SECRET_KEY_FASTENV }}
AWS_IAM_SECRET_KEY_SESSION: ${{ env.AWS_SECRET_ACCESS_KEY }}
AWS_IAM_SESSION_TOKEN: ${{ env.AWS_SESSION_TOKEN }}
AWS_S3_BUCKET_HOST: ${{ secrets.AWS_S3_BUCKET_HOST }}
BACKBLAZE_B2_ACCESS_KEY_FASTENV: ${{ secrets.BACKBLAZE_B2_ACCESS_KEY_FASTENV }}
BACKBLAZE_B2_BUCKET_HOST: ${{ secrets.BACKBLAZE_B2_BUCKET_HOST }}
BACKBLAZE_B2_BUCKET_REGION: ${{ secrets.BACKBLAZE_B2_BUCKET_REGION }}
BACKBLAZE_B2_SECRET_KEY_FASTENV: ${{ secrets.BACKBLAZE_B2_SECRET_KEY_FASTENV }}
CLOUDFLARE_R2_ACCESS_KEY_FASTENV: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_FASTENV }}
CLOUDFLARE_R2_BUCKET_HOST: ${{ secrets.CLOUDFLARE_R2_BUCKET_HOST }}
CLOUDFLARE_R2_SECRET_KEY_FASTENV: ${{ secrets.CLOUDFLARE_R2_SECRET_KEY_FASTENV }}
- name: Enforce test coverage
run: hatch run ${{ env.HATCH_ENV }}:coverage report
- name: Build Python package
run: hatch build
- name: Publish Python package to PyPI
if: >
github.ref_type == 'tag' &&
matrix.python-version == '3.11' &&
needs.setup.outputs.environment-name == 'PyPI'
uses: pypa/gh-action-pypi-publish@release/v1.8
changelog:
if: github.ref_type == 'tag'
needs: [ci]
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: develop
- name: Generate changelog from Git tags
run: |
echo '# Changelog
' >CHANGELOG.md
echo '# Changelog
[View on GitHub](https://github.com/br3ndonland/fastenv/blob/develop/CHANGELOG.md)
' >docs/changelog.md
GIT_LOG_FORMAT='## %(subject) - %(taggerdate:short)
%(contents:body)
Tagger: %(taggername) %(taggeremail)
Date: %(taggerdate:iso)
```text
%(contents:signature)```
'
git tag -l --sort=-taggerdate:iso --format="$GIT_LOG_FORMAT" >>CHANGELOG.md
git tag -l --sort=-taggerdate:iso --format="$GIT_LOG_FORMAT" >>docs/changelog.md
- name: Format changelog with Prettier
run: npx -s -y prettier@'^2' --write CHANGELOG.md docs/changelog.md
- name: Create pull request with updated changelog
uses: peter-evans/create-pull-request@v6
with:
add-paths: |
CHANGELOG.md
docs/changelog.md
branch: create-pull-request/${{ github.ref_name }}
commit-message: Update changelog for version ${{ github.ref_name }}
title: Update changelog for version ${{ github.ref_name }}