Skip to content

Commit 496123a

Browse files
authored
Skip deploy workflow when PyPI token is not defined in GitHub secrets (#147)
* Technical improvement * Impacted areas: CI workflows * Details: - Skip `deploy` workflow when PyPI token is not defined in GitHub secrets - Rename the GitHub secret `PYPI_TOKEN_OPENFISCA_BOT` used in `deploy` workflow to `PYPI_TOKEN` - Ensure lint scripts work properly without reachable tags - Update deprecated syntax in GitHub Action workflow
2 parents f3204f4 + 0af37a7 commit 496123a

File tree

8 files changed

+89
-30
lines changed

8 files changed

+89
-30
lines changed

.github/lint-changed-python-files.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/lint-changed-yaml-tests.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/lint-files.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#! /usr/bin/env bash
2+
3+
# Usage: lint-files.sh <file_pattern> <linter_command>
4+
#
5+
# Example usage:
6+
# lint-files.sh "*.py" "flake8"
7+
# lint-files.sh "tests/*.yaml" "yamllint"
8+
9+
file_pattern=$1
10+
linter_command=$2
11+
12+
if [ -z "$file_pattern" ] || [ -z "$linter_command" ]
13+
then
14+
echo "Usage: $0 <file_pattern> <linter_command>"
15+
exit 1
16+
fi
17+
18+
last_tagged_commit=$(git describe --tags --abbrev=0 --first-parent 2>/dev/null) # Attempt to find the last tagged commit in the direct ancestry of the main branch avoiding tags introduced by merge commits from other branches
19+
20+
if [ -z "$last_tagged_commit" ]
21+
then
22+
last_tagged_commit=$(git rev-list --max-parents=0 HEAD) # Fallback to finding the root commit if no tags are present
23+
fi
24+
25+
if ! changed_files=$(git diff-index --name-only --diff-filter=ACMR --exit-code $last_tagged_commit -- "$file_pattern")
26+
then
27+
echo "Linting the following files:"
28+
echo "$changed_files"
29+
$linter_command $changed_files
30+
else
31+
echo "No changed files matching pattern '$file_pattern' to lint."
32+
fi

.github/workflows/deploy.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,31 @@ jobs:
2323
with:
2424
python-version: 3.9.12
2525
- id: stop-early
26-
run: if "${GITHUB_WORKSPACE}/.github/has-functional-changes.sh" ; then echo "::set-output name=status::success" ; fi
26+
run: |
27+
if "${GITHUB_WORKSPACE}/.github/has-functional-changes.sh"
28+
then
29+
echo "status=success" >> $GITHUB_OUTPUT
30+
fi
2731
32+
check-pypi-token: # Use intermediary job as secrets cannot be directly referenced in `if:` conditionals; see https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#using-secrets-in-a-workflow
33+
runs-on: ubuntu-22.04
34+
outputs:
35+
pypi_token_present: ${{ steps.check_token.outputs.pypi_token_present }}
36+
steps:
37+
- name: Check PyPI token is defined
38+
id: check_token
39+
run: |
40+
if [[ -n "${{ secrets.PYPI_TOKEN }}" ]]
41+
then
42+
echo "pypi_token_present=true" >> $GITHUB_OUTPUT
43+
else
44+
echo "pypi_token_present=false" >> $GITHUB_OUTPUT
45+
fi
46+
2847
deploy:
2948
runs-on: ubuntu-22.04
30-
needs: [ validate, check-for-functional-changes ]
31-
if: needs.check-for-functional-changes.outputs.status == 'success'
49+
needs: [ validate, check-for-functional-changes, check-pypi-token ]
50+
if: needs.check-for-functional-changes.outputs.status == 'success' && needs.check-pypi-token.outputs.pypi_token_present == 'true'
3251
steps:
3352
- uses: actions/checkout@v4
3453
- name: Set up Python
@@ -46,6 +65,6 @@ jobs:
4665
path: dist
4766
key: release-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-${{ github.sha }}
4867
- name: Upload a Python package to PyPi
49-
run: twine upload dist/* --username __token__ --password ${{ secrets.PYPI_TOKEN_OPENFISCA_BOT }}
68+
run: twine upload dist/* --username __token__ --password ${{ secrets.PYPI_TOKEN }}
5069
- name: Publish a git tag
5170
run: "${GITHUB_WORKSPACE}/.github/publish-git-tag.sh"

.github/workflows/validate.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ jobs:
3333
- run: make check-style
3434

3535
- name: Lint Python files
36-
run: "${GITHUB_WORKSPACE}/.github/lint-changed-python-files.sh"
36+
run: "${GITHUB_WORKSPACE}/.github/lint-files.sh '*.py' 'flake8'"
3737

3838
- name: Lint YAML tests
39-
run: "${GITHUB_WORKSPACE}/.github/lint-changed-yaml-tests.sh"
39+
run: "${GITHUB_WORKSPACE}/.github/lint-files.sh 'tests/*.yaml' 'yamllint'"
4040

4141
test-yaml:
4242
runs-on: ubuntu-22.04

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
### 7.1.2 [#147](https://github.com/openfisca/country-template/pull/147)
4+
5+
* Technical improvement
6+
* Details:
7+
- Skip `deploy` workflow when PyPI token is not defined in GitHub secrets
8+
- Ensure lint scripts work properly without reachable tags
9+
- Rename the GitHub secret `PYPI_TOKEN_OPENFISCA_BOT` used in `deploy` workflow to `PYPI_TOKEN`
10+
- Update deprecated syntax in GitHub Actions workflow
11+
312
### 7.1.1 [#146](https://github.com/openfisca/country-template/pull/146)
413

514
* Technical improvement

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,28 @@ The files that are outside from the `openfisca_country_template` folder are used
5656

5757
## Packaging your Country Package for Distribution
5858

59-
Country packages are python distributions. To distribute your package via `pip`, follow the steps given by the [Python Packaging Authority](https://python-packaging-user-guide.readthedocs.io/tutorials/distributing-packages/#packaging-your-project).
59+
Country packages are Python distributions. You can choose to distribute your package automatically via the predefined continuous deployment system on GitHub Actions, or manually.
60+
61+
### Automatic continuous deployment on GitHub
62+
63+
This repository is configured with a continuous deployment system to automate the distribution of your package via `pip`.
64+
65+
#### Setting up continuous deployment
66+
67+
To activate the continuous deployment:
68+
69+
1. Create an account on [PyPI](https://pypi.org/) if you don't already have one.
70+
2. Generate a token in your PyPI account. This token will allow GitHub Actions to securely upload new versions of your package to PyPI.
71+
3. Add this token to your GitHub repository's secrets under the name `PYPI_TOKEN`.
72+
73+
Once set up, changes to the `main` branch will trigger an automated workflow to build and publish your package to PyPI, making it available for `pip` installation.
74+
75+
### Manual distribution
76+
77+
If you prefer to manually manage the release and distribution of your package, follow the guidelines provided by the [Python Packaging Authority](https://python-packaging-user-guide.readthedocs.io/tutorials/distributing-packages/#packaging-your-project).
78+
79+
This involves detailed steps on preparing your package, creating distribution files, and uploading them to PyPI.
80+
6081

6182
## Install Instructions for Users and Contributors
6283

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "openfisca-country_template"
3-
version = "7.1.1"
3+
version = "7.1.2"
44
description = "OpenFisca Rules as Code model for Country-Template."
55
readme = "README.md"
66
keywords = ["microsimulation", "tax", "benefit", "rac", "rules-as-code"]

0 commit comments

Comments
 (0)