Skip to content

Commit

Permalink
Merge pull request #173 from eamigo86/actions
Browse files Browse the repository at this point in the history
Implementing GitHub Actions
  • Loading branch information
eamigo86 authored Jun 30, 2021
2 parents 07f8d1a + 7ca14c5 commit 6423175
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 65 deletions.
149 changes: 145 additions & 4 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,153 @@
name: WIP
name: Continuous Integration & Delivery
on:
pull_request:
branches: [ master ]
workflow_dispatch:
branches: [ master, actions ]
jobs:
demo:
test:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- python: 3.6
django: 2.2
toxenv: py36-django22
- python: 3.6
django: 3.0
toxenv: py36-django30
- python: 3.7
django: 2.2
toxenv: py37-django22
- python: 3.7
django: 3.0
toxenv: py37-django30
- python: 3.8
django: 2.2
toxenv: py38-django22
- python: 3.8
django: 3.0
toxenv: py38-django30
- python: 3.9
django: 2.2
toxenv: py39-django22
- python: 3.9
django: 3.0
toxenv: py39-django30
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
- uses: actions/setup-python@v1
name: Set up Python ${{ matrix.python }} ${{ matrix.django }}
with:
python-version: ${{ matrix.python }}
- name: Install pip packages
run: |
echo Hello, world 1!
pip install pip --upgrade
pip install codecov poetry tox
- name: "Run tox - testenv: ${{ matrix.toxenv }}"
env:
DJANGO: ${{ matrix.django }}
TOXENV: ${{ matrix.toxenv }}
run: tox
- name: Run notification script
env:
GITHUB_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
GITHUB_JOB_STATUS: ${{ job.status }}
GITHUB_RUN_URL: ${{ github.event.repository.url }}/actions/runs/${{ github.run_id }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
run: ./scripts/notification.sh

quality:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- python: 3.6
toxenv: quality
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
name: Set up Python ${{ matrix.python }}
with:
python-version: ${{ matrix.python }}
- name: Install pip packages
run: |
pip install pip --upgrade
pip install poetry tox
- name: "Run tox - testenv: ${{ matrix.toxenv }}"
env:
TOXENV: ${{ matrix.toxenv }}
run: tox

security:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- python: 3.6
toxenv: security
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1
name: Set up Python ${{ matrix.python }}
with:
python-version: ${{ matrix.python }}
- name: Install pip packages
run: |
pip install pip --upgrade
pip install poetry tox
- name: "Run tox - testenv: ${{ matrix.toxenv }}"
env:
TOXENV: ${{ matrix.toxenv }}
run: tox

# # AUTHOR: Lukasz Dynowski
# # TODO: Uncomment this code only if we agreed for Continuous Delivery.
# # TODO: Update PYPI envars to production values!
# publish:
# needs:
# - test
# - security
# - quality
# runs-on: ubuntu-latest
# strategy:
# matrix:
# include:
# - python: 3.6
# toxenv: build
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-python@v1
# name: Set up Python ${{ matrix.python }}
# with:
# python-version: ${{ matrix.python }}
# - name: Install pip packages
# run: |
# pip install pip --upgrade
# pip install poetry tox
# - name: Bump package version
# run: ./scripts/bump.py --dry-run no
# - name: "Build package - testenv: ${{ matrix.toxenv }}"
# env:
# TOXENV: ${{ matrix.toxenv }}
# run: tox
# - name: "Publish - testenv: ${{ matrix.toxenv }}"
# env:
# PYPI_ACCESS_TOKEN: ${{ secrets.PYPI_ACCESS_TOKEN }}
# # Values production 'https://pypi.org/', testing 'https://test.pypi.org/legacy/'
# PYPI_REPOSITORY_URL: https://test.pypi.org/legacy/
# # Values production 'pypi', testing 'testpypi'
# PYPI_REPOSITORY: testpypi
# PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
# run: |
# poetry config repositories.$PYPI_REPOSITORY $PYPI_REPOSITORY_URL
# poetry config pypi-token.$PYPI_REPOSITORY $PYPI_ACCESS_TOKEN
# poetry publish -r $PYPI_REPOSITORY -u $PYPI_USERNAME
# - name: Create bump commit
# uses: EndBug/add-and-commit@latest
# with:
# author_name: github_actions
# author_email: github_actions
# message: PR Auto bumping package version
# add: pyproject.toml
46 changes: 0 additions & 46 deletions .travis.yml

This file was deleted.

135 changes: 135 additions & 0 deletions scripts/bump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env python3

from pathlib import Path
import argparse
import re
import toml


def get_args():
"""Set up CLI args"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--major",
type=int,
nargs="*",
help="Major version bump. Empty flag will auto bump current version.",
)
parser.add_argument(
"--minor",
type=int,
nargs="*",
help="Minor version bump. Empty flag will auto bump current version.",
)
parser.add_argument(
"--patch",
type=int,
nargs="*",
help="Patch version bump. Empty flag will auto bump current version.",
)
parser.add_argument(
"--dry-run", type=str, default="yes", choices=["yes", "no"], help="Dry run"
)

return parser.parse_args()


def get_version(pyproject_file_path):
"""Function returns tuple that elements follow semantic versioning order"""
with open(pyproject_file_path) as file:
pyproject = toml.loads(file.read())
current_version = pyproject["tool"]["poetry"]["version"]

validate_version(current_version)

# Normalize version to tuple
if current_version.count(".") == 0:
return tuple(int(current_version))

return tuple(int(v) for v in current_version.split("."))


def validate_version(current_version, pattern=r"^\d+\.\d+\.\d+$"):
"""
Validate that extracted version follows "MAJOR.MINOR.PATCH" pattern
"""
match = re.search(pattern, current_version)
if not match:
print(
f'Error: Package version {current_version} is not following semantic versioning "MAJOR.MINOR.PATCH"'
)
exit(1)


def bump_major(version):
if not version:
return CURRENT_VERSION[0] + 1

return version[0]


def bump_minor(version):
if not version:
return CURRENT_VERSION[1] + 1

return version[0]


def bump_patch(version):
if not version:
return CURRENT_VERSION[2] + 1

return version[0]


def bump_version(major, minor, patch):
if major or type(major) == list:
major = bump_major(major)

if minor or type(minor) == list:
minor = bump_minor(minor)

if (type(patch) == list) or all(v is None for v in (major, minor, patch)):
patch = bump_patch(patch)

# Construct bump from new version and current version
bump = []
new_versions = (major, minor, patch)
for index in range(len(new_versions)):
if new_versions[index] is None:
bump.append(CURRENT_VERSION[index])
else:
bump.append(new_versions[index])

return tuple(bump)


if __name__ == "__main__":
args = get_args()

# Obtain 'pyproject.toml' file path
current_file_path = str(Path(__file__).resolve())
pyproject_file_path = current_file_path.replace(current_file_path, "pyproject.toml")

# Bump and normalize current version
CURRENT_VERSION = get_version(pyproject_file_path)
NEW_VERSION = bump_version(args.major, args.minor, args.patch)
CURRENT_VERSION = ".".join(map(str, CURRENT_VERSION))
NEW_VERSION = ".".join(map(str, NEW_VERSION))

# Print version check
if args.dry_run == "yes":
print(f"Current version: {CURRENT_VERSION}")
print(f"New version: {NEW_VERSION}")
else:
with open(pyproject_file_path, "r") as file:
content = file.read()

with open(pyproject_file_path, "w") as file:
CV = f'version = "{CURRENT_VERSION}"'
NV = f'version = "{NEW_VERSION}"'
content = content.replace(CV, NV)
file.write(content)
print(
f"Successfully updated package version from {CURRENT_VERSION} to {NEW_VERSION}"
)
24 changes: 10 additions & 14 deletions scripts/travis_telegram_notification.sh → scripts/notification.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@ BOT_URL="https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage"
# Set formatting for the message. Can be either "Markdown" or "HTML"
PARSE_MODE="Markdown"

# Use built-in Travis variables to check if all previous steps passed:
if [ $TRAVIS_TEST_RESULT -ne 0 ]; then
build_status="failed"
else
build_status="succeeded"
fi

# Define send message function. parse_mode can be changed to
# HTML, depending on how you want to format your message:
send_msg () {
curl -s -X POST ${BOT_URL} -d chat_id=$TELEGRAM_CHAT_ID \
-d text="$1" -d parse_mode=${PARSE_MODE}
curl -s -X POST ${BOT_URL} \
-d chat_id=$TELEGRAM_CHAT_ID \
-d text="$1" \
-d parse_mode=${PARSE_MODE}
}

# Send message to the bot with some pertinent details about the job
# Note that for Markdown, you need to escape any backtick (inline-code)
# characters, since they're reserved in bash
send_msg "
----------------------------------------------------
Travis build *${build_status}!*
\`Repository: ${TRAVIS_REPO_SLUG}\`
\`Branch: ${TRAVIS_BRANCH}\`
GitHub Actions build *${GITHUB_JOB_STATUS}!*
\`Repository: ${GITHUB_REPOSITORY}\`
\`Branch: ${GITHUB_REF}\`
\`Environment: ${TOXENV}\`
\`Run Number/Run ID: ${GITHUB_RUN_NUMBER}/${GITHUB_RUN_ID}\`
*Commit Msg:*
${TRAVIS_COMMIT_MESSAGE}
${GITHUB_COMMIT_MESSAGE}
[See complete job log here](${TRAVIS_JOB_WEB_URL})
[See complete job log here](${GITHUB_RUN_URL})
-----------------------------------------------------
"
Loading

0 comments on commit 6423175

Please sign in to comment.