|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - Dockerfile |
| 7 | + - docker-compose.yml |
| 8 | + - docker-compose.build.yml |
| 9 | + - .dockerignore |
| 10 | + - package.json |
| 11 | + - package-lock.json |
| 12 | + - tsconfig.base.json |
| 13 | + - README.md |
| 14 | + - LICENSE |
| 15 | + - NOTICE |
| 16 | + - scripts/** |
| 17 | + - server/** |
| 18 | + - client/** |
| 19 | + - shared/** |
| 20 | + - .github/workflows/docker.yml |
| 21 | + push: |
| 22 | + branches: [main] |
| 23 | + tags: |
| 24 | + - v* |
| 25 | + paths: |
| 26 | + - Dockerfile |
| 27 | + - docker-compose.yml |
| 28 | + - docker-compose.build.yml |
| 29 | + - .dockerignore |
| 30 | + - package.json |
| 31 | + - package-lock.json |
| 32 | + - tsconfig.base.json |
| 33 | + - README.md |
| 34 | + - LICENSE |
| 35 | + - NOTICE |
| 36 | + - scripts/** |
| 37 | + - server/** |
| 38 | + - client/** |
| 39 | + - shared/** |
| 40 | + - .github/workflows/docker.yml |
| 41 | + workflow_dispatch: |
| 42 | + |
| 43 | +permissions: |
| 44 | + contents: read |
| 45 | + |
| 46 | +env: |
| 47 | + IMAGE_NAME: ghcr.io/zengliangyi/chatcrystal |
| 48 | + |
| 49 | +jobs: |
| 50 | + validate: |
| 51 | + runs-on: ubuntu-latest |
| 52 | + permissions: |
| 53 | + contents: read |
| 54 | + steps: |
| 55 | + - uses: actions/checkout@v5 |
| 56 | + with: |
| 57 | + persist-credentials: false |
| 58 | + |
| 59 | + - name: Set up Docker Buildx |
| 60 | + uses: docker/setup-buildx-action@v3 |
| 61 | + |
| 62 | + - name: Validate Compose |
| 63 | + run: docker compose config |
| 64 | + |
| 65 | + - name: Validate Compose source build override |
| 66 | + run: docker compose -f docker-compose.yml -f docker-compose.build.yml config |
| 67 | + |
| 68 | + - name: Build image |
| 69 | + run: docker build -t chatcrystal:test . |
| 70 | + |
| 71 | + - name: Smoke test CLI in image |
| 72 | + run: docker run --rm --entrypoint crystal chatcrystal:test --version |
| 73 | + |
| 74 | + - name: Smoke test container |
| 75 | + run: | |
| 76 | + docker run -d --name chatcrystal-smoke \ |
| 77 | + -e CHATCRYSTAL_CLOUD_MODE=true \ |
| 78 | + -e CHATCRYSTAL_API_TOKEN=ci-smoke-token-123456 \ |
| 79 | + -e DATA_DIR=/data \ |
| 80 | + -p 127.0.0.1::3721 \ |
| 81 | + chatcrystal:test |
| 82 | + mapped="$(docker port chatcrystal-smoke 3721/tcp)" |
| 83 | + port="${mapped##*:}" |
| 84 | + for _attempt in $(seq 1 30); do |
| 85 | + if curl -fsS "http://127.0.0.1:${port}/api/health"; then |
| 86 | + exit 0 |
| 87 | + fi |
| 88 | + sleep 1 |
| 89 | + done |
| 90 | + docker logs chatcrystal-smoke |
| 91 | + exit 1 |
| 92 | +
|
| 93 | + - name: Cleanup |
| 94 | + if: always() |
| 95 | + run: docker rm -f chatcrystal-smoke || true |
| 96 | + |
| 97 | + publish: |
| 98 | + if: github.event_name != 'pull_request' |
| 99 | + needs: validate |
| 100 | + runs-on: ubuntu-latest |
| 101 | + permissions: |
| 102 | + contents: read |
| 103 | + packages: write |
| 104 | + concurrency: |
| 105 | + group: docker-publish-${{ github.ref }} |
| 106 | + cancel-in-progress: true |
| 107 | + steps: |
| 108 | + - uses: actions/checkout@v5 |
| 109 | + with: |
| 110 | + persist-credentials: false |
| 111 | + fetch-depth: 0 |
| 112 | + |
| 113 | + - name: Verify publish ref |
| 114 | + run: | |
| 115 | + git fetch --no-tags origin main |
| 116 | + if [ "${GITHUB_REF}" = "refs/heads/main" ]; then |
| 117 | + remote_sha="$(git rev-parse origin/main)" |
| 118 | + if [ "${GITHUB_SHA}" != "${remote_sha}" ]; then |
| 119 | + echo "::error::Refusing to publish because ${GITHUB_SHA} is not the current origin/main (${remote_sha})." |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | + elif [[ "${GITHUB_REF}" == refs/tags/v* ]]; then |
| 123 | + if ! git merge-base --is-ancestor "${GITHUB_SHA}" origin/main; then |
| 124 | + echo "::error::Refusing to publish tag ${GITHUB_REF_NAME} because ${GITHUB_SHA} is not reachable from origin/main." |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | + else |
| 128 | + echo "::error::Docker image publishing is limited to main and v* tags." |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | +
|
| 132 | + - name: Set up QEMU |
| 133 | + uses: docker/setup-qemu-action@v3 |
| 134 | + |
| 135 | + - name: Set up Docker Buildx |
| 136 | + uses: docker/setup-buildx-action@v3 |
| 137 | + |
| 138 | + - name: Log in to GHCR |
| 139 | + uses: docker/login-action@v3 |
| 140 | + with: |
| 141 | + registry: ghcr.io |
| 142 | + username: ${{ github.actor }} |
| 143 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 144 | + |
| 145 | + - name: Docker metadata |
| 146 | + id: meta |
| 147 | + uses: docker/metadata-action@v5 |
| 148 | + with: |
| 149 | + images: ${{ env.IMAGE_NAME }} |
| 150 | + tags: | |
| 151 | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} |
| 152 | + type=sha,prefix=sha- |
| 153 | + type=ref,event=tag |
| 154 | + type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/v') }} |
| 155 | +
|
| 156 | + - name: Publish image |
| 157 | + id: publish |
| 158 | + uses: docker/build-push-action@v6 |
| 159 | + with: |
| 160 | + context: . |
| 161 | + platforms: linux/amd64,linux/arm64 |
| 162 | + push: true |
| 163 | + tags: ${{ steps.meta.outputs.tags }} |
| 164 | + labels: ${{ steps.meta.outputs.labels }} |
| 165 | + |
| 166 | + - name: Verify public pull |
| 167 | + run: | |
| 168 | + docker logout ghcr.io || true |
| 169 | + if ! docker pull "${IMAGE_NAME}@${{ steps.publish.outputs.digest }}"; then |
| 170 | + echo "::error::GHCR image is not publicly pullable. Open the package settings for ghcr.io/zengliangyi/chatcrystal, change visibility to Public, then rerun this workflow." |
| 171 | + exit 1 |
| 172 | + fi |
| 173 | + if [ "${GITHUB_REF}" = "refs/heads/main" ]; then |
| 174 | + docker pull "${IMAGE_NAME}:latest" |
| 175 | + fi |
0 commit comments