Skip to content

Commit 6423175

Browse files
authored
Merge pull request #173 from eamigo86/actions
Implementing GitHub Actions
2 parents 07f8d1a + 7ca14c5 commit 6423175

File tree

5 files changed

+306
-65
lines changed

5 files changed

+306
-65
lines changed

.github/workflows/cicd.yaml

+145-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,153 @@
1-
name: WIP
1+
name: Continuous Integration & Delivery
22
on:
3+
pull_request:
4+
branches: [ master ]
35
workflow_dispatch:
46
branches: [ master, actions ]
57
jobs:
6-
demo:
8+
test:
79
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
include:
13+
- python: 3.6
14+
django: 2.2
15+
toxenv: py36-django22
16+
- python: 3.6
17+
django: 3.0
18+
toxenv: py36-django30
19+
- python: 3.7
20+
django: 2.2
21+
toxenv: py37-django22
22+
- python: 3.7
23+
django: 3.0
24+
toxenv: py37-django30
25+
- python: 3.8
26+
django: 2.2
27+
toxenv: py38-django22
28+
- python: 3.8
29+
django: 3.0
30+
toxenv: py38-django30
31+
- python: 3.9
32+
django: 2.2
33+
toxenv: py39-django22
34+
- python: 3.9
35+
django: 3.0
36+
toxenv: py39-django30
837
steps:
938
- uses: actions/checkout@v2
10-
- name: Run a one-line script
39+
- uses: actions/setup-python@v1
40+
name: Set up Python ${{ matrix.python }} ${{ matrix.django }}
41+
with:
42+
python-version: ${{ matrix.python }}
43+
- name: Install pip packages
1144
run: |
12-
echo Hello, world 1!
45+
pip install pip --upgrade
46+
pip install codecov poetry tox
47+
- name: "Run tox - testenv: ${{ matrix.toxenv }}"
48+
env:
49+
DJANGO: ${{ matrix.django }}
50+
TOXENV: ${{ matrix.toxenv }}
51+
run: tox
52+
- name: Run notification script
53+
env:
54+
GITHUB_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
55+
GITHUB_JOB_STATUS: ${{ job.status }}
56+
GITHUB_RUN_URL: ${{ github.event.repository.url }}/actions/runs/${{ github.run_id }}
57+
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
58+
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
59+
run: ./scripts/notification.sh
60+
61+
quality:
62+
runs-on: ubuntu-latest
63+
strategy:
64+
matrix:
65+
include:
66+
- python: 3.6
67+
toxenv: quality
68+
steps:
69+
- uses: actions/checkout@v2
70+
- uses: actions/setup-python@v1
71+
name: Set up Python ${{ matrix.python }}
72+
with:
73+
python-version: ${{ matrix.python }}
74+
- name: Install pip packages
75+
run: |
76+
pip install pip --upgrade
77+
pip install poetry tox
78+
- name: "Run tox - testenv: ${{ matrix.toxenv }}"
79+
env:
80+
TOXENV: ${{ matrix.toxenv }}
81+
run: tox
82+
83+
security:
84+
runs-on: ubuntu-latest
85+
strategy:
86+
matrix:
87+
include:
88+
- python: 3.6
89+
toxenv: security
90+
steps:
91+
- uses: actions/checkout@v2
92+
- uses: actions/setup-python@v1
93+
name: Set up Python ${{ matrix.python }}
94+
with:
95+
python-version: ${{ matrix.python }}
96+
- name: Install pip packages
97+
run: |
98+
pip install pip --upgrade
99+
pip install poetry tox
100+
- name: "Run tox - testenv: ${{ matrix.toxenv }}"
101+
env:
102+
TOXENV: ${{ matrix.toxenv }}
103+
run: tox
104+
105+
# # AUTHOR: Lukasz Dynowski
106+
# # TODO: Uncomment this code only if we agreed for Continuous Delivery.
107+
# # TODO: Update PYPI envars to production values!
108+
# publish:
109+
# needs:
110+
# - test
111+
# - security
112+
# - quality
113+
# runs-on: ubuntu-latest
114+
# strategy:
115+
# matrix:
116+
# include:
117+
# - python: 3.6
118+
# toxenv: build
119+
# steps:
120+
# - uses: actions/checkout@v2
121+
# - uses: actions/setup-python@v1
122+
# name: Set up Python ${{ matrix.python }}
123+
# with:
124+
# python-version: ${{ matrix.python }}
125+
# - name: Install pip packages
126+
# run: |
127+
# pip install pip --upgrade
128+
# pip install poetry tox
129+
# - name: Bump package version
130+
# run: ./scripts/bump.py --dry-run no
131+
# - name: "Build package - testenv: ${{ matrix.toxenv }}"
132+
# env:
133+
# TOXENV: ${{ matrix.toxenv }}
134+
# run: tox
135+
# - name: "Publish - testenv: ${{ matrix.toxenv }}"
136+
# env:
137+
# PYPI_ACCESS_TOKEN: ${{ secrets.PYPI_ACCESS_TOKEN }}
138+
# # Values production 'https://pypi.org/', testing 'https://test.pypi.org/legacy/'
139+
# PYPI_REPOSITORY_URL: https://test.pypi.org/legacy/
140+
# # Values production 'pypi', testing 'testpypi'
141+
# PYPI_REPOSITORY: testpypi
142+
# PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
143+
# run: |
144+
# poetry config repositories.$PYPI_REPOSITORY $PYPI_REPOSITORY_URL
145+
# poetry config pypi-token.$PYPI_REPOSITORY $PYPI_ACCESS_TOKEN
146+
# poetry publish -r $PYPI_REPOSITORY -u $PYPI_USERNAME
147+
# - name: Create bump commit
148+
# uses: EndBug/add-and-commit@latest
149+
# with:
150+
# author_name: github_actions
151+
# author_email: github_actions
152+
# message: PR Auto bumping package version
153+
# add: pyproject.toml

