Skip to content

Commit

Permalink
Add seperate check for valid version
Browse files Browse the repository at this point in the history
  • Loading branch information
orome committed Nov 5, 2023
1 parent ac57b2d commit b7ca850
Showing 1 changed file with 98 additions and 56 deletions.
154 changes: 98 additions & 56 deletions .github/workflows/test-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ env:
# regexr.com/7mmq7
TAG_REGEX: '^v(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))((a|b|rc)(0|[1-9][0-9]*))?(\.(dev|post)(0|[1-9][0-9]*))?$'
DEV_TAG_REGEX: '\.(dev|post)(0|[1-9][0-9]*)$'
TOOL_PYTHON_VERSION: 3.12

jobs:

test:

name: Python ${{ matrix.python-version }} (${{ matrix.os }})
runs-on: ${{ matrix.os }}
outputs:
PACKAGE_VERSION: ${{ steps.get_version.outputs.PACKAGE_VERSION }}
continue-on-error: ${{ matrix.python-version == '3.13.0-alpha.1' }}
strategy:
matrix:
Expand Down Expand Up @@ -48,12 +47,6 @@ jobs:
- name: Display Python version (${{ matrix.python-version }})
run: python -c "import sys; print(sys.version)"

- name: Display package version
id: get_version
run: |
python -c "import automata; print(automata.__version__)"
echo "PACKAGE_VERSION=$(python -c 'import automata; print(automata.__version__)')" >> $GITHUB_OUTPUT
# Skip for Windows; path setting there isn't worth the trouble
- name: Run eyeball checks
if: runner.os != 'Windows'
Expand All @@ -66,71 +59,120 @@ jobs:
pip install pytest
pytest tests/ --maxfail=3 --showlocals --color=yes -v
validate_tag:
name: Validate Tag
validate_version:
name: Validate Version
runs-on: ubuntu-latest
needs: test
outputs:
IS_TAG: ${{ steps.validate.outputs.IS_TAG }}
TAG_VALID: ${{ steps.validate.outputs.TAG_VALID }}
TAG_IS_DEV: ${{ steps.validate.outputs.TAG_IS_DEV }}
TAG_VERSION_MATCH: ${{ steps.validate.outputs.TAG_VERSION_MATCH }}
PACKAGE_VERSION: ${{ steps.get_version.outputs.PACKAGE_VERSION }}
PACKAGE_VALID: ${{ steps.get_version.outputs.PACKAGE_VALID }}

steps:

- name: Checkout repo
uses: actions/checkout@v4

