Publish Docker #25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish Docker | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: "Version name" | |
| required: true | |
| type: string | |
| env: | |
| # github.repository as <account>/<repo> | |
| REGISTRY: docker.io | |
| AILIYUN_REGISTRY: registry.cn-hongkong.aliyuncs.com | |
| AILIYUN_MANAGER_IMAGE_NAME: snap_emu/manager | |
| AILIYUN_API_IMAGE_NAME: snap_emu/api | |
| MANAGER_IMAGE_NAME: heltec/snapemu-manager | |
| API_IMAGE_NAME: heltec/snapemu-api | |
| jobs: | |
| build: | |
| runs-on: ubuntu-24.04 | |
| permissions: | |
| contents: read | |
| packages: write | |
| id-token: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Install cosign | |
| if: github.event_name != 'pull_request' | |
| uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0 | |
| with: | |
| cosign-release: 'v2.2.4' | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 | |
| - uses: docker/setup-qemu-action@v3.2.0 | |
| - name: Log into registry ${{ env.REGISTRY }} | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ secrets.DOCKER_USER }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Log into registry ${{ env.AILIYUN_REGISTRY }} | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 | |
| with: | |
| registry: ${{ env.AILIYUN_REGISTRY }} | |
| username: ${{ secrets.AILIYUN_DOCKER_USER }} | |
| password: ${{ secrets.AILIYUN_DOCKER_PASSWORD }} | |
| - name: Extract Docker metadata | |
| id: meta-manager | |
| uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.MANAGER_IMAGE_NAME }} | |
| - name: Extract Docker metadata | |
| id: meta-api | |
| uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.API_IMAGE_NAME }} | |
| - name: Extract Aliyun Docker metadata | |
| id: aliyun-meta-manager | |
| uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | |
| with: | |
| images: ${{ env.AILIYUN_REGISTRY }}/${{ env.AILIYUN_MANAGER_IMAGE_NAME }} | |
| - name: Extract Aliyun Docker metadata | |
| id: aliyun-meta-api | |
| uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | |
| with: | |
| images: ${{ env.AILIYUN_REGISTRY }}/${{ env.AILIYUN_API_IMAGE_NAME }} | |
| - name: Print tags | |
| env: | |
| TAGS: ${{ steps.meta-manager.outputs.tags }} ${{ steps.meta-api.outputs.tags }} | |
| run: echo "${TAGS}" | |
| - name: Setup Dockerfile | |
| run: | | |
| cat <<"EOF" > Dockerfile | |
| FROM --platform=$BUILDPLATFORM rust:1.86.0-alpine3.21 AS builder | |
| ARG TARGETPLATFORM | |
| ARG BUILDPLATFORM | |
| ENV RUST_BACKTRACE=1 | |
| ENV SNAPEMU_DEVICE_VERSION=v1 | |
| ENV SNAPEMU_API_VERSION=v1 | |
| WORKDIR /usr/src/app | |
| COPY . . | |
| RUN apk add --no-cache musl-dev pkgconfig make perl protoc openssl-dev curl patch bash python3 g++ gcompat | |
| RUN set -eux; \ | |
| case "$TARGETPLATFORM" in \ | |
| "linux/amd64") rustArch='x86_64-unknown-linux-musl';; \ | |
| "linux/arm64") rustArch='aarch64-unknown-linux-musl'; \ | |
| wget https://resource.heltec.cn/download/snapemu-dep/aarch64-linux-musl-cross.tgz; \ | |
| tar xf aarch64-linux-musl-cross.tgz; \ | |
| export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="$PWD/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc";; \ | |
| *) echo >&2 "unsupported architecture: $TARGETPLATFORM"; exit 1 ;; \ | |
| esac; \ | |
| rustup target add ${rustArch}; \ | |
| cargo build --target=${rustArch} --release; \ | |
| cp target/${rustArch}/release/snap_api /snap_api; \ | |
| cp target/${rustArch}/release/devices_manager /devices_manager; | |
| FROM alpine:3.20.0 AS api | |
| WORKDIR /usr/src/app | |
| COPY --from=builder /snap_api ./ | |
| CMD [ "./snap_api" ] | |
| FROM alpine:3.20.0 AS manager | |
| WORKDIR /usr/src/app | |
| COPY --from=builder /devices_manager ./ | |
| CMD [ "./devices_manager" ] | |
| EOF | |
| - name: Build and push manager Docker image | |
| id: build-and-push | |
| uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
| with: | |
| platforms: linux/amd64,linux/arm64/v8 | |
| context: . | |
| target: manager | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta-manager.outputs.tags }} | |
| labels: ${{ steps.meta-manager.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push api Docker image | |
| id: build-and-push-api | |
| uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
| with: | |
| platforms: linux/amd64,linux/arm64/v8 | |
| context: . | |
| target: api | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta-api.outputs.tags }} | |
| labels: ${{ steps.meta-api.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push manager Docker image | |
| id: aliyun-build-and-push | |
| uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
| with: | |
| platforms: linux/amd64,linux/arm64/v8 | |
| context: . | |
| target: manager | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.aliyun-meta-manager.outputs.tags }} | |
| labels: ${{ steps.aliyun-meta-manager.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push api Docker image | |
| id: aliyun-build-and-push-api | |
| uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
| with: | |
| platforms: linux/amd64,linux/arm64/v8 | |
| context: . | |
| target: api | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.aliyun-meta-api.outputs.tags }} | |
| labels: ${{ steps.aliyun-meta-api.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |