NOTE: Apart from
(and even then it's questionable, I'm Scottish). These are machine translated in languages I don't read. If they're terrible please contact me.
You can see how this translation was done in this article.
Friday, 13 September 2024
//Less than a minute
这是如何使用 GitHub Action 构建和将 docker 图像推到容器登记册的简单例子。
对于这个项目,我从基本的.NET Core ASP.NET项目和由骑士创建的默认杜克文件开始。
此 Docker 文件是一个多阶段的建筑, 用于构建工程, 然后将输出复制到运行时图像 。
对于这个预示, 当我使用尾风CSS时, 我还需要安装 Node.js 并运行尾风CSS 构建命令 。
# 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/*
这将下载最新版本的Node.js (在撰写时), 并将其安装到构建图像中 。
在文件的后一页
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.com/scottgal/ mostlylucidweb/blob/main/.github/workflows/docker-image.yml https://github.com/scottgal/ mostlylucidweb/blob/main/.gitub/workflows/docker-image.yml 。
此动作检查仓库, 登录到 Docker 枢纽, 设置 Docker Buildx, 缓存 Docker 层, 构建并标记 Docker 图像, 然后将图像推到 Docker 枢纽 。
在 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 }}