Improve debug mode and allow to enable it using enviroment variable

This commit is contained in:
Benjamin Renard 2025-02-26 15:33:44 +01:00
parent adb45e01ea
commit 83421de02b
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 58 additions and 14 deletions

View file

@ -44,6 +44,7 @@ pipeline:
source_name: myproject source_name: myproject
max_retries: 2 max_retries: 2
force_overwrite: true force_overwrite: true
debug: false
``` ```
**Parameters:** **Parameters:**
@ -58,6 +59,7 @@ pipeline:
- **source_name:** Name of the source package to publish (optional, default: all `changes` files are will be publish) - **source_name:** Name of the source package to publish (optional, default: all `changes` files are will be publish)
- **max_retries:** The number of retry in case of error calling the Aptly API (optional, default: no retry) - **max_retries:** The number of retry in case of error calling the Aptly API (optional, default: no retry)
- **force_overwrite:** When publishing, overwrite files in `pool/` directory without notice (optional, default: false) - **force_overwrite:** When publishing, overwrite files in `pool/` directory without notice (optional, default: false)
- **debug:** Enable debug mode (optional, default: false)
## With Gitlab CI ## With Gitlab CI
@ -94,6 +96,7 @@ publish:
APTLY_SOURCE_NAME: "myproject" APTLY_SOURCE_NAME: "myproject"
APTLY_MAX_RETRIES: 2 APTLY_MAX_RETRIES: 2
APTLY_FORCE_OVERWRITE: true APTLY_FORCE_OVERWRITE: true
APTLY_DEBUG: false
script: script:
- echo "Publish Bullseye debian packages on APT repository..." - echo "Publish Bullseye debian packages on APT repository..."
- aptly-publish - aptly-publish
@ -154,6 +157,7 @@ jobs:
source_name: myproject source_name: myproject
max_retries: 2 max_retries: 2
force_overwrite: true force_overwrite: true
debug: false
``` ```
The parameters are passed using environment variables as designed with a Woodpecker CI plugin. Consequently, you could refer to the previous section for details about these parameters. For the password to request Aptly API, you have to set the `APTLY_API_PASSWORD` Actions secret in your Forgejo project configuration. The parameters are passed using environment variables as designed with a Woodpecker CI plugin. Consequently, you could refer to the previous section for details about these parameters. For the password to request Aptly API, you have to set the `APTLY_API_PASSWORD` Actions secret in your Forgejo project configuration.

View file

@ -80,21 +80,30 @@ def get_published_distribution_other_components_sources(args, distribution):
result.status_code, result.status_code,
) )
sys.exit(1) sys.exit(1)
for data in result.json(): data = result.json()
if data["Prefix"] != args.prefix: logging.debug(
"get_published_distribution_other_components_sources(%s): API return (%d): %s",
distribution,
result.status_code,
data,
)
for publish in data:
if publish["Prefix"] != args.prefix:
continue continue
if data["Distribution"] != distribution: if publish["Distribution"] != distribution:
continue continue
if data["SourceKind"] != "snapshot": if publish["SourceKind"] != "snapshot":
logging.error( logging.error(
"The distribution %s currently published on prefix '%s' do not sourcing packages " "The distribution %s currently published on prefix '%s' do not sourcing packages "
"from snapshot(s) but from %s.", "from snapshot(s) but from %s.",
distribution, distribution,
args.prefix, args.prefix,
data["SourceKind"], publish["SourceKind"],
) )
sys.exit(1) sys.exit(1)
return [source for source in data["Sources"] if source["Component"] != args.repo_component] return [
source for source in publish["Sources"] if source["Component"] != args.repo_component
]
logging.error( logging.error(
"Distribution %s seem not currently published on prefix '%s'. Please manually publish it " "Distribution %s seem not currently published on prefix '%s'. Please manually publish it "
"a first time before using %s.", "a first time before using %s.",
@ -110,10 +119,11 @@ def upload_file(session, package_name, filepath):
url = f"{args.api_url}/files/{package_name}" url = f"{args.api_url}/files/{package_name}"
with open(filepath, "rb") as file_desc: with open(filepath, "rb") as file_desc:
result = session.post(url, files={"file": file_desc}) result = session.post(url, files={"file": file_desc})
return ( data = result.json()
result.status_code == 200 logging.debug(
and f"{package_name}/{os.path.basename(filepath)}" in result.json() "upload_file(%s, %s): API return (%d): %s", package_name, filepath, result.status_code, data
) )
return result.status_code == 200 and f"{package_name}/{os.path.basename(filepath)}" in data
def include_changes_file(repo_name, package_name, changes_file): def include_changes_file(repo_name, package_name, changes_file):
@ -124,6 +134,14 @@ def include_changes_file(repo_name, package_name, changes_file):
) )
result = session.post(url) result = session.post(url)
data = result.json() data = result.json()
logging.debug(
"include_changes_file(%s, %s, %s): API return (%d): %s",
repo_name,
package_name,
changes_file,
result.status_code,
data,
)
if data.get("FailedFiles"): if data.get("FailedFiles"):
logging.error( logging.error(
"Some error occurred including %s:\nFailed files:\n - %s\nWarnings: - %s", "Some error occurred including %s:\nFailed files:\n - %s\nWarnings: - %s",
@ -149,7 +167,11 @@ def include_deb_package(repo_name, package_name, path):
data = result.json() data = result.json()
logging.debug( logging.debug(
"include_deb_package(%s, %s, %s): API return (%d): %s", "include_deb_package(%s, %s, %s): API return (%d): %s",
repo_name, package_name, path, result.status_code, data repo_name,
package_name,
path,
result.status_code,
data,
) )
if data.get("FailedFiles"): if data.get("FailedFiles"):
logging.error( logging.error(
@ -162,9 +184,7 @@ def include_deb_package(repo_name, package_name, path):
) )
return False return False
if not (result.status_code == 200 and data.get("Report", {}).get("Added")): if not (result.status_code == 200 and data.get("Report", {}).get("Added")):
logging.error( logging.error("Unknown error occurred including %s. See APTLY API logs for details.", path)
"Unknown error occurred including %s. See APTLY API logs for details.", path
)
return False return False
return True return True
@ -267,7 +287,21 @@ def publish_repo(args, session, distribution, repo_name, other_components_source
result = session.post(url, json=payload) result = session.post(url, json=payload)
try: try:
data = result.json() data = result.json()
logging.debug(
"publish_repo(%s, %s): API return (%d): %s",
distribution,
repo_name,
result.status_code,
data,
)
except ValueError: except ValueError:
logging.debug(
"publish_repo(%s, %s): API return (%d), fail to decode JSON result: '%s'",
distribution,
repo_name,
result.status_code,
result.text,
)
data = {} data = {}
error = ( error = (
result.status_code < 200 result.status_code < 200
@ -339,7 +373,13 @@ parser.add_argument(
action="store_true", action="store_true",
default=from_env("FORCE_OVERWRITE", "false").lower() in ["1", "true", "yes", "on"], default=from_env("FORCE_OVERWRITE", "false").lower() in ["1", "true", "yes", "on"],
) )
parser.add_argument("-d", "--debug", action="store_true", help="Enable debug mode") parser.add_argument(
"-d",
"--debug",
action="store_true",
help="Enable debug mode",
default=from_env("DEBUG", "false").lower() in ["1", "true", "yes", "on"],
)
args = parser.parse_args() args = parser.parse_args()