Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add compose check #20

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
id: selftest
uses: khalford/check-version-action@main
with:
path: "version.txt"
app_version_path: "version.txt"

- name: Log Success
if: ${{ steps.selftest.outputs.updated == 'true' }}
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/workflow_compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Integration Test with Compose
on: [push, pull_request]
jobs:
self-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Main to compare
uses: actions/checkout@v4
with:
ref: 'main'
path: 'main'

- uses: actions/checkout@v4
with:
path: 'branch'

- name: Self test
if: ${{ github.ref != 'refs/heads/main' }}
id: selftest
uses: khalford/check-version-action@dev
with:
app_version_path: "version.txt"
docker_compose_path: "docker-compose.yml"

- name: Log Success
if: ${{ steps.selftest.outputs.app_updated == 'true' }}
run: |
echo "App version has been updated correctly!"

- name: Log Success
if: ${{ steps.selftest.outputs.compose_updated == 'true' }}
run: |
echo "Compose version has been updated correctly!"
12 changes: 9 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
name: 'Check Semver Version Number'
description: 'Check if the semver version number has changed from the main branch.'
inputs:
path:
app_version_path:
description: 'Path to main app version file.'
required: true
default: './version.txt'
docker_compose_path:
description: 'Path to compose file.'
required: false
outputs:
updated:
description: 'If the version was updated or not.'
app_updated:
description: 'If the app version was updated or not.'
compose_updated:
description: 'If the compose version was updated or not.'

runs:
using: 'docker'
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
self-test:
image: some/test:0.3.5
61 changes: 48 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This module is the entry point for the Action."""

import os
import json
from pathlib import Path
from packaging.version import Version

Expand All @@ -10,24 +9,25 @@ class VersionNotUpdated(Exception):
"""The version number has not been updated or updated incorrectly."""


def get_file_contents(main_path: Path, branch_path: Path) -> (str, str):
def get_file_contents(main_path: Path, branch_path: Path, app_path: Path) -> (str, str):
"""
This function returns the contents of the main branch version and working branch version.
:param app_path: Path to app version file
:param main_path: Path to version on main
:param branch_path: Path to version on working branch
:return: Tuple of versions (main, branch)
"""
with open(main_path, "r", encoding="utf-8") as main_file:
with open(main_path / app_path, "r", encoding="utf-8") as main_file:
main_contents = main_file.read()
with open(branch_path, "r", encoding="utf-8") as branch_file:
with open(branch_path / app_path, "r", encoding="utf-8") as branch_file:
branch_contents = branch_file.read()
return main_contents, branch_contents


def compare(main: str, branch: str, input_path: str) -> str:
def compare_app_version(main: str, branch: str, input_path: str) -> str:
"""
This function compares the versions using the packaging library.
It raises an error is the versions are stale or incorrect.
It raises an error if the versions are stale or incorrect.
:param main: Version on main
:param branch: Version on working branch
:param input_path: Path to version file to display in error
Expand All @@ -41,17 +41,52 @@ def compare(main: str, branch: str, input_path: str) -> str:
return "true"


def compare_compose_version(branch_version: str, branch_path: Path, compose_path: str, app_path: str) -> str:
"""
This function compares the versions using the packaging library.
It raises an error if the versions don't match.
:param branch_version: Version of app on branch
:param branch_path: Path to branch
:param compose_path: Path to compose
:param app_path: Path to app version file
:return: true if found
"""
with open(branch_path / compose_path, "r", encoding="utf-8") as compose_file:
compose_file_contents = compose_file.readlines()
version_str = ""
for line in compose_file_contents:
if "image" in line:
version_str = line.strip('\n').split(":")[-1]
break
if Version(branch_version) != Version(version_str):
raise VersionNotUpdated(
f"The version number in {compose_path} doesn't match the version in {app_path}."
f"\nPlease update this."
)
return "true"


if __name__ == "__main__":
INPUT_PATH = Path(os.environ.get("INPUT_PATH"))
APP_PATH = Path(os.environ.get("INPUT_APP_VERSION_PATH"))
COMPOSE_PATH = os.environ.get("INPUT_DOCKER_COMPOSE_PATH")
ROOT_PATH = Path(os.environ.get("GITHUB_WORKSPACE"))
MAIN_PATH = ROOT_PATH / "main" / INPUT_PATH
BRANCH_PATH = ROOT_PATH / "branch" / INPUT_PATH
MAIN_PATH = ROOT_PATH / "main"
BRANCH_PATH = ROOT_PATH / "branch"

main_version, branch_version = get_file_contents(MAIN_PATH, BRANCH_PATH, APP_PATH)
APP_UPDATED = compare_app_version(main_version, branch_version, str(APP_PATH))

if COMPOSE_PATH:
COMPOSE_PATH = Path(COMPOSE_PATH)
COMPOSE_UPDATED = compare_compose_version(branch_version, BRANCH_PATH, str(COMPOSE_PATH), str(APP_PATH))
GITHUB_ENV = os.getenv('GITHUB_ENV')
with open(GITHUB_ENV, "a") as env:
print("wrote compose")
env.write("compose_updated=true")

main_version, branch_version = get_file_contents(MAIN_PATH, BRANCH_PATH)
UPDATED = compare(main_version, branch_version, INPUT_PATH)
# Set the outputs variable to "true"
# No need to check here as we expect only true returned
print(f"::set-output name=updated::{UPDATED}")
GITHUB_ENV = os.getenv('GITHUB_ENV')
with open(GITHUB_ENV, "a") as env:
env.write("updated=true")
print("wrote app")
env.write("app_updated=true")
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.4
0.3.5
Loading