|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + bump_type: |
| 7 | + description: 'Version bump type (patch, minor, major)' |
| 8 | + required: true |
| 9 | + default: 'patch' |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - patch |
| 13 | + - minor |
| 14 | + - major |
| 15 | +permissions: |
| 16 | + contents: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + tag_release: |
| 20 | + needs: tag_release |
| 21 | + runs-on: ubuntu-latest |
| 22 | + env: |
| 23 | + IMAGE_TAG: ${{ needs.tag_release.outputs.new_tag }} |
| 24 | + outputs: |
| 25 | + # Define output to pass the tag to the next job |
| 26 | + new_tag: ${{ steps.tag_version.outputs.next_version }} |
| 27 | + steps: |
| 28 | + - name: Checkout code |
| 29 | + uses: actions/checkout@v4 |
| 30 | + with: |
| 31 | + # Fetch all history and tags to find the latest SemVer tag |
| 32 | + fetch-depth: 0 |
| 33 | + # Checkout the specific branch if provided, otherwise default |
| 34 | + ref: ${{ github.event.inputs.branch }} |
| 35 | + |
| 36 | + - name: Get latest SemVer tag and calculate next version |
| 37 | + id: tag_version |
| 38 | + run: | |
| 39 | + # Fetch all tags from remote just in case |
| 40 | + git fetch --tags |
| 41 | +
|
| 42 | + # Get the latest SemVer tag (handles vX.Y.Z pattern) |
| 43 | + # Filters tags, sorts them version-aware, takes the last one |
| 44 | + LATEST_TAG=$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort='v:refname' | tail -n 1) |
| 45 | +
|
| 46 | + if [ -z "$LATEST_TAG" ]; then |
| 47 | + echo "No previous SemVer tag found. Starting with v0.1.0" |
| 48 | + NEXT_VERSION="v0.1.0" |
| 49 | + # Optionally adjust starting version based on bump_type, but v0.1.0 is common start |
| 50 | + if [ "${{ github.event.inputs.bump_type }}" == "minor" ]; then |
| 51 | + NEXT_VERSION="v0.1.0" # Or maybe v0.1.0 ? Depends on convention |
| 52 | + elif [ "${{ github.event.inputs.bump_type }}" == "major" ]; then |
| 53 | + NEXT_VERSION="v1.0.0" # Or maybe v1.0.0 ? Depends on convention |
| 54 | + fi |
| 55 | + else |
| 56 | + echo "Latest tag found: $LATEST_TAG" |
| 57 | + # Remove 'v' prefix for calculation |
| 58 | + VERSION=${LATEST_TAG#v} |
| 59 | +
|
| 60 | + # Split into parts |
| 61 | + MAJOR=$(echo $VERSION | cut -d. -f1) |
| 62 | + MINOR=$(echo $VERSION | cut -d. -f2) |
| 63 | + PATCH=$(echo $VERSION | cut -d. -f3) |
| 64 | +
|
| 65 | + # Bump version based on input |
| 66 | + case "${{ github.event.inputs.bump_type }}" in |
| 67 | + patch) |
| 68 | + PATCH=$((PATCH + 1)) |
| 69 | + ;; |
| 70 | + minor) |
| 71 | + MINOR=$((MINOR + 1)) |
| 72 | + PATCH=0 |
| 73 | + ;; |
| 74 | + major) |
| 75 | + MAJOR=$((MAJOR + 1)) |
| 76 | + MINOR=0 |
| 77 | + PATCH=0 |
| 78 | + ;; |
| 79 | + *) |
| 80 | + echo "Invalid bump type: ${{ github.event.inputs.bump_type }}" |
| 81 | + exit 1 |
| 82 | + ;; |
| 83 | + esac |
| 84 | + NEXT_VERSION="v${MAJOR}.${MINOR}.${PATCH}" |
| 85 | + fi |
| 86 | +
|
| 87 | + echo "Calculated next version: $NEXT_VERSION" |
| 88 | + # Set output for subsequent steps |
| 89 | + echo "next_version=$NEXT_VERSION" >> $GITHUB_OUTPUT |
| 90 | +
|
| 91 | + - name: Create and Push Tag |
| 92 | + run: | |
| 93 | + NEXT_TAG="${{ steps.tag_version.outputs.next_version }}" |
| 94 | + COMMIT_SHA=$(git rev-parse HEAD) |
| 95 | + echo "Tagging commit $COMMIT_SHA with $NEXT_TAG" |
| 96 | +
|
| 97 | + # Create an annotated tag (recommended) |
| 98 | + git tag -a "$NEXT_TAG" -m "Release $NEXT_TAG" |
| 99 | +
|
| 100 | + # Push the tag to the remote repository |
| 101 | + git push origin "$NEXT_TAG" |
| 102 | +
|
| 103 | + - name: Verify Tag Push |
| 104 | + run: | |
| 105 | + echo "Checking if tag ${{ steps.tag_version.outputs.next_version }} exists remotely..." |
| 106 | + git ls-remote --tags origin | grep "refs/tags/${{ steps.tag_version.outputs.next_version }}" || (echo "Tag push verification failed!" && exit 1) |
| 107 | + echo "Tag successfully pushed." |
| 108 | +
|
| 109 | + build__and_push_docker_image: |
| 110 | + runs-on: ubuntu-latest |
| 111 | + permissions: |
| 112 | + packages: write |
| 113 | + contents: read |
| 114 | + |
| 115 | + steps: |
| 116 | + - name: Checkout code |
| 117 | + uses: actions/checkout@v4 |
| 118 | + |
| 119 | + - name: Login to GitHub Container Registry |
| 120 | + uses: docker/login-action@v3 |
| 121 | + with: |
| 122 | + registry: ghcr.io |
| 123 | + username: ${{ github.repository_owner }} |
| 124 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + |
| 126 | + - name: Set up Docker Buildx |
| 127 | + uses: docker/setup-buildx-action@v3 |
| 128 | + |
| 129 | + - name: Extract metadata (tags, labels) for Docker build |
| 130 | + id: meta |
| 131 | + uses: docker/metadata-action@v5 |
| 132 | + with: |
| 133 | + images: ghcr.io/${{ github.repository_owner }}/surfsense_backend |
| 134 | + tags: | |
| 135 | + type=raw,value=0.0.1 |
| 136 | +
|
| 137 | + - name: Build and push Docker image |
| 138 | + uses: docker/build-push-action@v5 |
| 139 | + with: |
| 140 | + context: ./surfsense_backend |
| 141 | + push: true |
| 142 | + tags: $IMAGE_TAG |
| 143 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments