Skip to main content
Sign in
Snippets Groups Projects
Commit 247cbe20 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Rework CI to ensure there is no push to master.

parent cda7306f
No related branches found
No related tags found
1 merge request!2Rework CI
Pipeline #1056 failed
...@@ -9,11 +9,12 @@ default: ...@@ -9,11 +9,12 @@ default:
stages: stages:
- build - build
- sync_with_openfisca_france - sync_with_openfisca_france
- trigger
build: build:
stage: build stage: build
rules: rules:
- if: '$CI_PIPELINE_SOURCE == "push"' - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
before_script: before_script:
## Without apt update, apt install fails. ## Without apt update, apt install fails.
- apt update --yes - apt update --yes
...@@ -26,10 +27,33 @@ build: ...@@ -26,10 +27,33 @@ build:
script: script:
- poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test - poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test
build_with_latest_dependencies:
stage: build
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
before_script:
## Without apt update, apt install fails.
- apt update --yes
- apt install --yes python-is-python3 python3-virtualenv
- curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -
## Add Poetry to path:
- export PATH="/root/.local/bin:$PATH"
- poetry update
- poetry install
script:
- poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test
sync_with_openfisca_france: sync_with_openfisca_france:
stage: sync_with_openfisca_france stage: sync_with_openfisca_france
rules: rules:
- if: '$CI_PROJECT_URL == "https://git.leximpact.dev/openfisca/openfisca-france-reforms" && $CI_COMMIT_BRANCH == "master"' # This step is called
# - When a pipeline has been triggered (for example when OpenFisca-France has been updated).
# In this case a new version of OpenFisca-France-Reforms must be created if a dependency has changed.
- if: '($CI_PIPELINE_SOURCE == "api" || $CI_PIPELINE_SOURCE == "schedule" || $CI_PIPELINE_SOURCE == "trigger" || $CI_PIPELINE_SOURCE == "web") && $CI_PROJECT_URL == "https://git.leximpact.dev/openfisca/openfisca-france-reforms" && $CI_COMMIT_BRANCH == "master"'
# - When a merge request has been merged into master (ie content of OpenFisca-France-Reforms has been changed).
# In this case a new version of OpenFisca-France-Reforms must always be created.
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_PROJECT_URL == "https://git.leximpact.dev/openfisca/openfisca-france-reforms" && $CI_COMMIT_BRANCH == "master"'
before_script: before_script:
## Without apt update, apt install fails. ## Without apt update, apt install fails.
- apt update --yes - apt update --yes
...@@ -67,4 +91,13 @@ sync_with_openfisca_france: ...@@ -67,4 +91,13 @@ sync_with_openfisca_france:
## Needed when Node version changes: ## Needed when Node version changes:
- npm rebuild - npm rebuild
script: script:
- npx babel-node --extensions ".ts" -- src/scripts/sync_with_openfisca_france.ts - |
if [ "$CI_PIPELINE_SOURCE" == "push" ]; then
npx babel-node --extensions ".ts" -- src/scripts/sync_with_openfisca_france.ts --force
else
npx babel-node --extensions ".ts" -- src/scripts/sync_with_openfisca_france.ts
fi
trigger_openfisca_json_model:
stage: trigger
trigger: openfisca/json-model
[tool.poetry] [tool.poetry]
name = "openfisca-france-reforms" name = "openfisca-france-reforms"
version = "0.5.4" version = "0.0.0"
description = "Some reforms for French OpenFisca tax-benefit system" description = "Some reforms for French OpenFisca tax-benefit system"
authors = ["Emmanuel Raviart <emmanuel@raviart.com>"] authors = ["Emmanuel Raviart <emmanuel@raviart.com>"]
keywords = ["benefit", "france", "microsimulation", "reform", "social", "tax"] keywords = ["benefit", "france", "microsimulation", "reform", "social", "tax"]
... ...
......
import toml from "@ltd/j-toml" import toml from "@ltd/j-toml"
import { $, fs } from "zx" import { $, argv, fs } from "zx"
interface Package { interface Package {
name: string name: string
version: string version: string
} }
interface Project { async function latestVersionObjectFromTags() {
tool: { return (await $`git tag --list`).stdout
poetry: { .split("\n")
version: string .filter((line) => line.match(/^\d+\.\d+\.\d+$/))
} .map((version) => {
const match = version.match(/^(\d+)\.(\d+)\.(\d+)$/) as string[]
return {
major: parseInt(match[1]),
minor: parseInt(match[2]),
patch: parseInt(match[3]),
} }
})
.sort((groups1, groups2) =>
groups1.major !== groups2.major
? groups2.major - groups1.major
: groups1.minor !== groups2.minor
? groups2.minor - groups1.minor
: groups2.patch - groups1.patch,
)[0]
} }
/// Upgrade, test & push openfisca-france-reforms when openfisca-france or /// Upgrade, test & push openfisca-france-reforms when openfisca-france or
...@@ -19,23 +32,23 @@ interface Project { ...@@ -19,23 +32,23 @@ interface Project {
async function main() { async function main() {
await $`poetry update` await $`poetry update`
await $`poetry install` await $`poetry install`
await $`poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test`
if (!argv.force) {
await $`git add .` await $`git add .`
if ((await $`git diff --quiet --staged`.exitCode) === 1) { if ((await $`git diff --quiet --staged`.exitCode) === 0) {
// Repository content has not changed.
// => Ensure that job is stopped and other pipelines are not triggered.
process.exit(1)
}
}
// Dependencies of openfisca-france-reforms have changed. // Dependencies of openfisca-france-reforms have changed.
const projectTomlPath = "pyproject.toml" // Ensure that everything works.
let projectToml = await fs.readFile(projectTomlPath, "utf-8") await $`poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test`
const project = toml.parse(projectToml, "\n") as unknown as Project
const version = project.tool.poetry.version
const versionSplit = version.split(".")
const patch = parseInt(versionSplit[2] ?? "0")
const newVersion = [
versionSplit[0],
versionSplit[1],
(patch + 1).toString(),
].join(".")
// Retrieve current OpenFisca-France version
const poetryLockToml = await fs.readFile("poetry.lock") const poetryLockToml = await fs.readFile("poetry.lock")
const poetryLock = toml.parse(poetryLockToml, "\n") const poetryLock = toml.parse(poetryLockToml, "\n")
const openFiscaFrancePackage = (poetryLock.package as Package[]).find( const openFiscaFrancePackage = (poetryLock.package as Package[]).find(
...@@ -43,15 +56,18 @@ async function main() { ...@@ -43,15 +56,18 @@ async function main() {
) )
const openFiscaFranceVersion = openFiscaFrancePackage!.version const openFiscaFranceVersion = openFiscaFrancePackage!.version
// Retrieve next version of OpenFisca-France-Reforms.
const { major, minor, patch } = await latestVersionObjectFromTags()
const nextVersion = `${major}.${minor}.${patch + 1}`
// Update pyproject.toml with latest version of OpenFisca-France
// and next version of OpenFisca-France-Reforms.
const projectTomlPath = "pyproject.toml"
let projectToml = await fs.readFile(projectTomlPath, "utf-8")
projectToml = projectToml.replace( projectToml = projectToml.replace(
/^version = "(.*?)"$/m, /^version = "(.*?)"$/m,
`version = "${newVersion}"`, `version = "${nextVersion}"`,
) )
await fs.writeFile(projectTomlPath, projectToml, "utf-8")
await $`git add .`
await $`git commit -m "${newVersion} (openfisca-france@${openFiscaFranceVersion})"`
await $`git push`
projectToml = projectToml.replace( projectToml = projectToml.replace(
/^openfisca-france = "\*"$/im, /^openfisca-france = "\*"$/im,
`openfisca-france = "^${openFiscaFranceVersion}"`, `openfisca-france = "^${openFiscaFranceVersion}"`,
...@@ -59,14 +75,14 @@ async function main() { ...@@ -59,14 +75,14 @@ async function main() {
await fs.writeFile(projectTomlPath, projectToml, "utf-8") await fs.writeFile(projectTomlPath, projectToml, "utf-8")
await $`git add .` await $`git add .`
const branch = newVersion.replace(/\./g, "_") // Commit updated pyproject.tomls in a new branch tagged with version number.
const branch = nextVersion.replace(/\./g, "_")
await $`git switch -c ${branch}` await $`git switch -c ${branch}`
await $`git commit -m "Set openfisca-france version to ${openFiscaFranceVersion}."` await $`git commit -m "${nextVersion} (openfisca-france@${openFiscaFranceVersion})"`
await $`git tag -a "${newVersion}" -m "${newVersion} (openfisca-france@${openFiscaFranceVersion})"` await $`git tag -a "${nextVersion}" -m "${nextVersion} (openfisca-france@${openFiscaFranceVersion})"`
await $`git push --set-upstream --tags origin ${branch}` await $`git push --set-upstream --tags origin ${branch}`
await $`git switch master` await $`git switch master`
} }
}
main() main()
.then(() => { .then(() => {
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment