Skip to content
Snippets Groups Projects
Commit 33734676 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Merge branch 'rework_ci' into 'master'

Rework CI

See merge request openfisca/openfisca-france-reforms!2
parents cda7306f d69cfcfe
No related branches found
No related tags found
1 merge request!2Rework CI
Pipeline #1059 failed
......@@ -9,11 +9,12 @@ default:
stages:
- build
- sync_with_openfisca_france
- trigger
build:
stage: build
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
before_script:
## Without apt update, apt install fails.
- apt update --yes
......@@ -26,10 +27,33 @@ build:
script:
- 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:
stage: sync_with_openfisca_france
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:
## Without apt update, apt install fails.
- apt update --yes
......@@ -67,4 +91,18 @@ sync_with_openfisca_france:
## Needed when Node version changes:
- npm rebuild
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
only:
# OpenFisca-JSON-Model pipeline should only be called
# when a new version branch and version tag has been created.
# Here we assume that tags are only created for versions.
- tags
trigger: openfisca/json-model
[tool.poetry]
name = "openfisca-france-reforms"
version = "0.5.4"
version = "0.0.0"
description = "Some reforms for French OpenFisca tax-benefit system"
authors = ["Emmanuel Raviart <emmanuel@raviart.com>"]
keywords = ["benefit", "france", "microsimulation", "reform", "social", "tax"]
......
import toml from "@ltd/j-toml"
import { $, fs } from "zx"
import { $, argv, fs } from "zx"
interface Package {
name: string
version: string
}
interface Project {
tool: {
poetry: {
version: string
}
async function latestVersionObjectFromTags() {
return (await $`git tag --list`).stdout
.split("\n")
.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
......@@ -19,23 +32,23 @@ interface Project {
async function main() {
await $`poetry update`
await $`poetry install`
await $`poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test`
if (!argv.force) {
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.
const projectTomlPath = "pyproject.toml"
let projectToml = await fs.readFile(projectTomlPath, "utf-8")
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(".")
// Ensure that everything works.
await $`poetry run python -m openfisca_france_reforms.plf_2022.scripts.run_test`
// Retrieve current OpenFisca-France version
const poetryLockToml = await fs.readFile("poetry.lock")
const poetryLock = toml.parse(poetryLockToml, "\n")
const openFiscaFrancePackage = (poetryLock.package as Package[]).find(
......@@ -43,15 +56,18 @@ async function main() {
)
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(
/^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(
/^openfisca-france = "\*"$/im,
`openfisca-france = "^${openFiscaFranceVersion}"`,
......@@ -59,14 +75,14 @@ async function main() {
await fs.writeFile(projectTomlPath, projectToml, "utf-8")
await $`git add .`
const branch = newVersion.replace(/\./g, "_")
// Commit updated pyproject.toml in a new branch tagged with version number.
const branch = nextVersion.replace(/\./g, "_")
await $`git switch -c ${branch}`
await $`git commit -m "Set openfisca-france version to ${openFiscaFranceVersion}."`
await $`git tag -a "${newVersion}" -m "${newVersion} (openfisca-france@${openFiscaFranceVersion})"`
await $`git commit -m "${nextVersion} (openfisca-france@${openFiscaFranceVersion})"`
await $`git tag -a "${nextVersion}" -m "${nextVersion} (openfisca-france@${openFiscaFranceVersion})"`
await $`git push --set-upstream --tags origin ${branch}`
await $`git switch master`
}
}
main()
.then(() => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment