使用 GitHub 动作构建和推动 docker 图像
这是如何使用 GitHub Action 构建和将 docker 图像推到容器注册器@.的简单示例
先决条件
- 您想要构建和推动的工程 docker 文件已存在@ . @
- 此工程有一个 GitHub 仓库@. @%
- 存在一个容器登记器,可以将图像推到.
- 一个 docker 注册@' @%s 用户名和密码 @ (_ in GuitHub secrets @)}
对于这个项目,我开始的是基本的 .NET Core ASP.Net 项目和由 Rider}. 创建的默认 Dockerfile
嵌入文件
此 Docker 文件是一个多Q\ -\ 阶梯构建项目, 然后将输出复制到运行时的图像@ MS K1\
用于此 proect {,} , 当我使用 TailwindCS} ,}\ \ {我还需要安装 NOde. 并运行 LadywindCSS 构建命令@.}
# Install Node.js v20.x
RUN apt-get update && apt-get install -y curl \
&& curl -fsSL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh \
&& bash nodesource_setup.sh \
&& apt-get install -y nodejs \
&& rm -f nodesource_setup.sh \
&& rm -rf /var/lib/apt/lists/*
这将下载在写入@ ) @ 版本的诺德.}(note @ MS K2}) 时的最新 {(} 并安装到构建图像中\ .}
在文件的后一页
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
# Install Node.js v20.x
RUN apt-get update && apt-get install -y curl \
&& curl -fsSL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh \
&& bash nodesource_setup.sh \
&& apt-get install -y nodejs \
&& rm -f nodesource_setup.sh \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /src
COPY ["Mostlylucid/Mostlylucid.csproj", "Mostlylucid/"]
RUN dotnet restore "Mostlylucid/Mostlylucid.csproj"
COPY . .
WORKDIR "/src/Mostlylucid"
# Copy package.json and package-lock.json and install npm dependencies
COPY package*.json ./
RUN npm install
# Ensure npm-run-all is available and install npm dependencies
RUN npm --version
RUN npx tailwindcss -i ./src/css/main.css -o ./wwwroot/css/dist/main.css
RUN dotnet build "Mostlylucid.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
RUN dotnet publish "Mostlylucid.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Mostlylucid.dll"]
吉特胡布行动组织
这个网站的 GitHub Action 是一个简单的构建和推动动作, https @://github}.comQ}/scottgal/}最普通的网路 /BloboMS K5main{/.GithubMS7workflows@/docker}-image}MS10yml
此动作检查了 Docker HUBZ , @ 设置 Docker Buildingx\ MS K2}缓存的仓库@ , 构建并标记 Ducker 图像@ MPK4}, 然后将图像按到 Decker Hub+.{
在 docker 拼写文件中
name: Docker Image CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" | docker login -u "${{ secrets.DOCKER_HUB_USER_NAME }}" --password-stdin
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build and tag the Docker image
id: build
run: |
TIMESTAMP=$(date +%s)
echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV
docker build . --file Mostlylucid/Dockerfile --tag ${{ secrets.DOCKER_HUB_USER_NAME }}/mostlylucid:latest --tag ${{ secrets.DOCKER_HUB_USER_NAME }}/mostlylucid:$TIMESTAMP
- name: Push the Docker image to Docker Hub
run: |
docker push ${{ secrets.DOCKER_HUB_USER_NAME }}/mostlylucid:latest
docker push ${{ secrets.DOCKER_HUB_USER_NAME }}/mostlylucid:${{ env.TIMESTAMP }}
Finding related posts...