|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: |
| 7 | + - 'v*' |
| 8 | + pull_request: |
| 9 | + paths: |
| 10 | + - 'Dockerfile' |
| 11 | + - '.dockerignore' |
| 12 | + - '.github/workflows/docker.yml' |
| 13 | + - 'go.mod' |
| 14 | + - 'go.sum' |
| 15 | + - '**/*.go' |
| 16 | + |
| 17 | +env: |
| 18 | + REGISTRY: ghcr.io |
| 19 | + IMAGE_NAME: ${{ github.repository }} |
| 20 | + |
| 21 | +jobs: |
| 22 | + build: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: read |
| 26 | + # Only allow package writes for pushes to main/tags, not PRs |
| 27 | + packages: ${{ github.event_name != 'pull_request' && 'write' || 'read' }} |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout |
| 31 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 32 | + |
| 33 | + - name: Set up QEMU |
| 34 | + uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0 |
| 35 | + |
| 36 | + - name: Set up Docker Buildx |
| 37 | + uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0 |
| 38 | + |
| 39 | + - name: Log in to Container Registry |
| 40 | + if: github.event_name != 'pull_request' |
| 41 | + uses: docker/login-action@b45d80f862d83dbcd57f89517bcf500b2ab88fb2 # v4.0.0 |
| 42 | + with: |
| 43 | + registry: ${{ env.REGISTRY }} |
| 44 | + username: ${{ github.actor }} |
| 45 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 46 | + |
| 47 | + - name: Extract metadata for Docker |
| 48 | + id: meta |
| 49 | + uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0 |
| 50 | + with: |
| 51 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 52 | + tags: | |
| 53 | + # latest tag for main branch |
| 54 | + type=raw,value=latest,enable={{is_default_branch}} |
| 55 | + # version tags (v1.2.3 -> 1.2.3, v1.2.3 -> 1.2, v1.2.3 -> 1) |
| 56 | + type=semver,pattern={{version}} |
| 57 | + type=semver,pattern={{major}}.{{minor}} |
| 58 | + type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }} |
| 59 | + # sha tag for traceability |
| 60 | + type=sha,prefix=sha- |
| 61 | +
|
| 62 | + - name: Prepare build args |
| 63 | + id: build_args |
| 64 | + run: | |
| 65 | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then |
| 66 | + VERSION="${GITHUB_REF#refs/tags/}" |
| 67 | + else |
| 68 | + VERSION="dev-$(echo $GITHUB_SHA | cut -c1-8)" |
| 69 | + fi |
| 70 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 71 | + echo "commit=$(echo $GITHUB_SHA | cut -c1-8)" >> $GITHUB_OUTPUT |
| 72 | + echo "build_date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> $GITHUB_OUTPUT |
| 73 | +
|
| 74 | + - name: Build and push |
| 75 | + uses: docker/build-push-action@d08e5c354a6adb9ed34480a06d141179aa583294 # v7.0.0 |
| 76 | + with: |
| 77 | + context: . |
| 78 | + platforms: linux/amd64,linux/arm64 |
| 79 | + push: ${{ github.event_name != 'pull_request' }} |
| 80 | + tags: ${{ steps.meta.outputs.tags }} |
| 81 | + labels: ${{ steps.meta.outputs.labels }} |
| 82 | + build-args: | |
| 83 | + VERSION=${{ steps.build_args.outputs.version }} |
| 84 | + COMMIT=${{ steps.build_args.outputs.commit }} |
| 85 | + BUILD_DATE=${{ steps.build_args.outputs.build_date }} |
| 86 | + cache-from: type=gha |
| 87 | + cache-to: type=gha,mode=max |
| 88 | + |
| 89 | + - name: Test image (amd64) |
| 90 | + if: github.event_name == 'pull_request' |
| 91 | + run: | |
| 92 | + # Build single-arch for testing |
| 93 | + docker buildx build \ |
| 94 | + --platform linux/amd64 \ |
| 95 | + --load \ |
| 96 | + --tag msgvault:test \ |
| 97 | + --build-arg VERSION=test \ |
| 98 | + --build-arg COMMIT=$(echo $GITHUB_SHA | cut -c1-8) \ |
| 99 | + --build-arg BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ) \ |
| 100 | + . |
| 101 | +
|
| 102 | + # Smoke test: version command |
| 103 | + echo "--- Version test ---" |
| 104 | + docker run --rm msgvault:test version |
| 105 | +
|
| 106 | + # Smoke test: help command |
| 107 | + echo "--- Help test ---" |
| 108 | + docker run --rm msgvault:test --help |
| 109 | +
|
| 110 | + # Smoke test: init-db (creates database) |
| 111 | + echo "--- Init DB test ---" |
| 112 | + mkdir -p /tmp/msgvault-test && chmod 777 /tmp/msgvault-test |
| 113 | + docker run --rm -v /tmp/msgvault-test:/data msgvault:test init-db |
| 114 | + test -f /tmp/msgvault-test/msgvault.db || { echo "FATAL: database not created"; exit 1; } |
| 115 | + echo "Database created successfully" |
| 116 | +
|
| 117 | + # Cleanup |
| 118 | + rm -rf /tmp/msgvault-test |
0 commit comments