Skip to content

Commit

Permalink
Update travis configuration to release a new version from CI/CD pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ubajze authored May 12, 2021
1 parent 586022c commit a542a61
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 8 deletions.
48 changes: 45 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
---
# Add additional stages in the order of execution here, and then under the job:include: key
stages:
- "lint"
- "test"
- name: "lint"
- name: "test"
- name: "verify_pypi_release"
if: "tag IS present"
- name: "deploy_github"
if: "tag IS present"
- name: "deploy_pypi"
if: "tag IS present"

if: "type IN (pull_request)" # Add in "branch" as an option if desired for branch testing as well
language: "python"

env:
Expand Down Expand Up @@ -40,3 +45,40 @@ jobs:
- "invoke flake8"
- "invoke yamllint"
- "invoke pylint"
- stage: "verify_pypi_release"
before_script:
- "pip install --upgrade pip"
- "pip install invoke poetry toml"
- "poetry config virtualenvs.create false"
- "poetry install --no-interaction"
script:
- "invoke check-pypi-version"
- stage: "deploy_github"
before_script:
- "pip install --upgrade pip"
- "pip install poetry"
script:
- "echo Deploying the release to GitHub"
- "poetry version $TRAVIS_TAG"
- "poetry build"
deploy:
provider: "releases"
api_key: "$GITHUB_AUTH_TOKEN"
file_glob: true
file: "dist/*"
skip_cleanup: true
"on":
all_branches: true
- stage: "deploy_pypi"
before_script:
- "pip install --upgrade pip"
- "pip install poetry"
script:
- "echo Deploying the release to PyPI"
- "poetry version $TRAVIS_TAG"
deploy:
provider: "script"
skip_cleanup: true
script: "poetry publish --build -u __token__ -p $PYPI_TOKEN"
"on":
all_branches: true
2 changes: 1 addition & 1 deletion netutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"variables",
"vlan",
]
__version__ = "0.1.1"
__version__ = "0.1.2"
77 changes: 76 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "netutils"
version = "0.1.1"
version = "0.1.2"
description = "Common helper functions useful in network automation."
authors = ["Network to Code, LLC <[email protected]>"]

Expand All @@ -19,6 +19,7 @@ invoke = "^1.4.1"
toml = "0.10.1"
flake8 = "^3.8.3"
coverage = "^5.5"
requests = "^2.25.1"

[tool.black]
line-length = 120
Expand Down
34 changes: 34 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
from distutils.util import strtobool
import requests
from invoke import task

try:
Expand Down Expand Up @@ -39,6 +40,10 @@ def is_truthy(arg):
PWD = os.getcwd()
# Local or Docker execution provide "local" to run locally without docker execution
INVOKE_LOCAL = is_truthy(os.getenv("INVOKE_LOCAL", False)) # pylint: disable=W1508
# Get project name from the toml file
PROJECT_NAME = PYPROJECT_CONFIG["tool"]["poetry"]["name"]
# Get current project version from the toml file
PROJECT_VERSION = PYPROJECT_CONFIG["tool"]["poetry"]["version"]


def run_cmd(context, exec_cmd, local=INVOKE_LOCAL):
Expand Down Expand Up @@ -234,3 +239,32 @@ def tests(context, local=INVOKE_LOCAL):
coverage(context, local)

print("All tests have passed!")


@task
def check_pypi_version(context, name=PROJECT_NAME, version=PROJECT_VERSION):
"""Verify if the version specified already exists on PyPI.
Used mostly in CI/CD to make sure that the new version is merged to main.
If version already exists, then function exits with non-zero return code,
else the function exits with zero return code.
Args:
context (obj): Used to run specific commands
name (str): The name of the project
version (str): The version of the project
"""
# Running the following from context to pass pylint:
# context must be the first argument in invoke
context.run(f"echo Verifying the version {version} on PyPI.")

url = f"https://pypi.org/pypi/{name}/json"
response = requests.get(url)
data = response.json()
if version in data.get("releases", {}).keys():
print(f"The version {version} already exists.")
print("Bump the version. Run the command: poetry version.")
sys.exit(1)
print(f"The version {version} does not exist on PyPI.")
print("The version can be released.")
sys.exit(0)
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 0 additions & 1 deletion tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

0 comments on commit a542a61

Please sign in to comment.