跳过正文

使用GitHub Action构建并推送docker image

·179 字·1 分钟
目录

示例
#

创建Cloudflare wrangler的镜像用于部署hugo

FROM node:24.6.0-slim

ARG app_version

SHELL ["/bin/bash", "-c"]

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    git \
    unzip \
    bash \
    ca-certificates \
 && curl -sSL -o /tmp/hugo.tar.gz https://github.com/gohugoio/hugo/releases/download/v${app_version}/hugo_extended_${app_version}_linux-amd64.tar.gz \
 && tar -xzf /tmp/hugo.tar.gz -C /tmp \
 && mv /tmp/hugo /usr/local/bin/hugo \
 && chmod +x /usr/local/bin/hugo \
 && rm -rf /var/lib/apt/lists/* /tmp/*

RUN npm install -g wrangler

WORKDIR /app
name: Build and Push Docker Image

on:
  push:

env:
  IMAGE_NAME: ghcr.io/${{ github.repository }}
  VERSION: 0.148.2

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v5
      
      - name: Docker metadata
        id: meta
        uses: docker/metadata-action@v5.8.0
        with:
          images: |
            name=ghcr.io/${{ github.repository }}
          tags: |
            type=semver,pattern={{version}},value=v${{ env.VERSION }}
            type=semver,pattern={{major}}.{{minor}},value=v${{ env.VERSION }}
            type=semver,pattern={{major}},value=v${{ env.VERSION }}

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@v6.18.0
        with:
          push: true
          context: .
          file: Dockerfile
          # platforms: linux/386,linux/amd64,linux/arm,linux/arm64
          pull: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            app_version=${{ env.VERSION }}