.travis.yml

-46
This file was deleted.

scripts/bump.py

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/env python3
2+
3+
from pathlib import Path
4+
import argparse
5+
import re
6+
import toml
7+
8+
9+
def get_args():
10+
"""Set up CLI args"""
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument(
13+
"--major",
14+
type=int,
15+
nargs="*",
16+
help="Major version bump. Empty flag will auto bump current version.",
17+
)
18+
parser.add_argument(
19+
"--minor",
20+
type=int,
21+
nargs="*",
22+
help="Minor version bump. Empty flag will auto bump current version.",
23+
)
24+
parser.add_argument(
25+
"--patch",
26+
type=int,
27+
nargs="*",
28+
help="Patch version bump. Empty flag will auto bump current version.",
29+
)
30+
parser.add_argument(
31+
"--dry-run", type=str, default="yes", choices=["yes", "no"], help="Dry run"
32+
)
33+
34+
return parser.parse_args()
35+
36+
37+
def get_version(pyproject_file_path):
38+
"""Function returns tuple that elements follow semantic versioning order"""
39+
with open(pyproject_file_path) as file:
40+
pyproject = toml.loads(file.read())
41+
current_version = pyproject["tool"]["poetry"]["version"]
42+
43+
validate_version(current_version)
44+
45+
# Normalize version to tuple
46+
if current_version.count(".") == 0:
47+
return tuple(int(current_version))
48+
49+
return tuple(int(v) for v in current_version.split("."))
50+
51+
52+
def validate_version(current_version, pattern=r"^\d+\.\d+\.\d+$"):
53+
"""
54+
Validate that extracted version follows "MAJOR.MINOR.PATCH" pattern
55+
"""
56+
match = re.search(pattern, current_version)
57+
if not match:
58+
print(
59+
f'Error: Package version {current_version} is not following semantic versioning "MAJOR.MINOR.PATCH"'
60+
)
61+
exit(1)
62+
63+
64+
def bump_major(version):
65+
if not version:
66+
return CURRENT_VERSION[0] + 1
67+
68+
return version[0]
69+
70+
71+
def bump_minor(version):
72+
if not version:
73+
return CURRENT_VERSION[1] + 1
74+
75+
return version[0]
76+
77+
78+
def bump_patch(version):
79+
if not version:
80+
return CURRENT_VERSION[2] + 1
81+
82+
return version[0]
83+
84+
85+
def bump_version(major, minor, patch):
86+
if major or type(major) == list:
87+
major = bump_major(major)
88+
89+
if minor or type(minor) == list:
90+
minor = bump_minor(minor)
91+
92+
if (type(patch) == list) or all(v is None for v in (major, minor, patch)):
93+
patch = bump_patch(patch)
94+
95+
# Construct bump from new version and current version
96+
bump = []
97+
new_versions = (major, minor, patch)
98+
for index in range(len(new_versions)):
99+
if new_versions[index] is None:
100+
bump.append(CURRENT_VERSION[index])
101+
else:
102+
bump.append(new_versions[index])
103+
104+
return tuple(bump)
105+
106+
107+
if __name__ == "__main__":
108+
args = get_args()
109+
110+
# Obtain 'pyproject.toml' file path
111+
current_file_path = str(Path(__file__).resolve())
112+
pyproject_file_path = current_file_path.replace(current_file_path, "pyproject.toml")
113+
114+
# Bump and normalize current version
115+
CURRENT_VERSION = get_version(pyproject_file_path)
116+
NEW_VERSION = bump_version(args.major, args.minor, args.patch)
117+
CURRENT_VERSION = ".".join(map(str, CURRENT_VERSION))
118+
NEW_VERSION = ".".join(map(str, NEW_VERSION))
119+
120+
# Print version check
121+
if args.dry_run == "yes":
122+
print(f"Current version: {CURRENT_VERSION}")
123+
print(f"New version: {NEW_VERSION}")
124+
else:
125+
with open(pyproject_file_path, "r") as file:
126+
content = file.read()
127+
128+
with open(pyproject_file_path, "w") as file:
129+
CV = f'version = "{CURRENT_VERSION}"'
130+
NV = f'version = "{NEW_VERSION}"'
131+
content = content.replace(CV, NV)
132+
file.write(content)
133+
print(
134+
f"Successfully updated package version from {CURRENT_VERSION} to {NEW_VERSION}"
135+
)

scripts/travis_telegram_notification.sh scripts/notification.sh

+10-14
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@ BOT_URL="https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage"
66
# Set formatting for the message. Can be either "Markdown" or "HTML"
77
PARSE_MODE="Markdown"
88

9-
# Use built-in Travis variables to check if all previous steps passed:
10-
if [ $TRAVIS_TEST_RESULT -ne 0 ]; then
11-
build_status="failed"
12-
else
13-
build_status="succeeded"
14-
fi
15-
169
# Define send message function. parse_mode can be changed to
1710
# HTML, depending on how you want to format your message:
1811
send_msg () {
19-
curl -s -X POST ${BOT_URL} -d chat_id=$TELEGRAM_CHAT_ID \
20-
-d text="$1" -d parse_mode=${PARSE_MODE}
12+
curl -s -X POST ${BOT_URL} \
13+
-d chat_id=$TELEGRAM_CHAT_ID \
14+
-d text="$1" \
15+
-d parse_mode=${PARSE_MODE}
2116
}
2217

2318
# Send message to the bot with some pertinent details about the job
2419
# Note that for Markdown, you need to escape any backtick (inline-code)
2520
# characters, since they're reserved in bash
2621
send_msg "
2722
----------------------------------------------------
28-
Travis build *${build_status}!*
29-
\`Repository: ${TRAVIS_REPO_SLUG}\`
30-
\`Branch: ${TRAVIS_BRANCH}\`
23+
GitHub Actions build *${GITHUB_JOB_STATUS}!*
24+
\`Repository: ${GITHUB_REPOSITORY}\`
25+
\`Branch: ${GITHUB_REF}\`
3126
\`Environment: ${TOXENV}\`
27+
\`Run Number/Run ID: ${GITHUB_RUN_NUMBER}/${GITHUB_RUN_ID}\`
3228
*Commit Msg:*
33-
${TRAVIS_COMMIT_MESSAGE}
29+
${GITHUB_COMMIT_MESSAGE}
3430
35-
[See complete job log here](${TRAVIS_JOB_WEB_URL})
31+
[See complete job log here](${GITHUB_RUN_URL})
3632
-----------------------------------------------------
3733
"

0 commit comments

Comments
 (0)