diff --git a/.github/workflows/push-job.yaml b/.github/workflows/push-job.yaml new file mode 100644 index 0000000..4a59a0e --- /dev/null +++ b/.github/workflows/push-job.yaml @@ -0,0 +1,81 @@ +name: CI/CD on push +on: + push: + branches: + - master + - development + - feat/implement-ci-cd-pipeline +env: + POETRY_VERSION: 1.5.1 + PYTHON_VERSION: '3.10' + GH_TOKEN: ${{ github.token }} +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Check Version + run: bash scripts/check_pypi_version.sh + + - id: setup-python + name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: ${{ env.PYTHON_VERSION }} + + - name: Cache Poetry + uses: actions/cache@v2 + with: + path: ~/.local + key: poetry-${{ env.POETRY_VERSION }}-0 + + - name: Setup Poetry + uses: snok/install-poetry@v1 + with: + version: ${{ env.POETRY_VERSION }} + virtualenvs-create: true + virtualenvs-in-project: true + + - id: cache-python-dependencies + name: Cache Python Dependencies + uses: actions/cache@v2 + with: + path: .venv + key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} + + - name: Install Python Dependencies + if: steps.cache-python-dependencies.outputs.cache-hit != 'true' + run: poetry install --no-interaction --no-root + + - name: Build Package + # if: github.ref == 'ref/head/master' + run: poetry build + + - id: create-release + name: Create Release + # if: github.ref == 'ref/head/master' + run: | + git config --local user.name "Github Action" + + bash scripts/check_tag.sh + if [[ ! $? == 0 ]]; then exit 1; fi + + bash scripts/check_semver.sh $tag + semver=$([[ $? == 0 ]] && echo 1) + echo "SEMVER=$semver" >> $GITHUB_OUTPUT + + tag=$(bash scripts/get_release_tag.sh) + echo "Creating new release: $tag" + + gh release create $tag \ + --generate-notes \ + --title "Release: $tag" + ${semver:+--prerelease} + + # - name: Publish Package + # # if: github.ref == 'ref/head/master' && steps.create-release.outputs.semver == 1 + # run: | + # poetry config pypi-token.pypi ${{ secrets.PYPI_TOKEN }} + # poetry publish \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index fcb0b9c..b63ea4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ajaejokes" -version = "0.1.1" +version = "0.1.1.rc" description = "" authors = ["Jeongwon Song "] readme = "README.md" diff --git a/scripts/check_pypi_version.sh b/scripts/check_pypi_version.sh new file mode 100644 index 0000000..90d6a82 --- /dev/null +++ b/scripts/check_pypi_version.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# Scripts to check if the release already exists in pypi +latestVersion=$(curl -s https://pypi.org/pypi/ajaejokes/json | jq -r '.info.version') +currentVersion=$(bash scripts/get_release_tag.sh) + +echo "Latest Version: $latestVersion" +echo "Current Version: $currentVersion" + +# TODO: Check semver format of currentVersion +# Check if current version = latestVersion first. (i.e. more common mistakes) +if [[ $latestVersion == $currentVersion ]]; then +echo "$currentVersion is not upgraded" +exit 1 +fi + +releases=$(curl -s https://pypi.org/pypi/ajaejokes/json | jq -cr '.releases | keys | .[]') +for release in ${releases[@]}; do +if [[ $release == $currentVersion ]]; then + echo "$currentVersion already exists" + exit 1 +fi +done \ No newline at end of file diff --git a/scripts/check_semver.sh b/scripts/check_semver.sh new file mode 100644 index 0000000..93a95d4 --- /dev/null +++ b/scripts/check_semver.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Check if valid semver +# TODO: v prefix or .rc postfix are supported +VERSION="$@" +rx='^([0-9]+\.){0,2}(\*|[0-9]+)$' +if [[ $VERSION =~ $rx ]]; then + echo "valid semver" + exit 0 +else + echo "invalid semver" + exit 1 +fi \ No newline at end of file diff --git a/scripts/check_tag.sh b/scripts/check_tag.sh new file mode 100644 index 0000000..bda48a4 --- /dev/null +++ b/scripts/check_tag.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# Script to check if tag already exists +tags=$(git tag) +for tag in ${tags[@]}; do +if [[ "$@" == $tag ]]; then + echo "tag $@ already exists" + exit 1 +fi +done \ No newline at end of file diff --git a/scripts/get_release_tag.sh b/scripts/get_release_tag.sh new file mode 100644 index 0000000..754e01a --- /dev/null +++ b/scripts/get_release_tag.sh @@ -0,0 +1 @@ +echo -n $(cat pyproject.toml| grep -oP 'version = "\K.*?(?=")')