Skip to content

ci: fix version

ci: fix version #28

Workflow file for this run

name: CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.10.6
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install -r requirements.txt
- name: Run tests
run: |
pytest -v app/tests
tag_version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.fetch_version.outputs.version }}
steps:
- uses: actions/checkout@v3
# Step to fetch the version, validate, and tag the code
- name: Fetch version and create Git tag
id: fetch_version
run: |
VERSION=$(cat VERSION)
echo "Validating version format for $VERSION..."
# Check if the version follows Semantic Versioning (MAJOR.MINOR.PATCH)
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "ERROR: The version '$VERSION' does not follow Semantic Versioning (e.g., 1.0.0)."
exit 1
fi
# Prepend 'v' if not present
if [[ "$VERSION" != v* ]]; then
VERSION="v$VERSION"
echo "Prepending 'v'. New version is $VERSION"
fi
# Check if the version already has a Git tag
if git rev-parse "$VERSION" >/dev/null 2>&1; then
echo "ERROR: Version $VERSION already has a Git tag!"
exit 1
fi
# Configure Git user for tagging
git config user.name "GitHub Actions"
git config user.email "[email protected]"
# Create the Git tag
git tag "$VERSION"
# Push the tag to the repository
git push origin "$VERSION"
# Set the version as an output using GITHUB_OUTPUT
echo "version=$VERSION" >> $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
needs: tag_version
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build and tag Docker image using the version fetched in the previous job
- name: Build and tag Docker image
run: |
docker build -t ghcr.io/inab/observatory-api:${{ needs.tag_version.outputs.version }} -t ghcr.io/inab/observatory-api:latest .
# Push Docker image to the registry
- name: Push Docker image
run: |
docker push ghcr.io/inab/observatory-api:${{ needs.tag_version.outputs.version }}
docker push ghcr.io/inab/observatory-api:latest