Skip to content

Commit

Permalink
refactor(ci): separate version update and validation steps in CI work…
Browse files Browse the repository at this point in the history
…flows

Refactor both pypi-publish.yml and docker-publish.yml to separate the version string update in pyproject.toml and version validation into distinct steps. This makes the workflows more maintainable and clear.
  • Loading branch information
mdbecker committed Jun 26, 2023
1 parent b849d86 commit d943048
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 47 deletions.
92 changes: 50 additions & 42 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,54 @@ jobs:
docker-publish:
needs: run-test
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Extract release version
id: release-version
run: echo "::set-output name=version::${{ github.event.release.tag_name }}"

- name: Update and validate version in pyproject.toml
run: |
# Replacing the placeholder version with the actual version from the tag
sed -i "s/version = \"0.0.0\"/version = \"${{ steps.release-version.outputs.version }}\"/" pyproject.toml
# Validate that the version follows semantic versioning
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}(-rc\.\d+)?$'; then
echo "Invalid version format. Should be semantic versioning."
exit 1
fi
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository_owner }}/gull-api
# Include release candidate tag if it's an RC version
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}.{{patch}}-${{ steps.release-version.outputs.version }},value=rc
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Check out repository
uses: actions/checkout@v3

- name: Extract release version
id: release-version
run: echo "::set-output name=version::${{ github.event.release.tag_name }}"

- name: Update version in pyproject.toml
run: sed -i "s/version = \"0.0.0\"/version = \"${{ steps.release-version.outputs.version }}\"/" pyproject.toml

- name: Validate official version format
if: "!contains(steps.release-version.outputs.version, 'rc')"
run: |
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}$'; then
echo "Invalid official version format. Should be semantic versioning."
exit 1
fi
- name: Validate pre-release version format
if: "contains(steps.release-version.outputs.version, 'rc')"
run: |
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}-rc\.\d+$'; then
echo "Invalid pre-release version format. Should be semantic versioning with pre-release tag."
exit 1
fi
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository_owner }}/gull-api
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}.{{patch}}-${{ steps.release-version.outputs.version }},value=rc
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
22 changes: 17 additions & 5 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ jobs:
needs: run-test
runs-on: ubuntu-latest
environment: release

permissions:
id-token: write

steps:
- name: Check out repository
uses: actions/checkout@v3
Expand All @@ -27,11 +28,22 @@ jobs:
id: release-version
run: echo "::set-output name=version::${{ github.event.release.tag_name }}"

- name: Update and validate version in pyproject.toml
- name: Update version in pyproject.toml
run: sed -i "s/version = \"0.0.0\"/version = \"${{ steps.release-version.outputs.version }}\"/" pyproject.toml

- name: Validate official release version
if: "!contains(steps.release-version.outputs.version, 'rc')"
run: |
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}$'; then
echo "Invalid official release version format. Should be semantic versioning."
exit 1
fi
- name: Validate pre-release version
if: contains(steps.release-version.outputs.version, 'rc')
run: |
sed -i "s/version = \"0.0.0\"/version = \"${{ steps.release-version.outputs.version }}\"/" pyproject.toml
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}(-rc\.\d+)?$'; then
echo "Invalid version format. Should be semantic versioning."
if ! echo "${{ steps.release-version.outputs.version }}" | grep -Pq '^\d+(\.\d+){2}-rc\.\d+$'; then
echo "Invalid pre-release version format. Should be semantic versioning with rc."
exit 1
fi
Expand Down

0 comments on commit d943048

Please sign in to comment.