new versioning action #25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Pipeline | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
needs: test | ||
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 }} | ||
# Step to read the VERSION file and validate its format | ||
- name: Read and validate VERSION file | ||
id: validate_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 | ||
echo "VERSION=$VERSION" >> $GITHUB_ENV | ||
# Build and tag Docker image using the validated version | ||
- name: Build and tag Docker image | ||
run: | | ||
docker build -t ghcr.io/inab/observatory-api:${{ env.VERSION }} -t ghcr.io/inab/observatory-api:latest . | ||
env: | ||
VERSION: ${{ env.VERSION }} | ||
# Push Docker image to the registry | ||
- name: Push Docker image | ||
run: | | ||
docker push ghcr.io/inab/observatory-api:${{ env.VERSION }} | ||
docker push ghcr.io/inab/observatory-api:latest |