ci: add version file #9
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: | |
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 "actions@github.com" | |
# 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 |