Skip to content

Commit

Permalink
Support auto-versioning (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi committed Apr 22, 2024
1 parent 5ee6b56 commit 8d9d858
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 27 deletions.
39 changes: 37 additions & 2 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,55 @@ name: CI/CD
- master

jobs:
yml-md-style-and-link-checks:
uses: QubitPi/hashicorp-aws/.github/workflows/yml-md-style-and-link-checks.yml@master

tests:
name: Run all tests
needs: yml-md-style-and-link-checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Run tests
run: |
python setup.py pep8
python setup.py isort
- name: Test dist build
run: python setup.py sdist

push-release-tag:
needs: tests
if: github.ref == 'refs/heads/master'
uses: QubitPi/hashicorp-aws/.github/workflows/version-bump.yml@master
with:
user: QubitPi
email: jack20220723@gmail.com

release:
name: Publish Python SDK To PyPI
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Bump version
run: |
git pull
VERSION=$(git describe)
echo "INFO Last tag: $VERSION"
cat setup.py | sed -i "s/ version=\"1.0.0\",/ version=\"$NEW_VERSION\",/"
- name: Package up SDK
if: github.ref == 'refs/heads/master'
run: python setup.py sdist
- name: Publish a Python distribution to PyPI
if: github.ref == 'refs/heads/master'
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ sphinx:

python:
install:
- requirements: docs/source/requirements.txt
- requirements: docs/source/requirements.txt
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Screwdriver CD Python SDK
=========================

![Python Version][Python Version Badge]
[![Read the Docs][Read the Docs badge]][Read the Docs URL]
[![PyPI][PyPI project badge]][PyPI project url]
[![GitHub Workflow Status][GitHub Workflow Status badge]][GitHub Workflow Status URL]
[![Apache License badge]][Apache License URL]
Expand Down Expand Up @@ -39,3 +40,6 @@ The use and distribution terms for Screwdriver CD Python SDK are covered by the
[Python Version Badge]: https://img.shields.io/badge/Python-3.10-brightgreen?style=for-the-badge&logo=python&logoColor=white
[PyPI project badge]: https://img.shields.io/pypi/v/screwdriver-cd-python-sdk?logo=pypi&logoColor=white&style=for-the-badge
[PyPI project url]: https://pypi.org/project/screwdriver-cd-python-sdk/

[Read the Docs badge]: https://img.shields.io/readthedocs/screwdriver-cd-python-sdk?style=for-the-badge&logo=readthedocs&logoColor=white&label=Read%20the%20Docs&labelColor=8CA1AF
[Read the Docs URL]: https://screwdriver-cd-python-sdk.readthedocs.io/en/latest/
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
import os
import sys

sys.path.insert(0, os.path.abspath('../../'))


Expand Down
5 changes: 2 additions & 3 deletions docs/source/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ Frequently Asked Questions
Python Sphinx Autodoc Is Not Rendering on readthedocs
=====================================================

The project's dependencies are not specified on RTD, but instead have installed the dependencies locally. We can verify
this in the `build log <https://readthedocs.org/api/v2/build/17857935.txt>`_. Visit the project's Builds, click a build,
and click "view raw"::
The project's dependencies are not specified on RTD, but instead have installed the dependencies locally. Visit the
project's Builds, click a build, and click "view raw"::

WARNING: autodoc: failed to import module 'rnn' from module 'screwdriver_cd_python_sdk'; the following exception was raised:
No module named 'matplotlib'
Expand Down
12 changes: 9 additions & 3 deletions screwdriver_cd_python_sdk/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

import requests
import sys, os


def start_build(pipeline_id: int, screwdriver_api_url: str, token: str) -> object:
"""
Creates and starts a pipeline build.
:param pipeline_id: The ID of the pipeline to trigger the build
:param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or https://mysd.com
:param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or
https://mysd.com
:param token: The Screwdriver API token
:return: The exact same response body as the "POST /v4/events" Swagger API in JSON
Expand All @@ -35,13 +39,15 @@ def start_build(pipeline_id: int, screwdriver_api_url: str, token: str) -> objec
token
)


def create_and_start_event(screwdriver_api_url: str, body: object, token: str) -> object:
"""
Creates and starts a specific event.
If an error occurs, this function returns nothing but throws the causing error.
:param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or https://mysd.com
:param screwdriver_api_url: The URL of the Screwdriver API server. For example: http://192.168.7.2:9001 or
https://mysd.com
:param body: The exact same body as the one used in "POST /v4/events" Swagger API
:param token: The Screwdriver API token
Expand Down
6 changes: 5 additions & 1 deletion screwdriver_cd_python_sdk/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# limitations under the License.

import logging
import os
import sys

import requests
import sys, os


def search_pipelines_by_name(name: str, screwdriver_api_url: str, token: str) -> list[object]:
"""
Expand Down Expand Up @@ -89,6 +92,7 @@ def search_pipelines_by_name(name: str, screwdriver_api_url: str, token: str) ->

return response.json()


def create_pipeline(checkout_url: str, screwdriver_api_url: str, token: str, source_directory: object = None) -> object:
"""
Creates a new Screwdriver pipeline for a particular repo and an optional source directory.
Expand Down
25 changes: 15 additions & 10 deletions screwdriver_cd_python_sdk/screwdriver_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import json
import csv
from screwdriver_cd_python_sdk.pipeline import create_pipeline
from screwdriver_cd_python_sdk.pipeline import search_pipelines_by_name
from screwdriver_cd_python_sdk.secrets import create_or_update_secret
import json
import logging

from screwdriver_cd_python_sdk.events import start_build
from screwdriver_cd_python_sdk.pipeline import (create_pipeline,
search_pipelines_by_name)
from screwdriver_cd_python_sdk.secrets import create_or_update_secret


def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str) -> None:
with open(pipelines_config_path, 'r') as file:
Expand All @@ -28,9 +29,9 @@ def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str)
for pipeline in pipelines:

git_url = pipeline["git"]
repo_name = git_url[git_url.find(":")+1 : git_url.find(".git")]
repo_name = git_url[git_url.find(":") + 1:git_url.find(".git")]

pipeline_id=None
pipeline_id = None
for match in search_pipelines_by_name(name=repo_name, screwdriver_api_url=screwdriver_api_url, token=token):
if match["name"] == repo_name:
pipeline_id = match["id"]
Expand All @@ -40,7 +41,11 @@ def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str)

if pipeline_id is None:
logging.debug("Creating {}...".format(repo_name))
pipeline_id = create_pipeline(checkout_url=pipeline["git"], screwdriver_api_url=screwdriver_api_url, token=token)["id"]
pipeline_id = create_pipeline(
checkout_url=pipeline["git"],
screwdriver_api_url=screwdriver_api_url,
token=token
)["id"]

if "awsCredentialFile" in pipeline:
with open(pipeline["awsCredentialFile"], 'r') as file:
Expand Down Expand Up @@ -86,4 +91,4 @@ def initialize(pipelines_config_path: str, screwdriver_api_url: str, token: str)

def _file_content(file_path: str) -> str:
with open(file_path, 'r') as file:
return file.read().rstrip('\n') # https://stackoverflow.com/a/70233945
return file.read().rstrip('\n') # https://stackoverflow.com/a/70233945
23 changes: 19 additions & 4 deletions screwdriver_cd_python_sdk/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# limitations under the License.

import logging
import os
import sys

import requests
import sys, os

logging.basicConfig(level=logging.DEBUG)


def _headers(token: str) -> object:
return {
'accept': 'application/json',
Expand All @@ -26,7 +29,13 @@ def _headers(token: str) -> object:
}


def create_or_update_secret(secret_name: str, secret_value: str, pipeline_id: int, screwdriver_api_url: str, token: str) -> None:
def create_or_update_secret(
secret_name: str,
secret_value: str,
pipeline_id: int,
screwdriver_api_url: str,
token: str
) -> None:
"""
"allowInPR" is set to be false by default
Expand All @@ -53,7 +62,11 @@ def create_or_update_secret(secret_name: str, secret_value: str, pipeline_id: in
'allowInPR': False,
}

if requests.put('{}/v4/secrets/{}'.format(screwdriver_api_url, secrete["id"]), headers=_headers(token), json=json_data).status_code != 200:
if requests.put(
'{}/v4/secrets/{}'.format(screwdriver_api_url, secrete["id"]),
headers=_headers(token),
json=json_data
).status_code != 200:
sys.exit(os.EX_CONFIG)
else:
logging.debug("Creating secret '{}'".format(secret_name))
Expand All @@ -65,5 +78,7 @@ def create_or_update_secret(secret_name: str, secret_value: str, pipeline_id: in
'allowInPR': False,
}

if requests.post('{}/v4/secrets'.format(screwdriver_api_url), headers=_headers(token), json=json_data).status_code != 201:
if requests.post(
'{}/v4/secrets'.format(screwdriver_api_url), headers=_headers(token), json=json_data
).status_code != 201:
sys.exit(os.EX_CONFIG)
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pep8]
max-line-length = 120
exclude = ./.eggs
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from setuptools import setup, find_packages
from setuptools import find_packages, setup

setup(
name="screwdriver-cd-python-sdk",
version="1.0.0",
description="Screwdriver CD Python Software Development Kit (SDK) used to write Python automation scripts that create and manage resources in Screwdriver",
description="Screwdriver CD Python Software Development Kit (SDK) for managing resources in Screwdriver",
url="https://github.com/QubitPi/screwdriver-cd-python-sdk",
author="Jiaqi liu",
author_email="jack20220723@gmail.com",
Expand All @@ -14,5 +14,6 @@

],
zip_safe=False,
include_package_data=True
include_package_data=True,
setup_requires=["setuptools-pep8", "isort"]
)

0 comments on commit 8d9d858

Please sign in to comment.