- name: Validate version tag
id: validate
run: |
TAG_REGEX="${{ env.TAG_REGEX }}"
DEV_TAG_REGEX="${{ env.DEV_TAG_REGEX }}"
TAG_NAME=${GITHUB_REF#refs/tags/}
PACKAGE_VERSION=${{ needs.test.outputs.PACKAGE_VERSION }}
- name: Set up Python (${{ env.TOOL_PYTHON_VERSION }})
uses: actions/setup-python@v4
with:
python-version: ${{ env.TOOL_PYTHON_VERSION }}

if [[ "${TAG_NAME}" =~ "refs/tags/" ]]; then IS_TAG="true"; else IS_TAG="false"; fi
echo "IS_TAG=IS_TAG" >> $GITHUB_OUTPUT
if [[ TAG_NAME == "true" ]]; then
echo "Tag: $TAG_NAME"
else
echo "Not a tag: $TAG_NAME"
fi
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Check if the tag name matches the version pattern
if [[ "${TAG_NAME}" =~ ${TAG_REGEX} ]]; then TAG_VALID="true"; else TAG_VALID="false"; fi
echo "TAG_VALID=$TAG_VALID" >> $GITHUB_OUTPUT
if [[ $TAG_VALID == "true" ]]; then
echo "Valid version tag"
else
echo "Invalid (remember the v) or missing version tag"
fi
- name: Validate package version
id: get_version
run: |
# python -c "import automata; print(automata.__version__)"
# Check if the tag name matches the dev version pattern
if [[ "${TAG_NAME}" =~ ${DEV_TAG_REGEX} ]]; then TAG_IS_DEV="true"; else TAG_IS_DEV="false"; fi
echo "TAG_IS_DEV=$TAG_IS_DEV" >> $GITHUB_OUTPUT
if [[ $TAG_IS_DEV == "true" ]]; then
echo "Development version tag"
else
echo "Release or invalid (remember the v) version tag"
fi
PACKAGE_VERSION=$(python -c 'import automata; print(automata.__version__)')
echo "PACKAGE_VERSION=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
if [[ ${TAG_NAME#v} == $PACKAGE_VERSION ]]; then TAG_VERSION_MATCH="true"; else TAG_VERSION_MATCH="false"; fi
echo "TAG_VERSION_MATCH=$TAG_VERSION_MATCH" >> $GITHUB_OUTPUT
if [[ $TAG_VERSION_MATCH == "true" ]]; then
echo "Version in tag matches package version: $PACKAGE_VERSION != ${TAG_NAME#v}"
# echo "PACKAGE_VERSION=$(python -c 'import automata; print(automata.__version__)')" >> $GITHUB_OUTPUT
echo "Package version: $PACKAGE_VERSION"
# Check if the tag name matches the version pattern
if [[ v"${PACKAGE_VERSION}" =~ ${TAG_REGEX} ]]; then PACKAGE_VALID="true"; else PACKAGE_VALID="false"; fi
echo "PACKAGE_VALID=$PACKAGE_VALID" >> $GITHUB_OUTPUT
if [[ $PACKAGE_VALID == "true" ]]; then
echo "Valid package version"
else
echo "Version in tag does not match package version: $PACKAGE_VERSION != ${TAG_NAME#v}"
echo "Invalid package version"
# exit 1 # Tests fail if the package version is invalid
fi
shell: bash
validate_tag:
name: Validate Tag
runs-on: ubuntu-latest
needs: validate_version
if: >-
needs.validate_version.outputs.PACKAGE_VALID == 'true'
outputs:
IS_TAG: ${{ steps.validate.outputs.IS_TAG }}
TAG_VALID: ${{ steps.validate.outputs.TAG_VALID }}
TAG_IS_DEV: ${{ steps.validate.outputs.TAG_IS_DEV }}
TAG_VERSION_MATCH: ${{ steps.validate.outputs.TAG_VERSION_MATCH }}

steps:

- name: Checkout repo
uses: actions/checkout@v4

- name: Validate version tag
id: validate
run: |
TAG_REGEX="${{ env.TAG_REGEX }}"
DEV_TAG_REGEX="${{ env.DEV_TAG_REGEX }}"
PACKAGE_VERSION=${{ needs.validate_version.outputs.PACKAGE_VERSION }}
if [[ "${GITHUB_REF}" =~ "refs/tags/" ]]; then IS_TAG="true"; else IS_TAG="false"; fi
echo "IS_TAG=$IS_TAG" >> $GITHUB_OUTPUT
if [[ $IS_TAG == "true" ]]; then
TAG_NAME=${GITHUB_REF#refs/tags/}
echo "Tag: $TAG_NAME"
else
TAG_NAME=${GITHUB_REF}
echo "Not a tag: $TAG_NAME"
fi
# Check if the tag name matches the version pattern
if [[ "${TAG_NAME}" =~ ${TAG_REGEX} ]]; then TAG_VALID="true"; else TAG_VALID="false"; fi
echo "TAG_VALID=$TAG_VALID" >> $GITHUB_OUTPUT
if [[ $TAG_VALID == "true" ]]; then
echo "Valid version tag"
else
echo "Invalid (remember the v) or missing version tag"
fi
# Check if the tag name matches the dev version pattern
if [[ "${TAG_NAME}" =~ ${DEV_TAG_REGEX} ]]; then TAG_IS_DEV="true"; else TAG_IS_DEV="false"; fi
echo "TAG_IS_DEV=$TAG_IS_DEV" >> $GITHUB_OUTPUT
if [[ $TAG_IS_DEV == "true" ]]; then
echo "Development version tag"
else
echo "Release or invalid (remember the v) version tag"
fi
if [[ ${TAG_NAME#v} == $PACKAGE_VERSION ]]; then TAG_VERSION_MATCH="true"; else TAG_VERSION_MATCH="false"; fi
echo "TAG_VERSION_MATCH=$TAG_VERSION_MATCH" >> $GITHUB_OUTPUT
if [[ $TAG_VERSION_MATCH == "true" ]]; then
echo "Version in tag matches package version: $PACKAGE_VERSION != ${TAG_NAME#v}"
else
echo "Version in tag does not match package version: $PACKAGE_VERSION != ${TAG_NAME#v}"
fi
publish:

name: Build and Publish
needs: validate_tag
needs: [validate_tag, validate_version]
runs-on: ubuntu-latest
environment: deployment
# if: needs.validate_tag.outputs.TAG_VALID == 'true'
# Includes redundant check for PACKAGE_VALID
if: >-
needs.validate_version.outputs.PACKAGE_VALID == 'true' &&
needs.validate_tag.outputs.IS_TAG == 'true' &&
needs.validate_tag.outputs.TAG_VALID == 'true' &&
needs.validate_tag.outputs.TAG_VERSION_MATCH == 'true' &&
Expand All @@ -154,10 +196,10 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v4

- name: Set up Python (3.12)
- name: Set up Python (${{ env.TOOL_PYTHON_VERSION }})
uses: actions/setup-python@v4
with:
python-version: 3.12
python-version: ${{ env.TOOL_PYTHON_VERSION }}

- name: Install dependencies
run: |
Expand All @@ -181,7 +223,7 @@ jobs:
# - name: Install Flit
# run: |
# python -m pip install --upgrade pip
# python -m pip install flit
# python -m pip install flit #
# flit --version
#
# - name: Build the package
Expand Down

0 comments on commit b7ca850

Please sign in to comment.