|
| 1 | +name: Build and Deploy Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + version_type: |
| 10 | + description: "Version type to bump (patch, minor, major)" |
| 11 | + required: true |
| 12 | + default: "patch" |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - patch |
| 16 | + - minor |
| 17 | + - major |
| 18 | + |
| 19 | +env: |
| 20 | + REGISTRY: ghcr.io |
| 21 | + IMAGE_NAME: noku-team/assertoor |
| 22 | + |
| 23 | +jobs: |
| 24 | + build-and-push: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + contents: write |
| 28 | + packages: write |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@v3 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + |
| 36 | + - name: Determine new version |
| 37 | + id: versioning |
| 38 | + run: | |
| 39 | + git fetch --tags |
| 40 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 41 | + echo "Latest tag: $LATEST_TAG" |
| 42 | +
|
| 43 | + # Remove 'v' prefix if present |
| 44 | + VERSION=${LATEST_TAG#v} |
| 45 | +
|
| 46 | + # Split version into components |
| 47 | + IFS='.' read -r -a VERSION_PARTS <<< "$VERSION" |
| 48 | + MAJOR="${VERSION_PARTS[0]}" |
| 49 | + MINOR="${VERSION_PARTS[1]}" |
| 50 | + PATCH="${VERSION_PARTS[2]}" |
| 51 | +
|
| 52 | + # Increment version based on input |
| 53 | + if [[ "${{ github.event.inputs.version_type }}" == "major" ]]; then |
| 54 | + MAJOR=$((MAJOR + 1)) |
| 55 | + MINOR=0 |
| 56 | + PATCH=0 |
| 57 | + elif [[ "${{ github.event.inputs.version_type }}" == "minor" ]]; then |
| 58 | + MINOR=$((MINOR + 1)) |
| 59 | + PATCH=0 |
| 60 | + else |
| 61 | + PATCH=$((PATCH + 1)) |
| 62 | + fi |
| 63 | +
|
| 64 | + NEW_VERSION="v$MAJOR.$MINOR.$PATCH" |
| 65 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 66 | + echo "New version: $NEW_VERSION" |
| 67 | +
|
| 68 | + - name: Create and push tag |
| 69 | + if: github.event_name == 'workflow_dispatch' |
| 70 | + run: | |
| 71 | + git config --global user.name "GitHub Actions" |
| 72 | + git config --global user.email "actions@github.com" |
| 73 | + git tag ${{ steps.versioning.outputs.new_version }} |
| 74 | + git push origin ${{ steps.versioning.outputs.new_version }} |
| 75 | + echo "Created and pushed tag: ${{ steps.versioning.outputs.new_version }}" |
| 76 | +
|
| 77 | + - name: Set up Docker Buildx |
| 78 | + uses: docker/setup-buildx-action@v2 |
| 79 | + |
| 80 | + - name: Log in to GitHub Container Registry |
| 81 | + uses: docker/login-action@v2 |
| 82 | + with: |
| 83 | + registry: ${{ env.REGISTRY }} |
| 84 | + username: ${{ github.actor }} |
| 85 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + |
| 87 | + - name: Extract metadata |
| 88 | + id: meta |
| 89 | + uses: docker/metadata-action@v4 |
| 90 | + with: |
| 91 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 92 | + tags: | |
| 93 | + type=ref,event=branch |
| 94 | + type=raw,value=${{ steps.versioning.outputs.new_version }} |
| 95 | + type=raw,value=latest,enable={{is_default_branch}} |
| 96 | +
|
| 97 | + - name: Build and push Docker image |
| 98 | + uses: docker/build-push-action@v4 |
| 99 | + with: |
| 100 | + context: . |
| 101 | + push: true |
| 102 | + platforms: linux/amd64,linux/arm64 |
| 103 | + tags: ${{ steps.meta.outputs.tags }} |
| 104 | + labels: ${{ steps.meta.outputs.labels }} |
| 105 | + cache-from: type=gha |
| 106 | + cache-to: type=gha,mode=max |
0 commit comments