From 2826a6a4670d9929fc612d0db4992617966519c9 Mon Sep 17 00:00:00 2001 From: Anes Benmerzoug Date: Mon, 14 Oct 2024 21:31:04 +0200 Subject: [PATCH] Add github actions --- .github/actions/poetry/action.yml | 80 ++++++++++++++++++++++++++ .github/workflows/main.yml | 94 +++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 .github/actions/poetry/action.yml create mode 100644 .github/workflows/main.yml diff --git a/.github/actions/poetry/action.yml b/.github/actions/poetry/action.yml new file mode 100644 index 0000000..2bef991 --- /dev/null +++ b/.github/actions/poetry/action.yml @@ -0,0 +1,80 @@ +name: Setup Poetry Virtual Environment + +inputs: + python_version: + required: true + type: string + poetry_version: + required: true + type: string + default: '1.8.3' + poetry_included_groups: + required: false + type: string + poetry_excluded_groups: + required: false + type: string + +runs: + using: "composite" + steps: + - name: Setup and Cache Python ${{ inputs.python_version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ inputs.python_version }} + + - name: Install poetry + uses: abatilo/actions-poetry@v2 + + - name: Setup a local virtual environment (if no poetry.toml file) + run: | + poetry config virtualenvs.create true --local + poetry config virtualenvs.in-project true --local + + - name: Load cached Poetry installation + id: cached-poetry + uses: actions/cache@v4 + with: + path: ~/.local + key: poetry-${{ runner.os }}-${{ inputs.poetry_version }} + + - name: Install and configure Poetry + if: steps.cached-poetry.outputs.cache-hit != 'true' + uses: snok/install-poetry@v1 + with: + version: ${{ inputs.poetry_version }} + virtualenvs-create: true + virtualenvs-in-project: true + installer-parallel: true + + - name: Load cached venv + id: cached-venv + uses: actions/cache@v4 + with: + path: .venv + key: venv-${{ runner.os }}-${{ inputs.python_version }}-${{ hashFiles('poetry.lock') }} + + - if: steps.cached-venv.outputs.cache-hit != 'true' + name: Create Virtual Environment + shell: bash + run: | + echo "Included optional dependency groups ${{ inputs.poetry_included_groups }}" + echo "Excluded optional dependency groups ${{ inputs.poetry_excluded_groups }}" + + # --sync: Synchronize the environment with the locked packages and the specified groups. + COMMON_OPTS="--sync" + WITH_OPTS= + WITHOUT_OPTS= + + if [ -n "${{ inputs.poetry_included_groups }}" ]; then + WITH_OPTS="--with ${{ inputs.poetry_included_groups }}" + fi + if [ -n "${{ inputs.poetry_excluded_groups }}" ]; then + WITHOUT_OPTS="--without ${{ inputs.poetry_excluded_groups }}" + fi + + poetry install $COMMON_OPTS $WITH_OPTS $WITHOUT_OPTS + + source $(poetry env info --path)/bin/activate + # Set the path for subsequent steps so that the environment is activated by default + echo PATH=$PATH >> $GITHUB_ENV diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..7792d53 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,94 @@ +name: Main CI Workflow + +on: + pull_request: + branches: + - main + push: + branches: + - main + workflow_dispatch: + inputs: + reason: + description: Why did you trigger the pipeline? + required: False + default: Check if it runs again due to external changes + + +env: + GITHUB_BOT_USERNAME: github-actions[bot] + GITHUB_BOT_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com + PY_COLORS: 1 + PYTHON_VERSION: '3.11' + PACKAGE_NAME: 'language-transfer' + + +jobs: + code-quality: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup Virtual Environment + uses: ./.github/actions/poetry + with: + python_version: {{ '${{ env.PYTHON_VERSION }}' }} + poetry_included_groups: dev + + - name: Run pre-commit + run: pre-commit run --all-files --verbose --show-diff-on-failure + + tests: + runs-on: ubuntu-latest + needs: [code-quality] + steps: + - uses: actions/checkout@v4 + + - name: Setup Virtual Environment + uses: ./.github/actions/poetry + with: + python_version: {{ '${{ env.PYTHON_VERSION }}' }} + poetry_included_groups: dev + - name: Run pytest + run: pytest -vvv + + publish-testpypi: + name: Publish package to TestPyPI + runs-on: ubuntu-latest + needs: [code-quality, tests] + if: ${{ github.ref == 'refs/heads/main' }} + concurrency: + group: publish-testpypi + permissions: + id-token: write + environment: + name: testpypi + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + lfs: true + + - name: Setup Virtual Environment + uses: ./.github/actions/poetry + with: + python_version: {{ '${{ env.PYTHON_VERSION }}' }} + poetry_included_groups: dev + + - name: Bump Version and Build Package + run: | + set -x + CURRENT_VERSION=$(bump-my-version show current_version) + BUILD_NUMBER=$GITHUB_RUN_NUMBER + NEW_VERSION=$(echo $CURRENT_VERSION | sed -e "s/-dev[0-9]\+/-dev${BUILD_NUMBER}/g") + bump-my-version bump --no-tag --no-commit --verbose --new-version ${NEW_VERSION} + echo "Version was bumped from ${CURRENT_VERSION} to ${NEW_VERSION}!" + poetry build --format=wheel + + # TODO: uncomment once the repository and testpypi are configured + # - name: Publish to TestPyPI + # uses: pypa/gh-action-pypi-publish@release/v1 + # with: + # repository-url: https://test.pypi.org/legacy/ + # print-hash: true + # verbose: true