# Utilizzare le azioni di GitHub per costruire e spingere l'immagine del docker

<datetime class="hidden">2024-07-30T13:30</datetime>

Questo è un semplice esempio di come usare le azioni GitHub per costruire e spingere l'immagine del docker in un registro dei contenitori.

## Prerequisite

- C'è un file docker per il progetto che volete costruire e spingere.
- C'è un riposito di GitHub per il progetto.
- C'è un registro di contenitori per spingere l'immagine a.
- Un registro di docker's username and password M SK1in GuitHub Secrets)

Per questo progetto ho iniziato con il progetto di base .NET Core ASP.NET e il Dockerfile predefinito creato da Rider.

### Dockerfile

Questo Dockerfile è un multi-build di stadio che costruisce il progetto e poi copia il risultato in un'immagine di runtime.

Per questo proect, siccome uso TailwindCSS, Ho anche bisogno di installare NodeM SK2js e far funzionare il comando di costruzione Tail windCSS .

```dockerfile
# 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/*
```

Questo scarica la più recente ( nel momento di scrivere) versione del NodeM SK2js e l'installa nell'immagine di costruzione .

Più tardi nel file.

```dockerfile

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"]
```

### Le azioni di GitHub

L'azione GitHub per questo sito è una semplice azione di costruzione e spinta che viene attivata quando si spinge verso la filiale principale.
https://githubMSC1comM SK2scottgal/mostlylucidwebMST4blobMS/mainMSP6gitHubMSSK7workflowsMMSC8dockerMPS9imageMSS10yml

Questa azione controlla il riposito, si registra nel Docker Hub, crea il Docker BuildxM SK2 archivia le strati del Docker , costruisce e etichetta l'immagine del dockerMSC4 e poi spinge l'immaginazione al DockerHubMST5

Nel docker compose file.

```yaml
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 }}
```

<!--category-- Docker, GitHub Actions -->
