From d5a129d253b8aacc29f6b8678fc0f54d3664219d Mon Sep 17 00:00:00 2001 From: Baptiste-Ferrand Date: Wed, 6 Aug 2025 14:27:04 +0200 Subject: [PATCH] rename workflow for better understanding create pipeline who crate tag release and package --- .github/workflows/Deploy.yml | 140 --------------- .github/workflows/release-publish.yml | 234 ++++++++++++++++++++++++++ 2 files changed, 234 insertions(+), 140 deletions(-) delete mode 100644 .github/workflows/Deploy.yml create mode 100644 .github/workflows/release-publish.yml diff --git a/.github/workflows/Deploy.yml b/.github/workflows/Deploy.yml deleted file mode 100644 index 840b616..0000000 --- a/.github/workflows/Deploy.yml +++ /dev/null @@ -1,140 +0,0 @@ -name: Track&Train Docker CI - -on: - pull_request: - types: [closed] - branches: - - main - - develop - - 'feat/*' -jobs: - versioning: - runs-on: ubuntu-latest - permissions: - contents: write - packages: write - outputs: - version: ${{ steps.version.outputs.version }} - steps: - - name: Checkout du dépôt - uses: actions/checkout@v4 - - - name: Install toml - run: | - python -m pip install toml - - - name: Récupérer la version actuelle depuis pyproject.toml - id: get_version - run: | - VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])") - echo "VERSION=${VERSION}" >> $GITHUB_ENV - - - name: Log version actuelle - run: | - echo "La version actuelle extraite de pyproject.toml est : ${{ env.VERSION }}" - - - name: Incrémenter la version en fonction de la branche source de la PR - id: increment_version - run: | - VERSION=${{ env.VERSION }} - BRANCH_NAME="${GITHUB_HEAD_REF}" # Branche source de la PR - - # Vérifier le type de commit et incrémenter la version - case $BRANCH_NAME in - feat/*) - VERSION=$(echo $VERSION | awk -F. -v OFS=. '{$2++; $3=0; print $0}') # Incrémentation mineure pour feat/* - ;; - fix/*) - VERSION=$(echo $VERSION | awk -F. -v OFS=. '{$3++; print $0}') # Incrémentation de patch pour fix/* - ;; - *"breaking change"*) - VERSION=$(echo $VERSION | awk -F. -v OFS=. '{$1++; $2=0; $3=0; print $0}') # Incrémentation majeure pour breaking change - ;; - *) - echo "Aucun changement sur la version" - ;; - esac - - echo "VERSION=$VERSION" >> $GITHUB_ENV - - - name: Mettre à jour la version dans pyproject.toml - id: version - run: | - python -c "import toml; data = toml.load('pyproject.toml'); data['project']['version'] = '${{ env.VERSION }}'; toml.dump(data, open('pyproject.toml', 'w'))" - echo "version=${{ env.VERSION }}" >> $GITHUB_OUTPUT - - - name: Commit and push the updated pyproject.toml - run: | - git config --global user.name "GitHub Actions" - git config --global user.email "actions@github.com" - - # Ajouter les changements du fichier pyproject.toml - git add pyproject.toml - - # Committer les changements - git commit -m "Update version to ${{ steps.increment_version.outputs.version }}" - - # Pousser le commit vers la branche actuelle - git push origin ${{ github.ref }} - - setup-docker-tag: - runs-on: ubuntu-latest - outputs: - docker_tag: ${{ steps.set-tag.outputs.tag }} - steps: - - name: Définir le tag Docker en fonction de la branche de destination - id: set-tag - run: | - if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then - echo "tag=prod" >> $GITHUB_OUTPUT - elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then - echo "tag=pre-prod" >> $GITHUB_OUTPUT - else - echo "tag=dev" >> $GITHUB_OUTPUT - fi - - build-and-deploy-image: - needs: [versioning, setup-docker-tag] - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - name: Checkout du dépôt - uses: actions/checkout@v4 - - - name: Vérifier la version - run: | - echo "Version: ${{ needs.versioning.outputs.version }}" - - - name: Valider le tag généré - run: | - TAG="${{ needs.setup-docker-tag.outputs.docker_tag }}" - if [[ ! "$TAG" =~ ^[a-zA-Z0-9_.-]+$ ]]; then - echo "Invalid tag format: $TAG" - exit 1 - fi - - - name: Connexion à GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Activer Buildx - uses: docker/setup-buildx-action@v2 - - - name: Construire et pousser l'image Docker - uses: docker/build-push-action@v4 - with: - context: . - push: true - platforms: linux/amd64,linux/arm64/v8 - tags: | - ghcr.io/track-train/api:${{ needs.versioning.outputs.version }} - ghcr.io/track-train/api:latest - ghcr.io/track-train/api:${{ needs.setup-docker-tag.outputs.docker_tag }} - - -# add test job waiting test diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml new file mode 100644 index 0000000..534f336 --- /dev/null +++ b/.github/workflows/release-publish.yml @@ -0,0 +1,234 @@ +name: Version, Tag, Release & Package + +on: + pull_request: + types: [closed] + branches: + - main + - develop + +jobs: + versioning-and-release: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + outputs: + version: ${{ steps.version.outputs.version }} + full_tag: ${{ steps.version.outputs.full_tag }} + is_latest: ${{ steps.version.outputs.is_latest }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install toml + run: | + python -m pip install toml + + - name: Get current version from pyproject.toml + id: get_version + run: | + VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])") + echo "CURRENT_VERSION=${VERSION}" >> $GITHUB_ENV + + - name: Increment version based on source branch + id: increment_version + run: | + CURRENT_VERSION=${{ env.CURRENT_VERSION }} + BRANCH_NAME="${{ github.event.pull_request.head.ref }}" + TARGET_BRANCH="${{ github.base_ref }}" + + CLEAN_VERSION=$(echo $CURRENT_VERSION | sed 's/-pre-prod//') + + case $BRANCH_NAME in + feat/*|feature/*) + NEW_VERSION=$(echo $CLEAN_VERSION | awk -F. -v OFS=. '{$2++; $3=0; print $0}') + ;; + fix/*|hotfix/*) + NEW_VERSION=$(echo $CLEAN_VERSION | awk -F. -v OFS=. '{$3++; print $0}') + ;; + *breaking*|*major*) + NEW_VERSION=$(echo $CLEAN_VERSION | awk -F. -v OFS=. '{$1++; $2=0; $3=0; print $0}') + ;; + *) + NEW_VERSION=$CLEAN_VERSION + ;; + esac + + if [[ "$TARGET_BRANCH" == "develop" ]]; then + FULL_VERSION="${NEW_VERSION}-pre-prod" + IS_LATEST="false" + elif [[ "$TARGET_BRANCH" == "main" ]]; then + FULL_VERSION="${NEW_VERSION}" + IS_LATEST="true" + fi + + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV + echo "FULL_VERSION=$FULL_VERSION" >> $GITHUB_ENV + echo "IS_LATEST=$IS_LATEST" >> $GITHUB_ENV + + - name: Extract changelog from PR description + id: extract_changelog + run: | + PR_BODY='${{ github.event.pull_request.body }}' + + if echo "$PR_BODY" | grep -q "#changelog"; then + CHANGELOG=$(echo "$PR_BODY" | sed -n '/#changelog/,$p' | sed '1d' | sed '/^[[:space:]]*$/d') + else + CHANGELOG="- Update to version ${{ env.FULL_VERSION }}" + fi + + if [ -z "$CHANGELOG" ]; then + CHANGELOG="- Update to version ${{ env.FULL_VERSION }}" + fi + + { + echo 'CHANGELOG<> $GITHUB_ENV + + - name: Update version in pyproject.toml + id: version + run: | + python -c "import toml; data = toml.load('pyproject.toml'); data['project']['version'] = '${{ env.FULL_VERSION }}'; toml.dump(data, open('pyproject.toml', 'w'))" + echo "version=${{ env.NEW_VERSION }}" >> $GITHUB_OUTPUT + echo "full_tag=${{ env.FULL_VERSION }}" >> $GITHUB_OUTPUT + echo "is_latest=${{ env.IS_LATEST }}" >> $GITHUB_OUTPUT + + - name: Commit and push updated pyproject.toml + run: | + git config --global user.name "GitHub Actions" + git config --global user.email "actions@github.com" + + git add pyproject.toml + git commit -m "Update version to ${{ env.FULL_VERSION }}" + git push origin ${{ github.ref_name }} + + - name: Remove old latest tag + if: env.IS_LATEST == 'true' + run: | + if git tag -l | grep -q "^latest$"; then + git tag -d latest || true + git push origin :refs/tags/latest || true + fi + + - name: Create Git tags + run: | + git config --global user.name "GitHub Actions" + git config --global user.email "actions@github.com" + + git tag ${{ env.FULL_VERSION }} + git push origin ${{ env.FULL_VERSION }} + + if [[ "${{ env.IS_LATEST }}" == "true" ]]; then + git tag latest + git push origin latest + fi + + - name: Create GitHub Release + uses: softprops/action-gh-release@v2 + with: + tag_name: ${{ env.FULL_VERSION }} + name: "Release ${{ env.FULL_VERSION }}" + body: | + ## 📋 Changelog + + ${{ env.CHANGELOG }} + + ## 🏷️ Tags + - **Version**: `${{ env.FULL_VERSION }}`${{ env.IS_LATEST == 'true' && format('{0}- **Latest**: `latest`', ' + ') || '' }} + + ## 📦 Docker Package + + ```bash + # Pull specific version + docker pull ghcr.io/track-train/api:${{ env.FULL_VERSION }} + ${{ env.IS_LATEST == 'true' && format('{0}# Pull latest stable{0}docker pull ghcr.io/track-train/api:latest', ' + ') || '' }} + ``` + + ## ℹ️ Information + + - **Source branch**: `${{ github.event.pull_request.head.ref }}` + - **Target branch**: `${{ github.base_ref }}` + - **Release type**: ${{ env.IS_LATEST == 'true' && '**Stable** 🎯' || '**Pre-production** 🧪' }} + - **PR author**: @${{ github.event.pull_request.user.login }} + prerelease: ${{ env.IS_LATEST != 'true' }} + make_latest: ${{ env.IS_LATEST == 'true' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + build-and-deploy-image: + needs: versioning-and-release + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.ref_name }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Prepare Docker tags + id: prepare_tags + run: | + TAGS="ghcr.io/track-train/api:${{ needs.versioning-and-release.outputs.full_tag }}" + + if [[ "${{ needs.versioning-and-release.outputs.is_latest }}" == "true" ]]; then + TAGS="${TAGS},ghcr.io/track-train/api:latest" + fi + + echo "DOCKER_TAGS=${TAGS}" >> $GITHUB_ENV + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + platforms: linux/amd64,linux/arm64/v8 + tags: ${{ env.DOCKER_TAGS }} + labels: | + org.opencontainers.image.title=Track&Train API + org.opencontainers.image.description=API for Track&Train application + org.opencontainers.image.version=${{ needs.versioning-and-release.outputs.full_tag }} + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.created=${{ github.event.head_commit.timestamp }} + org.opencontainers.image.source=${{ github.event.repository.clone_url }} + + - name: Deployment summary + run: | + echo "## 🎉 Deployment successful!" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "### 📦 Package created" >> $GITHUB_STEP_SUMMARY + echo "- **Version**: \`${{ needs.versioning-and-release.outputs.full_tag }}\`" >> $GITHUB_STEP_SUMMARY + if [[ "${{ needs.versioning-and-release.outputs.is_latest }}" == "true" ]]; then + echo "- **Latest**: \`latest\`" >> $GITHUB_STEP_SUMMARY + fi + echo "" >> $GITHUB_STEP_SUMMARY + echo "### 🐳 Docker commands" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY + echo "docker pull ghcr.io/track-train/api:${{ needs.versioning-and-release.outputs.full_tag }}" >> $GITHUB_STEP_SUMMARY + if [[ "${{ needs.versioning-and-release.outputs.is_latest }}" == "true" ]]; then + echo "docker pull ghcr.io/track-train/api:latest" >> $GITHUB_STEP_SUMMARY + fi + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "### 📋 pyproject.toml" >> $GITHUB_STEP_SUMMARY + echo "- Version in pyproject.toml updated automatically" >> $GITHUB_STEP_SUMMARY \ No newline at end of file