Compare commits

..

5 commits

Author SHA1 Message Date
d228660900 Add CI badge to README
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-05-02 16:15:10 +02:00
9813621c3c Add CI configuration for pylint & flake8 tests 2022-05-02 16:14:11 +02:00
b0ecbb6fbc Add README 2022-05-02 16:12:52 +02:00
40db466c57 Fix some pylint warnings 2022-05-02 16:12:37 +02:00
909c726c75 Fix snapshot name formating 2022-05-02 16:12:12 +02:00
4 changed files with 67 additions and 8 deletions

3
.pylintrc Normal file
View file

@ -0,0 +1,3 @@
[MESSAGES CONTROL]
disable=locally-disabled,
redefined-outer-name,

13
.woodpecker.yml Normal file
View file

@ -0,0 +1,13 @@
pipeline:
test-pylint:
group: test
image: pipelinecomponents/pylint
commands:
- python3 -m pip install debian-parser
- pylint entrypoint.py
test-flake8:
group: test
image: pipelinecomponents/flake8
commands:
- flake8 entrypoint.py

38
README.md Normal file
View file

@ -0,0 +1,38 @@
# Woodpecker CI plugin to publish Debian package on a Aptly repository
This docker image could be used as an Woodpecker CI plugin to publish one (or more) Debian package on a Aptly repository using its API.
This plugin will try to :
- locate all `changes` files in the `dist` directory (that matched `source_name` if provided)
- extract files that are mentioned by the changes files and upload it using Aptly files API. Files are published on a subdirectory of the name of the source package
- include all processed changes files using Aplty repos API
- create a snapshot of the repository named using current datetime and repository name (format: `YYYYMMDD-HHMMSS-repo`) using Aptly snapshot API
- update the published snapshot of the repository using Aptly publish API
[![status-badge](https://ci.zionetrix.net/api/badges/bn8/aptly-publish/status.svg)](https://ci.zionetrix.net/bn8/aptly-publish)
# Usage
The below pipeline configuration demonstrates simple usage:
```
pipeline:
publish:
image: brenard/aptly-publish
settings:
api_url: https://your.aptly.tld/api
api_username: myproject
api_password:
from_secret: aptly_api_password
repo_name: stable
path: dist
source_name: myproject
```
__Parameters:__
- __api_url:__ Your Aptly API URL (required)
- __api_username:__ Username to authenticate on your Aptly API (required)
- __api_password:__ Password to authenticate on your Aptly API (required)
- __repo_name:__ Repository name to publish on (optional, default: `stable`)
- __path:__ Path to the directory where files to publish are stored (optional, default: `dist`)
- __source_name:__ Name of the source package to publish (optional, default: all `changes` files are will be publish)

View file

@ -1,4 +1,9 @@
#!/usr/bin/python3 #!/usr/bin/python3
""""
Entrypoint of a Woodpecker CI docker image plugin that permit to publish Debian
packages on a Aptly repository using its API
"""
import datetime import datetime
import os import os
import re import re
@ -76,8 +81,8 @@ if MAX_RETRY:
def list_files_in_changes_file(filepath): def list_files_in_changes_file(filepath):
""" List files included by a changes file """ """ List files included by a changes file """
dirpath = os.path.dirname(filepath) dirpath = os.path.dirname(filepath)
with open(filepath, "r", encoding="utf-8") as fd: with open(filepath, "r", encoding="utf-8") as file_desc:
changes_file = fd.read() changes_file = file_desc.read()
parser = PackagesParser(changes_file) parser = PackagesParser(changes_file)
files = [] files = []
@ -101,8 +106,8 @@ def changes_file2package_name(changes_file):
def upload_file(package_name, filepath): def upload_file(package_name, filepath):
""" Upload a file using Aptly API """ """ Upload a file using Aptly API """
url = f'{API_URL}/files/{package_name}' url = f'{API_URL}/files/{package_name}'
with open(filepath, 'rb') as fd: with open(filepath, 'rb') as file_desc:
result = session.post(url, files={'file': fd}) result = session.post(url, files={'file': file_desc})
return ( return (
result.status_code == 200 and result.status_code == 200 and
f'{package_name}/{os.path.basename(filepath)}' in result.json() f'{package_name}/{os.path.basename(filepath)}' in result.json()
@ -160,7 +165,7 @@ for changes_file in changes_files:
# Create a snapshot of the repository # Create a snapshot of the repository
snap_name = datetime.datetime.now().strftime('%Y%m%d-%H%M%S') + f'{REPO_NAME}' snap_name = datetime.datetime.now().strftime(f'%Y%m%d-%H%M%S_{REPO_NAME}')
print(f'Create new snapshot "{snap_name}" of repository "{REPO_NAME}"') print(f'Create new snapshot "{snap_name}" of repository "{REPO_NAME}"')
url = f'{API_URL}/repos/{REPO_NAME}/snapshots' url = f'{API_URL}/repos/{REPO_NAME}/snapshots'
@ -168,7 +173,7 @@ payload = {'Name': snap_name}
result = session.post(url, json=payload) result = session.post(url, json=payload)
try: try:
data = result.json() data = result.json()
except Exception: except Exception: # pylint: disable=broad-except
data = {} data = {}
error = ( error = (
result.status_code < 200 or result.status_code < 200 or