-
Notifications
You must be signed in to change notification settings - Fork 0
116 lines (107 loc) · 4.92 KB
/
python_pipeline.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
name: Release Python Package
on:
workflow_call:
secrets:
PYPI_API_TOKEN:
required: true
jobs:
publish:
runs-on: self-hosted
permissions: write-all
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Verify Latest Build Version
id: verify_version
run: |
echo "Fetching Latest Tags"
git fetch --tags
git reset --hard origin/main
git pull
echo "Pulled latest repo changes"
REPO_NAME=$(echo ${{ github.event.repository.name }})
echo "Repository Name: ${REPO_NAME}"
FOLDER_NAME=$(echo ${{ github.event.repository.name }} | sed 's/-/_/g')
echo "Folder Name: ${FOLDER_NAME}"
CURRENT_VERSION=$(cat ${FOLDER_NAME}/version.py | grep -o '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*')
echo "Current Version: ${CURRENT_VERSION}"
TAG=$(git tag -l | sort -V | tail -n1)
echo "Tag: ${TAG}"
LATEST_COMMIT=$(git rev-parse --verify HEAD)
echo "Last Commit: ${LATEST_COMMIT}"
if [[ $TAG != '' ]]; then
CURRENT_COMMIT=$(git rev-list -n 1 $TAG)
CURRENT_RELEASE=$(git tag -l | sort -V | tail -n1 | sed 's/v//')
else
TAG=''
CURRENT_COMMIT=${LATEST_COMMIT}
CURRENT_RELEASE=${CURRENT_VERSION}
fi
COMMIT_DIFFERENCE=$(git log ${CURRENT_COMMIT}..${LATEST_COMMIT} --pretty=oneline | wc -l)
echo "Commit Difference: ${COMMIT_DIFFERENCE}"
COMMIT_MESSAGE=$(git log --format=%B -n $COMMIT_DIFFERENCE $LATEST_COMMIT | tr '\n' ' ')
COMMIT_DATETIME=$(date "+%m/%d/%Y %H:%M:%S")
echo -e "COMMIT_DATETIME=$COMMIT_DATETIME\nTAG=$TAG\nREPO_NAME=$REPO_NAME\nFOLDER_NAME=$FOLDER_NAME\nCURRENT_RELEASE=$CURRENT_RELEASE\nCURRENT_VERSION=$CURRENT_VERSION\nLATEST_COMMIT=$LATEST_COMMIT\nCURRENT_COMMIT=$CURRENT_COMMIT\nCOMMIT_DIFFERENCE=$COMMIT_DIFFERENCE\nCOMMIT_MESSAGE=$COMMIT_MESSAGE" | tee -a $GITHUB_ENV
- name: Increment Version
if: ${{ env.CURRENT_RELEASE <= env.CURRENT_VERSION }}
id: increment_version
run: |
export PATH=$PATH:~/.local/bin
python -m pip install --upgrade autoversioner
autoversioner -v "${CURRENT_VERSION}" | tee -a VERSION
COMMIT_MESSAGE=$(echo "${{ github.event.head_commit.message }}" | tr -d '\n')
echo "Commit Message: $COMMIT_MESSAGE"
NEW_VERSION="$(cat VERSION)"
rm -rf ./VERSION
echo -e "Old Version: $CURRENT_RELEASE\nNew Version: $NEW_VERSION"
sed -i "s/CURRENT_VERSION=.*$/CURRENT_VERSION=$NEW_VERSION/" $GITHUB_ENV
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
sed -i "s/__version__ = .*$/__version__ = \"$NEW_VERSION\"/" $FOLDER_NAME/version.py
sed -i "s/^\*Version: [0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*\*.*$/*Version: $NEW_VERSION*/" README.md
- name: Commit Repository Changes
if: ${{ env.CURRENT_RELEASE <= env.CURRENT_VERSION && env.CURRENT_RELEASE != '' }}
id: commit_repository
run: |
git config --global user.name 'GitHub Pipeline'
git config --global user.email 'GitHub@users.noreply.github.com'
git add -A
git commit -am "Automated Version Increment to $NEW_VERSION"
git push origin main --force
- name: Build package
if: ${{ env.CURRENT_RELEASE <= env.CURRENT_VERSION && env.CURRENT_RELEASE != '' }}
id: build_package
run: |
echo "Installing dependencies"
python -m pip install -r requirements.txt
echo "Building package"
python setup.py bdist_wheel --universal
BUILD_VERSION=$(ls dist | grep -o '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*')
echo "BUILD VERSION: $BUILD_VERSION"
echo -e "BUILD_VERSION=$BUILD_VERSION" | tee -a $GITHUB_ENV
- name: Upload Artifact
if: ${{ env.CURRENT_RELEASE <= env.CURRENT_VERSION && env.CURRENT_RELEASE != '' }}
id: upload_artifact
uses: actions/upload-artifact@v3
with:
name: ${{ github.event.repository.name }}-${{ env.VERSION }}
path: dist/${{ github.event.repository.name }}*.whl
- name: Create Release
if: ${{ env.CURRENT_RELEASE <= env.CURRENT_VERSION && env.CURRENT_RELEASE != '' }}
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ env.BUILD_VERSION }}
release_name: Release v${{ env.BUILD_VERSION }}
body: |
Released: ${{ env.COMMIT_DATETIME }}
Commit: ${{ env.LATEST_COMMIT }}
Changes:
${{ env.COMMIT_MESSAGE }}
draft: false
prerelease: false
- name: Publish package
if: ${{ env.CURRENT_RELEASE <= env.CURRENT_VERSION && env.CURRENT_RELEASE != '' }}
run: |
twine upload dist/* --verbose -u "__token__" -p "${{ secrets.PYPI_API_TOKEN }}"