CI/CD Build Workflow #1503
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Build Workflow | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
workflow_dispatch: | |
env: | |
CANCEL_OTHERS: true | |
PATHS_IGNORE: '["**/README.rst", "**/docs/**", "**/ISSUE_TEMPLATE/**", "**/pull_request_template.md", "**/.vscode/**"]' | |
jobs: | |
pre-commit-hooks: | |
runs-on: ubuntu-latest | |
timeout-minutes: 3 | |
steps: | |
- id: skip_check | |
uses: fkirc/skip-duplicate-actions@master | |
with: | |
cancel_others: ${{ env.CANCEL_OTHERS }} | |
paths_ignore: ${{ env.PATHS_IGNORE }} | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
name: Checkout Code Repository | |
uses: actions/checkout@v3 | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
name: Set up Python 3.10 | |
uses: actions/setup-python@v3 | |
with: | |
python-version: "3.10" | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
# Run all pre-commit hooks on all the files. | |
# Getting only staged files can be tricky in case a new PR is opened | |
# since the action is run on a branch in detached head state | |
name: Install and Run Pre-commit | |
uses: pre-commit/action@v3.0.0 | |
build: | |
name: Build (Python ${{ matrix.python-version }}) | |
runs-on: "ubuntu-latest" | |
timeout-minutes: 10 | |
defaults: | |
run: | |
shell: bash -l {0} | |
strategy: | |
matrix: | |
python-version: ["3.9", "3.10", "3.11"] | |
steps: | |
- id: skip_check | |
uses: fkirc/skip-duplicate-actions@master | |
with: | |
cancel_others: ${{ env.CANCEL_OTHERS }} | |
paths_ignore: ${{ env.PATHS_IGNORE }} | |
do_not_skip: '["push", "workflow_dispatch"]' | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
uses: actions/checkout@v3 | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
name: Set up Conda Environment | |
uses: conda-incubator/setup-miniconda@v2 | |
with: | |
miniforge-variant: Mambaforge | |
miniforge-version: latest | |
activate-environment: "xcdat_ci" | |
use-mamba: true | |
mamba-version: "*" | |
channel-priority: strict | |
auto-update-conda: true | |
python-version: ${{ matrix.python-version }} | |
# Refresh the cache every 24 hours to avoid inconsistencies of package versions | |
# between the CI pipeline and local installations. | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
id: get-date | |
name: Get Date | |
run: echo "today=$(/bin/date -u '+%Y%m%d')" >> $GITHUB_OUTPUT | |
shell: bash | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
id: cache | |
name: Cache Conda env | |
uses: actions/cache@v3 | |
with: | |
path: ${{ env.CONDA }}/envs | |
key: | |
conda-${{ runner.os }}-${{ runner.arch }}-${{ matrix.python-version }}-${{ | |
steps.get-date.outputs.today }}-${{hashFiles('conda-env/ci.yml') }}-${{ env.CACHE_NUMBER}} | |
env: | |
# Increase this value to reset cache if conda-env/ci.yml has not changed in the workflow | |
CACHE_NUMBER: 0 | |
- if: | | |
steps.skip_check.outputs.should_skip == 'false' && | |
steps.cache.outputs.cache-hit == 'false' | |
name: Update environment | |
run: | | |
mamba env update -n xcdat_ci -f conda-env/ci.yml | |
# Make sure the Python version in the env matches the current matrix version. | |
mamba install -c conda-forge python=${{ matrix.python-version }} | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
name: Install xcdat | |
# Source: https://github.com/conda/conda-build/issues/4251#issuecomment-1053460542 | |
run: | | |
python -m pip install --no-build-isolation --no-deps -e . | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
name: Run Tests | |
run: | | |
pytest | |
- if: ${{ steps.skip_check.outputs.should_skip == 'false' }} | |
name: Upload Coverage Report | |
uses: codecov/codecov-action@v3 | |
with: | |
file: "tests_coverage_reports/coverage.xml" | |
fail_ci_if_error: true |