Skip to content

feat: add headers for destination consumption #114

feat: add headers for destination consumption

feat: add headers for destination consumption #114

name: Publish Package to Internal Artifactory
on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]
jobs:
publish:
name: Publish to Artifactory
runs-on: ["self-hosted"]
environment: 'artifactory:sap-cloud-sdk'
if: ${{ !contains(github.server_url, 'github.com') }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
enable-cache: true
- name: Read package name and version from pyproject.toml
id: metadata
run: |
pip install toml
PKG_NAME=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['name'])")
BASE_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
echo "Base version: $BASE_VERSION"
echo "name=$PKG_NAME" >> $GITHUB_OUTPUT
echo "base_version=$BASE_VERSION" >> $GITHUB_OUTPUT
- name: Generate prerelease version
id: prerelease
run: |
BASE_VERSION="${{ steps.metadata.outputs.base_version }}"
# Get current date in YYYYMMDD format
DATE=$(date +%Y%m%d)
# Get branch name and sanitize it (remove refs/heads/ prefix and replace / with -)
BRANCH_NAME="${{ github.head_ref || github.ref_name }}"
BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/refs\/heads\///' | sed 's/\//-/g')
# Get short commit SHA
SHORT_SHA="${{ github.event.pull_request.head.sha || github.sha }}"
SHORT_SHA=${SHORT_SHA:0:7}
# Create PEP 440-compliant prerelease version
# Format: X.Y.Z.devYYYYMMDD+branch.sha
PRERELEASE_VERSION="${BASE_VERSION}.dev${DATE}+${BRANCH_NAME}.${SHORT_SHA}"
echo "Generated prerelease version: $PRERELEASE_VERSION"
echo "version=$PRERELEASE_VERSION" >> $GITHUB_OUTPUT
- name: Update version in pyproject.toml
run: |
PRERELEASE_VERSION="${{ steps.prerelease.outputs.version }}"
# Update version in pyproject.toml
python -c "
import toml
data = toml.load('pyproject.toml')
data['project']['version'] = '$PRERELEASE_VERSION'
with open('pyproject.toml', 'w') as f:
toml.dump(data, f)
"
echo "Updated pyproject.toml with version: $PRERELEASE_VERSION"
- name: Check if version already exists in Artifactory
env:
REPOSITORY_NAME: ${{ vars.REPOSITORY_NAME }}
ARTIFACTORY_URL: ${{ vars.ARTIFACTORY_URL }}
ARTIFACTORY_USER: ${{ secrets.ARTIFACTORY_USER }}
ARTIFACTORY_TOKEN: ${{ secrets.ARTIFACTORY_TOKEN }}
run: |
PKG_NAME="${{ steps.metadata.outputs.name }}"
VERSION="${{ steps.prerelease.outputs.version }}"
echo "Checking if $PKG_NAME version $VERSION exists in Artifactory..."
# Convert package name to filename format (replace hyphens with underscores)
PKG_FILE_NAME=$(echo "$PKG_NAME" | sed 's/-/_/g')
FILE_PATH="$ARTIFACTORY_URL/$REPOSITORY_NAME/$PKG_NAME/$VERSION/${PKG_FILE_NAME}-${VERSION}.tar.gz"
echo "Checking for file: $FILE_PATH"
# Check if file exists using HEAD request
HTTP_STATUS=$(curl -I -s -o /dev/null -w "%{http_code}" \
-u "$ARTIFACTORY_USER:$ARTIFACTORY_TOKEN" \
"$FILE_PATH")
if [ "$HTTP_STATUS" = "200" ]; then
echo "ERROR: Version $VERSION of $PKG_NAME already exists in Artifactory!"
echo "File found: $FILE_PATH"
echo "Please increment the version in pyproject.toml before publishing."
exit 1
else
echo "Version $VERSION is available for upload to Artifactory"
fi
- name: Build release distribution
run: uv build
- name: Publish to Artifactory
env:
REPOSITORY_NAME: ${{ vars.REPOSITORY_NAME }}
ARTIFACTORY_URL: ${{ vars.ARTIFACTORY_URL }}
ARTIFACTORY_USER: ${{ secrets.ARTIFACTORY_USER }}
ARTIFACTORY_TOKEN: ${{ secrets.ARTIFACTORY_TOKEN }}
run: |
PKG_NAME="${{ steps.metadata.outputs.name }}"
VERSION="${{ steps.prerelease.outputs.version }}"
echo "Publishing to Artifactory..."
echo "Repository URL: $ARTIFACTORY_URL/api/pypi/$REPOSITORY_NAME/"
uv publish \
--publish-url "$ARTIFACTORY_URL/api/pypi/$REPOSITORY_NAME/" \
--username "$ARTIFACTORY_USER" \
--password "$ARTIFACTORY_TOKEN" \
dist/*
echo "Successfully published $PKG_NAME version $VERSION to Artifactory!"
- name: Generate Installation Summary
if: success()
env:
REPOSITORY_NAME: ${{ vars.REPOSITORY_NAME }}
ARTIFACTORY_URL: ${{ vars.ARTIFACTORY_URL }}
run: |
PKG_NAME="${{ steps.metadata.outputs.name }}"
VERSION="${{ steps.prerelease.outputs.version }}"
BASE_VERSION="${{ steps.metadata.outputs.base_version }}"
# Write to GitHub Step Summary
cat >> $GITHUB_STEP_SUMMARY <<EOF
## ✅ Package Published Successfully
**Package:** \`$PKG_NAME\`
**Version:** \`$VERSION\`
**Base Version:** \`$BASE_VERSION\`
**Repository:** $ARTIFACTORY_URL/artifactory/api/pypi/$REPOSITORY_NAME/
---
## 📦 Installation Instructions
\`\`\`bash
# uv
uv add $PKG_NAME==$VERSION --index $ARTIFACTORY_URL/artifactory/api/pypi/$REPOSITORY_NAME/simple/
# Poetry
poetry add $PKG_NAME==$VERSION --source $ARTIFACTORY_URL/artifactory/api/pypi/$REPOSITORY_NAME/simple/
# Pip
pip install $PKG_NAME==$VERSION --index-url $ARTIFACTORY_URL/artifactory/api/pypi/$REPOSITORY_NAME/simple/
\`\`\`
---
*Published at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')*
EOF