-
Notifications
You must be signed in to change notification settings - Fork 1
198 lines (175 loc) · 7.67 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
name: CI
on:
workflow_dispatch:
push:
branches:
- '**'
- "main"
- staging
- 'feature/**'
- '!ws-iac-scan-results/**'
- '!whitesource-remediate/master-all**'
- '!whitesource/**'
tags:
- '*'
# schedule:
# - cron: '30 13 * * *'
jobs:
build-and-test:
runs-on: ubuntu-latest
outputs:
python_version: ${{ steps.set_env_vars.outputs.PYTHON_VERSION }}
min_py_ver: ${{ steps.set_env_vars.outputs.MIN_PY_VER }}
version: ${{ steps.set_env_vars.outputs.VERSION }}
release: ${{ steps.set_env_vars.outputs.RELEASE }}
app_name: ${{ steps.set_env_vars.outputs.APP_NAME }}
app_pwd: ${{ steps.set_env_vars.outputs.APP_PWD }}
app_dir: ${{ steps.set_env_vars.outputs.APP_DIR }}
pkg_name: ${{ steps.set_env_vars.outputs.PKG_NAME }}
whl_name: ${{ steps.set_env_vars.outputs.WHL_NAME }}
source_branch: ${{ steps.get_source_branch.outputs.SOURCE_BRANCH }}
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v3
- name: List Tags
run: |
git fetch --tags
git tag -l
- name: Set Environment Variables
id: set_env_vars
env:
APP_PWD: '.' # Change this if the app directory and manifest file(s) are not located under the repo root
run: |
APP_DIR="$(find "${{ env.APP_PWD }}" -maxdepth 1 -type d -path "*mend_*" | sed 's|${{ env.APP_PWD }}/||')"
APP_NAME="$APP_DIR" # "$(echo "$APP_DIR" | sed 's/_/-/g')"
echo "App directory: $APP_DIR"
echo "App name: $APP_NAME"
pkgVersion="$(echo "$(cat ${APP_DIR}/_version.py | grep "__version__")" | sed 's/__version__ = "\(.*\)"/\1/')"
pkgRelease=false
echo "========= GITHUB_OUTPUT ========="
echo "PYTHON_VERSION=${{ matrix.python-version }}" >> $GITHUB_OUTPUT
if [[ ${{ strategy.job-index }} == 0 ]]; then
echo "MIN_PY_VER=${{ matrix.python-version }}" >> $GITHUB_OUTPUT
fi
echo "GitHub Ref: $GITHUB_REF"
if [[ "$GITHUB_REF" == *"refs/tags/v"* || "$GITHUB_REF" == *"refs/tags/test-v"* ]]; then
pkgVersion="$(echo ${{ github.ref }} | sed -r 's/^[\/a-zA-z-]+//')"
if [[ $pkgVersion != *@(a|b)* ]]; then
pkgRelease=true
fi
else
echo "get latestver"
latestVer="$(git describe --tags $(git rev-list --tags --max-count=1) | sed -r 's/^[\/a-zA-z-]+//')"
echo "Last ver: $latestVer"
if [[ $(echo "$latestVer" | cut -d. -f1,2) == $(date +"%y.%-m") ]] ; then
nextVersion="$(date +"%y.%-m").$(( $(echo "$latestVer" | sed 's/.*\.//') + 1 ))"
else
nextVersion="$(date +"%y.%-m.1.1")"
fi
if [[ "$GITHUB_REF" == *"/staging" ]]; then
pkgVersion="${nextVersion}rc$(date +"%Y%m%d%H%M")"
elif [[ "$GITHUB_REF" == *"/main" ]]; then
pkgVersion="${nextVersion}"
else
pkgVersion="${nextVersion}.dev0"
fi
fi
echo "Package ver: $pkgVersion"
echo "VERSION=$pkgVersion" >> $GITHUB_OUTPUT
echo "RELEASE=$pkgRelease" >> $GITHUB_OUTPUT
echo "APP_NAME=$APP_DIR" >> $GITHUB_OUTPUT
echo "APP_PWD=${{ env.APP_PWD }}" >> $GITHUB_OUTPUT
echo "APP_DIR=$APP_DIR" >> $GITHUB_OUTPUT
echo "PKG_NAME=${APP_NAME}-${pkgVersion}" >> $GITHUB_OUTPUT
echo "WHL_NAME=${APP_NAME}-${pkgVersion}-py3-none-any.whl" >> $GITHUB_OUTPUT
echo "========== GITHUB_ENV ==========="
echo "PYTHON_VERSION=${{ matrix.python-version }}" >> $GITHUB_ENV
echo "VERSION=$pkgVersion" >> $GITHUB_ENV
echo "APP_NAME=$APP_DIR" >> $GITHUB_ENV
echo "APP_PWD=${{ env.APP_PWD }}" >> $GITHUB_ENV
echo "APP_DIR=$APP_DIR" >> $GITHUB_ENV
echo "PKG_NAME=${APP_NAME}-${pkgVersion}" >> $GITHUB_ENV
echo "WHL_NAME=${APP_NAME}-${pkgVersion}-py3-none-any.whl" >> $GITHUB_ENV
- name: Get Source Branch
id: get_source_branch
working-directory: ${{ env.APP_PWD }}
run: |
sourceBranch="$(git branch --contains ${{ github.sha }} | grep -E 'feature|staging' | sed 's/[ *]//g')"
echo "Source branch: $sourceBranch"
echo "SOURCE_BRANCH=$sourceBranch" >> $GITHUB_OUTPUT
- name: Set Package Version
id: set_package_version
working-directory: ${{ env.APP_PWD }}
run: |
sed -E -i "s/^__version__ = \"[a-z0-9\.]+\"/__version__ = \"${{ env.VERSION }}\"/g" "${{ env.APP_DIR }}/_version.py"
cat "${{ env.APP_DIR }}/_version.py"
- name: Setup Python ${{ matrix.python-version }}
id: setup_python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
id: install_dependencies
working-directory: ${{ env.APP_PWD }}
run: |
python -m pip install --upgrade pip
pip install flake8 wheel pytest -r requirements.txt
- name: Lint with flake8
id: lint_with_flake8
working-directory: ${{ env.APP_PWD }}
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --ignore=E501,F841
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Create wheel Package
id: create_whl
working-directory: ${{ env.APP_PWD }}
run: python setup.py bdist_wheel
- name: Install ${{ env.PKG_NAME }}
id: install_whl
working-directory: ${{ env.APP_PWD }}
run: pip install "dist/${{ env.WHL_NAME }}"
- name: Copy wheel
id: copy_whl
working-directory: ${{ env.APP_PWD }}
run: |
mkdir "dist/${{ env.PYTHON_VERSION }}"
cp "dist/${{ env.WHL_NAME }}" "dist/${{ env.PYTHON_VERSION }}/"
- name: Cache wheel
id: cache_whl
uses: actions/cache@v3
with:
path: ${{ env.APP_PWD }}/dist/${{ env.PYTHON_VERSION }}/${{ env.WHL_NAME }}
key: ${{ env.PYTHON_VERSION }}_${{ env.APP_DIR }}_${{ github.run_id }}
publish-release:
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
needs: [build-and-test]
runs-on: ubuntu-latest
steps:
- name: Get Environment Variables
id: get_env_vars
run: |
echo "MIN_PY_VER=${{ needs.build-and-test.outputs.min_py_ver }}" >> $GITHUB_ENV
echo "APP_PWD=${{ needs.build-and-test.outputs.app_pwd }}" >> $GITHUB_ENV
echo "APP_DIR=${{ needs.build-and-test.outputs.app_dir }}" >> $GITHUB_ENV
echo "VERSION=${{ needs.build-and-test.outputs.version }}" >> $GITHUB_ENV
echo "WHL_NAME=${{ needs.build-and-test.outputs.whl_name }}" >> $GITHUB_ENV
echo "SOURCE_BRANCH=${{ needs.build-and-test.outputs.source_branch }}" >> $GITHUB_ENV
- name: Restore whl
id: restore_whl
uses: actions/cache@v3
with:
path: ${{ env.APP_PWD }}/dist/${{ env.MIN_PY_VER }}/${{ env.WHL_NAME }}
key: ${{ env.MIN_PY_VER }}_${{ env.APP_DIR }}_${{ github.run_id }}
- name: Publish to PyPI
id: publish_to_pypi
uses: pypa/gh-action-pypi-publish@release/v1
with:
skip-existing: true
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
packages-dir: dist/${{ needs.build-and-test.outputs.min_py_ver }}
print-hash: true