medium.com 3 min read Original Source

Production-Grade Python CI/CD in Azure DevOps: Semantic Versioning, Tagging, and Artifact…

When you’re managing CI/CD pipelines for Python services in Azure DevOps, ensuring consistency between code, builds, releases, and artifacts is crucial. This post will show you how to:

✅ Automate versioning using __version__.py
✅ Use that version to label builds and classic releases
✅ Create Git tags based on the build
✅ Push artifacts to Azure Artifacts
✅ Push version file changes with [skip ci]
✅ Set up required permissions — the right way

Whether you’re building web services, data pipelines, or ML code — this approach works for any Python repo.

1. Store & Manage Version in __version__.py

Start by maintaining a semantic version in a central location like __version__.py:

# __version__.py
__version__ = "1.0.47"
# __version__.py
__version__ = "1.0.47"

This file should be:

  • Used in your Python code (e.g. for /healthz endpoint, version headers, or logs)
  • Read and updated by your pipeline

2. Generate New Version in CI

Use this YAML snippet to:

  • Extract the current version
  • Bump the patch
  • Update __version__.py
  • Set $(generatedVersion) for use across the pipeline
  • Update the build name

<button data-code="- task: Bash@3
displayName: "Generate & Set Version"
inputs:
targetType: 'inline'
script: |
version=$(sed -nE 's/^__version__ *= *"([^"]+)".*/1/p' __version__.py)
IFS='.' read -r major minor patch <<

- task: Bash@3
  displayName: "Generate & Set Version"
  inputs:
    targetType: 'inline'
    script: |
      version=$(sed -nE 's/^__version__ *= *"([^"]+)".*/1/p' __version__.py)
      IFS='.' read -r major minor patch <<< "$version"
      patch=$((patch + 1))
      new_version="${major}.${minor}.${patch}"
      sed -i "s/^__version__ *= *".*"/__version__ = "$new_version"/" __version__.py
      echo "##vso[task.setvariable variable=generatedVersion]$new_version"
      echo "##vso[build.updatebuildnumber]$new_version"
- task: Bash@3
  displayName: "Generate & Set Version"
  inputs:
    targetType: 'inline'
    script: |
      version=$(sed -nE 's/^__version__ *= *"([^"]+)".*/1/p' __version__.py)
      IFS='.' read -r major minor patch <<< "$version"
      patch=$((patch + 1))
      new_version="${major}.${minor}.${patch}"
      sed -i "s/^__version__ *= *".*"/__version__ = "$new_version"/" __version__.py
      echo "##vso[task.setvariable variable=generatedVersion]$new_version"
      echo "##vso[build.updatebuildnumber]$new_version"

3. Push Updated __version__.py to Git with [skip ci]

After the version bump, we want to commit the updated file back to Git. But we don’t want to trigger another pipeline, so we add [skip ci] in the commit message.

- task: Bash@3
  displayName: 'Commit Updated Version'
  inputs:
    targetType: 'inline'
    script: |
      git config user.name "Azure Pipelines"
      git config user.email "[email protected]"
      git checkout $(Build.SourceBranchName)
      git add __version__.py
      git commit -m "Bump version to $(generatedVersion) [skip ci]"
      git push origin HEAD:$(Build.SourceBranchName)
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
- task: Bash@3
  displayName: 'Commit Updated Version'
  inputs:
    targetType: 'inline'
    script: |
      git config user.name "Azure Pipelines"
      git config user.email "[email protected]"
      git checkout $(Build.SourceBranchName)
      git add __version__.py
      git commit -m "Bump version to $(generatedVersion) [skip ci]"
      git push origin HEAD:$(Build.SourceBranchName)
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

Why [skip ci]?

Including [skip ci] in your Git commit message prevents infinite CI loops. Without it, the version bump commit would re-trigger the pipeline — which would again bump the version — and so on.

💡 Azure DevOps automatically skips CI when [skip ci] or [ci skip] appears in the commit message.

4. Use Version for Build & Release Labels

Once you’ve set ##vso[build.updatebuildnumber], your build label becomes 1.0.48.

To use the same version for classic releases:

  • Go to your Release Pipeline
  • Click Options → Release name format
  • Use: $(Build.BuildNumber)

✅ Now your build and release pipelines will carry the same semantic version.

5. Tag the Commit with Generated Version

Tagging your repo allows traceability from deployed artifacts back to a commit.

