Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 0 additions & 140 deletions .github/workflows/Deploy.yml

This file was deleted.

234 changes: 234 additions & 0 deletions .github/workflows/release-publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
Comment thread
Baptiste-Ferrand marked this conversation as resolved.

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<<EOF'
echo "$CHANGELOG"
echo 'EOF'
} >> $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'))"
Comment thread
Baptiste-Ferrand marked this conversation as resolved.
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 }}
Comment thread
Baptiste-Ferrand marked this conversation as resolved.

- 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
Comment thread
Baptiste-Ferrand marked this conversation as resolved.
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
Loading