Scheduled version update #10
This file contains hidden or 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: Scheduled version update | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| on: | |
| schedule: | |
| # 08:30 Europe/Paris pendant l'heure d'été (CEST = UTC+2) | |
| # mois 3-10 (mars à octobre inclus) | |
| - cron: '30 6 * 3-10 4' | |
| # 08:30 Europe/Paris pendant l'heure d'hiver (CET = UTC+1) | |
| # mois 11,12,1,2 (novembre, décembre, janvier, février) | |
| - cron: '30 7 * 11,12,1,2 4' | |
| workflow_dispatch: | |
| jobs: | |
| update-version: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v5 | |
| with: | |
| path: genesis | |
| ref: main | |
| fetch-depth: 0 | |
| - name: Get last commit SHA | |
| id: current_sha | |
| run: | | |
| cd genesis | |
| CURRENT_SHA=$( | |
| git log --pretty=format:"%H" \ | |
| --invert-grep --grep="^ci:" \ | |
| --invert-grep --grep="^Merge branch 'main'" \ | |
| --invert-grep --grep="^Revert " \ | |
| -n 1 | |
| ) | |
| echo "last commit SHA : $CURRENT_SHA" | |
| echo "current_sha=$CURRENT_SHA" >> $GITHUB_OUTPUT | |
| - name: Get last version tag and SHA | |
| id: last_tag | |
| run: | | |
| cd genesis | |
| LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") | |
| TAG_SHA=$(git rev-list -n 1 "$LAST_TAG") | |
| echo "Last version SHA : $TAG_SHA" | |
| echo "tag_sha=$TAG_SHA" >> $GITHUB_OUTPUT | |
| echo "Last version tag : $LAST_TAG" | |
| echo "last_tag=$LAST_TAG" >> $GITHUB_OUTPUT | |
| - name: Compare SHAs | |
| id: compare | |
| run: | | |
| if [ "${{ steps.current_sha.outputs.current_sha }}" = "${{ steps.last_tag.outputs.tag_sha }}" ]; then | |
| echo "No new commit since version. Exiting." | |
| exit 1 | |
| fi | |
| - name: Get version from changelog | |
| id: changelog_version | |
| run: | | |
| cd genesis | |
| FILE_VERSION=$(grep -m1 '^## ' CHANGELOG.md | sed -E 's/^## ([0-9]+\.[0-9]+\.[0-9]+).*/\1/') | |
| echo "Last version in changelog : $FILE_VERSION" | |
| echo "file_version=$FILE_VERSION" >> $GITHUB_OUTPUT | |
| - name: Determine version to use | |
| id: determine_version | |
| run: | | |
| RAW_TAG="${{ steps.last_tag.outputs.last_tag }}" | |
| RAW_TAG="${RAW_TAG#v}" | |
| IFS='.' read -r MAJOR LAST_MINOR LAST_PATCH <<< "$RAW_TAG" | |
| IFS='.' read -r FILE_MAJOR FILE_MINOR FILE_PATCH <<< "${{ steps.changelog_version.outputs.file_version }}" | |
| if [ "$FILE_MAJOR" -gt "$MAJOR" ] || \ | |
| [ "$FILE_MAJOR" -eq "$MAJOR" -a "$FILE_MINOR" -gt "$LAST_MINOR" ] || \ | |
| [ "$FILE_MAJOR" -eq "$MAJOR" -a "$FILE_MINOR" -eq "$LAST_MINOR" -a "$FILE_PATCH" -gt "$LAST_PATCH" ]; then | |
| NEW_VERSION="${{ steps.changelog_version.outputs.file_version }}" | |
| else | |
| PATCH=$((LAST_PATCH + 1)) | |
| NEW_VERSION="$MAJOR.$LAST_MINOR.$PATCH" | |
| fi | |
| echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| - name: Clone BPM | |
| uses: actions/checkout@master | |
| with: | |
| repository: InseeFr/BPM | |
| path: bpm | |
| - name: Get BPM version | |
| id: bpm-version | |
| run: | | |
| cd bpm | |
| BPM_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec) | |
| echo "bpm_version=$BPM_VERSION" >> $GITHUB_OUTPUT | |
| - name: Update files and commit | |
| run: | | |
| cd genesis | |
| BPM_VERSION="${{ steps.bpm-version.outputs.bpm_version }}" | |
| NEW_VERSION="${{ steps.determine_version.outputs.new_version }}" | |
| NEW_VERSION_NO_V="${NEW_VERSION#v}" | |
| FILE="CHANGELOG.md" | |
| #Update BPM version | |
| ##POM | |
| CURRENT_BPM_VERSION=$(grep -oPm1 "(?<=<bpm.version>)[0-9]+\.[0-9]+\.[0-9]+(?=</bpm.version>)" pom.xml) | |
| if [ "$CURRENT_BPM_VERSION" != "$BPM_VERSION" ]; then | |
| echo "BPM version changed!" | |
| sed -i "s|<bpm.version>.*</bpm.version>|<bpm.version>${BPM_VERSION}</bpm.version>|" pom.xml | |
| git add pom.xml | |
| ##Change "Changed" Changelog | |
| NEW_LINE="- BPM ${BPM_VERSION}" | |
| if awk -v ver="^## ${NEW_VERSION_NO_V}" -v line="$NEW_LINE" ' | |
| $0 ~ ver {in_ver=1; next} | |
| in_ver && /^## / {exit} | |
| in_ver && $0==line {found=1; exit} | |
| END {exit !found} | |
| ' "$FILE"; then | |
| echo "BPM version already present in changelog" | |
| else | |
| echo "Adding BPM to changelog" | |
| if awk -v ver="^## ${NEW_VERSION_NO_V}" ' | |
| $0 ~ ver {in_ver=1; next} | |
| in_ver && /^## / {exit} | |
| in_ver && /^### Updated/ {found=1; exit} | |
| END {exit !found} | |
| ' "$FILE"; then | |
| echo "Adding BPM ${BPM_VERSION} to existing updated section" | |
| awk -v ver="^## ${NEW_VERSION_NO_V}" -v newline="$NEW_LINE" ' | |
| $0 ~ ver {print; in_ver=1; next} | |
| in_ver && /^## / {in_ver=0} | |
| in_ver && !done && /^### Updated/ {print; print newline; done=1; next} | |
| {print} | |
| ' "$FILE" > tmp && mv tmp "$FILE" | |
| else | |
| ### New changed section | |
| echo "Adding BPM ${BPM_VERSION} to a new updated section" | |
| awk -v ver="^## ${NEW_VERSION_NO_V}" -v newline="$NEW_LINE" ' | |
| $0 ~ ver {print; in_ver=1; next} | |
| in_ver && /^## / && !done { | |
| print "### Updated"; print newline; in_ver=0; done=1 | |
| } | |
| {print} | |
| END { if(in_ver && !done) {print "### Updated"; print newline} } | |
| ' "$FILE" > tmp && mv tmp "$FILE" | |
| fi | |
| fi | |
| fi | |
| #Update app version | |
| ##Update pom | |
| mvn versions:set -DnewVersion="${{ steps.determine_version.outputs.new_version }}" -DprocessAllModules | |
| mvn versions:commit | |
| ## Update TODO to today in changelog | |
| TODAY=$(date +%Y-%m-%d) | |
| if grep -q '\[TODO\]' CHANGELOG.md; then | |
| sed -i "0,/\[TODO\]/s/\[TODO\]/[$TODAY]/" CHANGELOG.md | |
| else | |
| echo "No date change done in changelog" | |
| fi | |
| git add CHANGELOG.md | |
| git config user.name 'github-actions' | |
| git config user.email '[email protected]' | |
| git commit -am "chore: update version to ${{ steps.determine_version.outputs.new_version }}" || echo "No changes to commit" | |
| - name: Create Pull Request | |
| id: create_pr | |
| uses: peter-evans/create-pull-request@v7 | |
| with: | |
| path: genesis | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| base: "main" | |
| commit-message: "Update for release ${{ steps.determine_version.outputs.new_version }}" | |
| branch: "update-version-${{ steps.determine_version.outputs.new_version }}" | |
| branch-suffix: timestamp | |
| title: "Auto release ${{ steps.determine_version.outputs.new_version }}" | |
| body: "This PR updates the application to version ${{ steps.determine_version.outputs.new_version }}." | |
| labels: "Version Update - Prod" | |
| - name: Merge pull request | |
| run: | | |
| cd genesis | |
| gh pr merge --merge --auto "${{ steps.create_pr.outputs.pull-request-number }}" | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| wait-for-merge: | |
| runs-on: ubuntu-latest | |
| needs: update-version | |
| steps: | |
| - uses: actions/checkout@v5 | |
| - name: Wait for merged version commit | |
| run: | | |
| echo "Fetching main branch..." | |
| git fetch origin main | |
| while true; do | |
| LAST_VERSION_COMMIT=$(git log origin/main --pretty=format:"%H %s" | grep -v 'Merge pull request' | grep 'chore: update version to' | head -n1 | awk '{print $1}') | |
| if [ -n "$LAST_VERSION_COMMIT" ]; then | |
| echo "Found version commit: $LAST_VERSION_COMMIT" | |
| break | |
| else | |
| echo "Version commit not yet merged, sleeping 10s..." | |
| sleep 10 | |
| git fetch origin main | |
| fi | |
| done | |
| launch-create-release-workflow: | |
| needs: wait-for-merge | |
| uses: ./.github/workflows/create-release.yaml | |
| secrets: | |
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
| DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} |