- task: Bash@3
  displayName: 'Tag Commit'
  inputs:
    targetType: 'inline'
    script: |
      git config --global user.email "[email protected]"
      git config --global user.name "Azure Pipelines"
      git tag -a "v$(generatedVersion)" -m "Version $(generatedVersion)"
      git push origin "v$(generatedVersion)"
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
- task: Bash@3
  displayName: 'Tag Commit'
  inputs:
    targetType: 'inline'
    script: |
      git config --global user.email "[email protected]"
      git config --global user.name "Azure Pipelines"
      git tag -a "v$(generatedVersion)" -m "Version $(generatedVersion)"
      git push origin "v$(generatedVersion)"
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

Required Permission

To push a tag, the Project Build Service must have Contribute and Create Tag permissions on the repo.

Steps:

  1. Go to Project Settings → Repositories
  2. Select your repo → Security
  3. Under Project Build Service ():
  • Set Contribute → Allow
  • Set Create Tag → Allow

6. Publish Python Artifacts to Azure Artifacts

If you’re using poetry or setuptools, publish .whl or .tar.gz packages to Azure Artifacts:

<button data-code="- script: |
python -m pip install –upgrade pip
curl -sSL https://install.python-poetry.org | python3 –
export PATH="$HOME/.local/bin:$PATH"
poetry build
displayName: 'Build Python Artifact'
– task: TwineAuthenticate@1
inputs:
artifactFeeds: ''
displayName: 'Authenticate Twine'
– script: |
python -m pip install twine
twine upload -r dist/*
displayName: 'Upload to Azure Artifacts'” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>

- script: |
    python -m pip install --upgrade pip
    curl -sSL https://install.python-poetry.org | python3 -
    export PATH="$HOME/.local/bin:$PATH"
    poetry build
  displayName: 'Build Python Artifact'
- task: TwineAuthenticate@1
  inputs:
    artifactFeeds: '<your_project_feed>'
  displayName: 'Authenticate Twine'
- script: |
    python -m pip install twine
    twine upload -r <your_project_feed> dist/*
  displayName: 'Upload to Azure Artifacts'
- script: |
    python -m pip install --upgrade pip
    curl -sSL https://install.python-poetry.org | python3 -
    export PATH="$HOME/.local/bin:$PATH"
    poetry build
  displayName: 'Build Python Artifact'
- task: TwineAuthenticate@1
  inputs:
    artifactFeeds: '<your_project_feed>'
  displayName: 'Authenticate Twine'
- script: |
    python -m pip install twine
    twine upload -r <your_project_feed> dist/*
  displayName: 'Upload to Azure Artifacts'

Replace <your_project_feed> with the actual feed name.

7. Set Up Artifact Publishing Permissions

To publish artifacts, grant the pipeline’s identity Contributor access to the feed:

  1. Go to Azure Artifacts → your feed → Permissions
  2. Add Project Build Service (<your project>)
  3. Allow:
  • View packages
  • Push packages
  • Promote packages

8. Expose Version in /healthz or /version

Use __version__.py in your API:

from my_package.__version__ import __version__
@app.get("/healthz")
def health():
    return {"status": "ok", "version": __version__}
from my_package.__version__ import __version__
@app.get("/healthz")
def health():
    return {"status": "ok", "version": __version__}

✅ This gives a live indication of what build/version is running in production.

Why This Matters: Advantages for Developers & Teams

  • ✅ Accurate Tracking of What Was Deployed
    Every build has a unique version, and tags help you trace it easily. You can even hit a /health/version API to get the live version running in production.
  • ✅ Debugging Made Effortless
    No more “which commit is this?” confusion — you can pinpoint the source, log changes, and replicate builds.
  • ✅ Reproducible Builds
    You can re-release or promote the same artifact version to staging or production without rebuilding it.
  • ✅ Avoids Build Loops with [skip ci]
    Updating your version file doesn’t trigger a recursive pipeline run, thanks to [skip ci].
  • ✅ Audit-Ready Releases
    Tags and commit messages act as changelogs, and artifacts are retained per version.
  • ✅ Health Monitoring Ready
    Clients, testers, and support engineers can easily fetch the current deployed version.

Final Thoughts

Automating your versioning, tagging, and publishing process builds consistency, saves time, and gives your team confidence in the software lifecycle. By embedding version awareness into your pipelines and services, you turn your builds into verifiable, traceable, and deployable units of value.

It’s fast, clean, and production-ready for any Python project.