build(ci): handle no semver tags case #2
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: Release | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
name: Build and Push Docker Images | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: write | |
id-token: write | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to GHCR | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Get Latest Version | |
id: get-version | |
run: | | |
all_tags=$(git tag -l) | |
# Filter tags that match semver pattern vMAJOR.MINOR.PATCH | |
semver_tags=() | |
semver_regex='^v([0-9]+)\.([0-9]+)\.([0-9]+)$' | |
while IFS= read -r tag; do | |
if [[ $tag =~ $semver_regex ]]; then | |
semver_tags+=("$tag") | |
fi | |
done <<< "$all_tags" | |
# Check if any semver tags were found | |
if [ ${#semver_tags[@]} -eq 0 ]; then | |
semver_tags+=("v0.0.0") | |
fi | |
# Sort semver tags | |
IFS=$'\n' sorted_semver_tags=($(sort -V <<<"${semver_tags[*]}")) | |
unset IFS | |
# Get the latest semver tag | |
latest_tag="${sorted_semver_tags[-1]}" | |
echo "Latest semver tag: $latest_tag" | |
# Extract version numbers | |
if [[ $latest_tag =~ $semver_regex ]]; then | |
major="${BASH_REMATCH[1]}" | |
minor="${BASH_REMATCH[2]}" | |
patch="${BASH_REMATCH[3]}" | |
next_minor=$((minor + 1)) | |
new_version="v${major}.${next_minor}.0" | |
else | |
echo "Error: Unable to parse the latest semver tag." | |
exit 1 | |
fi | |
echo "New version: $new_version" | |
echo "new_version=$new_version" >> $GITHUB_OUTPUT | |
- name: Build and Push Images | |
run: | | |
apps=("web" "api" "sync") | |
git_sha=${GITHUB_SHA::7} | |
for app in "${apps[@]}"; do | |
image="ghcr.io/avelinapp/$app" | |
tags=( | |
"canary" | |
"${{ steps.get-version.outputs.new_version }}" | |
"$git_sha" | |
"latest" | |
) | |
tag_args="" | |
for tag in "${tags[@]}"; do | |
tag_args="$tag_args -t $image:$tag" | |
done | |
if [ "$app" = "web" ]; then | |
build_args="--build-arg NEXT_PUBLIC_APP_URL=${{ secrets.NEXT_PUBLIC_APP_URL }} \ | |
--build-arg NEXT_PUBLIC_SYNC_URL=${{ secrets.NEXT_PUBLIC_SYNC_URL }} \ | |
--build-arg NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }}" | |
else | |
build_args="" | |
fi | |
docker buildx build -f "apps/$app/Dockerfile" $tag_args $build_args --push . | |
done | |
- name: Create Tag | |
run: | | |
git config user.name "${{ github.actor }}" | |
git config user.email "${{ github.actor }}@users.noreply.github.com" | |
git tag ${{ steps.get-version.outputs.new_version }} | |
git push origin ${{ steps.get-version.outputs.new_version }} |