From a503b3e99e204490ae296c3223c737d499f343da Mon Sep 17 00:00:00 2001 From: Michael Honaker Date: Wed, 11 Jan 2023 04:22:42 +0000 Subject: [PATCH] Configure Github Actions --- .github/workflows/publish-build.yml | 34 ++++++++++++++++++++++++++++ .github/workflows/validate-build.yml | 25 ++++++++++++++++++++ Dockerfile | 6 +++-- Makefile | 17 ++++++++++++-- scripts/format_package.sh | 2 +- scripts/lint_package.sh | 2 +- scripts/run_container.sh | 10 ++++++++ scripts/set_version.sh | 8 +++++++ scripts/test_package.sh | 2 +- 9 files changed, 99 insertions(+), 7 deletions(-) create mode 100755 .github/workflows/publish-build.yml create mode 100644 .github/workflows/validate-build.yml create mode 100755 scripts/run_container.sh create mode 100755 scripts/set_version.sh diff --git a/.github/workflows/publish-build.yml b/.github/workflows/publish-build.yml new file mode 100755 index 0000000..ad7d78d --- /dev/null +++ b/.github/workflows/publish-build.yml @@ -0,0 +1,34 @@ +name: Publish Build + +on: + release: + types: [published] + push: + # support traditional versions, and dev versions + tags: ["*.*.*","*.*.*-*"] + +jobs: + + publish: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image + run: | + export DOCKER_IMAGE=mtconnect-python:$(date +%s) + echo "DOCKER_IMAGE=${DOCKER_IMAGE}" >> $GITHUB_ENV + docker build . --file Dockerfile --tag ${DOCKER_IMAGE} + - name: Bump Version + run: | + ./scripts/run_container.sh ${DOCKER_IMAGE} scripts/set_version.sh "${GITHUB_REF_NAME}" + echo "VERSION_CHANGE=$(git diff --exit-code)" >> $GITHUB_ENV + - name: Commit Change + if: ${{ vars.VERSION_CHANGE == '1' }} + run: | + git add . + git commit -m 'Bump Version' + git tag -d ${GITHUB_REF_NAME} + git tag ${GITHUB_REF_NAME} + git push --force origin ${GITHUB_REF_NAME} \ No newline at end of file diff --git a/.github/workflows/validate-build.yml b/.github/workflows/validate-build.yml new file mode 100644 index 0000000..8656e93 --- /dev/null +++ b/.github/workflows/validate-build.yml @@ -0,0 +1,25 @@ +name: Validate Build + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + validate: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build the Docker image + run: | + export DOCKER_IMAGE=mtconnect-python:$(date +%s) + echo "DOCKER_IMAGE=${DOCKER_IMAGE}" >> $GITHUB_ENV + docker build . --file Dockerfile --tag ${DOCKER_IMAGE} + - name: Test Format + run: ./scripts/run_container.sh ${DOCKER_IMAGE} scripts/format_package.sh --check + - name: Test Package + run: ./scripts/run_container.sh ${DOCKER_IMAGE} scripts/test_package.sh \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4b0a745..db335fb 100755 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,10 @@ FROM python:3.8-slim as base WORKDIR /data -# Install poetry -RUN pip3 install poetry +# Install deps +RUN pip3 install poetry && \ + apt update && \ + apt install git -y # Copy depdencies and install base packages COPY pyproject.toml poetry.lock ./ diff --git a/Makefile b/Makefile index 5f2b3bf..842da25 100644 --- a/Makefile +++ b/Makefile @@ -6,16 +6,29 @@ build: develop: build ./scripts/run_develop_container.sh ${IMAGE_NAME} bash +run: build + ./scripts/run_container.sh ${IMAGE_NAME} bash + test: build ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/test_package.sh +test.format: build + ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/format_package.sh --check + + format: build ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/format_package.sh + lint: build - ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/lint_package.sh + ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/lint_package.sh PYPI_USER?=${PYPI_USER} PYPI_PASSWORD?=${PYPI_PASSWORD} publish: build - ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/publish_package.sh \ No newline at end of file + ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/publish_package.sh + +# This is only here for completeness. This should only be called via CI +VERSION?=${VERSION} +version: build + ./scripts/run_develop_container.sh ${IMAGE_NAME} scripts/set_version.sh ${VERSION} \ No newline at end of file diff --git a/scripts/format_package.sh b/scripts/format_package.sh index a953a75..363b6f0 100755 --- a/scripts/format_package.sh +++ b/scripts/format_package.sh @@ -1,3 +1,3 @@ #!/bin/bash -python3 -m black . \ No newline at end of file +python3 -m black . "$@" \ No newline at end of file diff --git a/scripts/lint_package.sh b/scripts/lint_package.sh index a958b04..fa24d57 100755 --- a/scripts/lint_package.sh +++ b/scripts/lint_package.sh @@ -1,3 +1,3 @@ #!/bin/bash -python3 -m pylint mtconnect tests \ No newline at end of file +python3 -m pylint mtconnect tests "$@" \ No newline at end of file diff --git a/scripts/run_container.sh b/scripts/run_container.sh new file mode 100755 index 0000000..fe4af55 --- /dev/null +++ b/scripts/run_container.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ "$#" -lt 2 ]; then + echo "Please supply image name and command to run" + exit 1 +fi + +image_name=$1 + +docker run --name run-ctr --rm ${image_name} "${@:2}" \ No newline at end of file diff --git a/scripts/set_version.sh b/scripts/set_version.sh new file mode 100755 index 0000000..3fc34ac --- /dev/null +++ b/scripts/set_version.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +if [ "$#" -ne 1 ]; then + echo "Please supply version" + exit 1 +fi + +poetry version "$@" \ No newline at end of file diff --git a/scripts/test_package.sh b/scripts/test_package.sh index 0e022aa..f102c1b 100755 --- a/scripts/test_package.sh +++ b/scripts/test_package.sh @@ -1,3 +1,3 @@ #!/bin/bash -python3 -m unittest \ No newline at end of file +python3 -m unittest "$@" \ No newline at end of file