From cc2eb05b822bb34e6b77242a790c3cb667d1a827 Mon Sep 17 00:00:00 2001 From: rgcosta Date: Tue, 5 Aug 2025 23:06:04 +0000 Subject: [PATCH] Add .gitea/workflows/build-docker-image.yml --- .gitea/workflows/build-docker-image.yml | 119 ++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 .gitea/workflows/build-docker-image.yml diff --git a/.gitea/workflows/build-docker-image.yml b/.gitea/workflows/build-docker-image.yml new file mode 100644 index 0000000..554e026 --- /dev/null +++ b/.gitea/workflows/build-docker-image.yml @@ -0,0 +1,119 @@ +name: Build and Release Docker Image + +on: + push: + branches: + - main + tags: + - 'v*' +#on: +# release: +# # Or use [published] if you want it to trigger slightly later +# types: [created] + + +jobs: + build-and-release: + name: Build Docker Image + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 # Fetch all history for proper versioning + persist-credentials: false + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Calculate version + id: version + run: | + if [[ $GITHUB_REF == refs/tags/v* ]]; then + # Use the tag if this is a tag push + VERSION=${GITHUB_REF#refs/tags/v} + else + # For pushes to main, use the last tag + commit count + hash + VERSION=$(git describe --tags --always --dirty | sed 's/^v//') + if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then + # If no tag exists, start with 0.1.0 + commit count + COMMIT_COUNT=$(git rev-list --count HEAD) + SHORT_HASH=$(git rev-parse --short HEAD) + VERSION="0.1.0-$COMMIT_COUNT-$SHORT_HASH" + fi + fi + echo "::set-output name=version::$VERSION" + echo "VERSION=$VERSION" >> $GITHUB_ENV + - name: Debug Docker Login + run: | + echo "Trying to log in to: ${{ secrets.REGISTRY }}" + docker login ${{ secrets.REGISTRY }} -u ${{ github.repository_owner }} -p ${{ secrets.TOKEN }} + + + - name: Login to Gitea Container Registry + uses: docker/login-action@v2 + with: + registry: ${{ secrets.REGISTRY }} + username: ${{ github.repository_owner }} + password: ${{ secrets.TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: | + ${{ secrets.REGISTRY }}/${{ github.repository }}:${{ steps.version.outputs.version }} + ${{ secrets.REGISTRY }}/${{ github.repository }}:latest + + - name: Save image as tar archive + run: | + mkdir -p ./artifacts + IMAGE_NAME="${{ secrets.REGISTRY }}/${{ github.repository }}:${{ steps.version.outputs.version }}" + docker pull $IMAGE_NAME + docker save $IMAGE_NAME -o ./artifacts/docker-image-${{ steps.version.outputs.version }}.tar + gzip ./artifacts/docker-image-${{ steps.version.outputs.version }}.tar + + - name: Create Release + id: create_release + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITEA_TOKEN }} + with: + tag_name: v${{ steps.version.outputs.version }} + release_name: Release v${{ steps.version.outputs.version }} + draft: false + prerelease: false + + - name: Upload Docker image to Release + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITEA_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./artifacts/docker-image-${{ steps.version.outputs.version }}.tar.gz + asset_name: docker-image-${{ steps.version.outputs.version }}.tar.gz + asset_content_type: application/gzip + + - name: Store image locally for all pushes + if: always() + run: | + mkdir -p /var/lib/gitea/data/docker-images/${{ github.repository }} + cp ./artifacts/docker-image-${{ steps.version.outputs.version }}.tar.gz /var/lib/gitea/data/docker-images/${{ github.repository }}/ + echo "Docker image stored at: /var/lib/gitea/data/docker-images/${{ github.repository }}/docker-image-${{ steps.version.outputs.version }}.tar.gz" + + # Create version registry file + if [ ! -f /var/lib/gitea/data/docker-images/${{ github.repository }}/versions.json ]; then + echo '{"versions":[]}' > /var/lib/gitea/data/docker-images/${{ github.repository }}/versions.json + fi + + # Update versions registry + jq --arg version "${{ steps.version.outputs.version }}" \ + --arg date "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ + --arg commit "$(git rev-parse HEAD)" \ + '.versions += [{"version": $version, "date": $date, "commit": $commit}]' \ + /var/lib/gitea/data/docker-images/${{ github.repository }}/versions.json > temp.json && \ + mv temp.json /var/lib/gitea/data/docker-images/${{ github.repository }}/versions.json \ No newline at end of file