Skip to content

Commit

Permalink
Improve ref server docker build (#2035)
Browse files Browse the repository at this point in the history
- complete build in Dockerfile, not in ci pipeline
- Use recommended build tasks for github actions
- build multiarch with linux/amd64,linux/arm64
- docker signing support with cosign (yet disabled)
  • Loading branch information
mregen committed Jan 5, 2023
1 parent 6711523 commit 61a6108
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 50 deletions.
24 changes: 24 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ updates:
- package-ecosystem: "nuget"
directory: "/" # Location of package manifests
schedule:
interval: "daily"
interval: "weekly"
4 changes: 4 additions & 0 deletions .github/workflows/buildandtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
- '**.cs'
- '**.csproj'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'

Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
schedule:
- cron: '30 6 * * 6'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
analyze:
name: Analyze
Expand All @@ -29,7 +33,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand Down
127 changes: 91 additions & 36 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@ on:
push:
branches: [ master, release/*, docker* ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
REGISTRY: ghcr.io
IMAGE_NAME: refserver
DOCKERSIGN: false

jobs:
build:
build-and-push-image:
runs-on: ubuntu-latest
env:
IS_NOT_PR: ${{ !github.head_ref && true }}
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:

- uses: actions/checkout@v3
with:
fetch-depth: 0
Expand All @@ -27,46 +40,88 @@ jobs:
- name: Set Version
shell: pwsh
run: ./.azurepipelines/set-version.ps1


# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request' && env.DOCKERSIGN == 'true'
uses: sigstore/cosign-installer@main
with:
cosign-release: 'v1.13.1'

# https://github.com/docker/build-push-action
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Prepare Tag Repo Info
run: |
echo IMAGE_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | awk '{gsub(/-|\./, "")}1')/refserver >> $GITHUB_ENV
echo IMAGE_REPOSITORY=${{ env.REGISTRY }}/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]' | awk '{gsub(/-|\./, "")}1')/${{ env.IMAGE_NAME }} >> $GITHUB_ENV
echo IMAGE_VERSION=$(echo ${{ env.NBGV_SimpleVersion }}${{ env.NBGV_PrereleaseVersion }}) >> $GITHUB_ENV
- name: Prepare Tag Branch info
if: env.NBGV_PublicRelease == 'False'
run: |
echo IMAGE_TAG=-$(echo ${{ github.ref }} | tr '[:upper:]' '[:lower:]' | awk '{sub(/([^\/]*\/){2}/,""); gsub(/\/|_/, "-")}1') >> $GITHUB_ENV
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
if: env.IS_NOT_PR == 'true'
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build the Docker image
run: |
cd Applications/ConsoleReferenceServer
./dockerbuild.sh
docker image ls
docker image tag consolerefserver:latest ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_Version }}${{ env.NBGV_PrereleaseVersion }}${{ env.IMAGE_TAG }}
docker image tag consolerefserver:latest ghcr.io/${{ env.IMAGE_REPOSITORY }}:latest${{ env.IMAGE_TAG }}
- name: Push to GitHub Container Registry
if: env.IS_NOT_PR == 'true'
run: |
docker image push ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_Version }}${{ env.NBGV_PrereleaseVersion }}${{ env.IMAGE_TAG }}
docker image push ghcr.io/${{ env.IMAGE_REPOSITORY }}:latest${{ env.IMAGE_TAG }}
docker images ghcr.io/${{ env.IMAGE_REPOSITORY }}
- name: Prepare version and latest tag
run: |
echo TAG_BRANCH=$(echo ${{ env.IMAGE_REPOSITORY }}:${{ env.IMAGE_VERSION }}${{ env.IMAGE_TAG }}) >> $GITHUB_ENV
echo TAG_LATEST=$(echo ${{ env.IMAGE_REPOSITORY }}:latest${{ env.IMAGE_TAG }}) >> $GITHUB_ENV
- name: Push Release tags to GitHub Container Registry
- name: Prepare Release tags
if: ${{ env.NBGV_PublicRelease == 'True' && env.NBGV_PrereleaseVersion == '' && env.IS_NOT_PR == 'true' }}
run: |
docker image tag consolerefserver:latest ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_MajorMinorVersion }}
docker image push ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_MajorMinorVersion }}
docker image tag consolerefserver:latest ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_SimpleVersion }}
docker image push ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_SimpleVersion }}
docker image tag consolerefserver:latest ghcr.io/${{ env.IMAGE_REPOSITORY }}:release
docker image push ghcr.io/${{ env.IMAGE_REPOSITORY }}:release
docker images ghcr.io/${{ env.IMAGE_REPOSITORY }}
echo TAG_RELEASE=,${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_MajorMinorVersion }},${{ env.IMAGE_REPOSITORY }}:${{ env.NBGV_SimpleVersion }},${{ env.IMAGE_REPOSITORY }}:release >> $GITHUB_ENV
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.IMAGE_REPOSITORY }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v3
with:
context: .
build-args: |
Version=${{ env.NBGV_Version }}
SimpleVersion=${{ env.NBGV_SimpleVersion }}
InformationalVersion=${{ env.NBGV_AssemblyInformationalVersion }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.TAG_BRANCH }},${{ env.TAG_LATEST }}${{ env.TAG_RELEASE }}
platforms: linux/amd64,linux/arm64
file: ./Applications/ConsoleReferenceServer/Dockerfile
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Sign the resulting Docker image digest except on PRs.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: github.event_name != 'pull_request' && env.DOCKERSIGN == 'true'
env:
COSIGN_EXPERIMENTAL: "true"
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: |
echo Signing ${{ env.IMAGE_REPOSITORY }}@${{ steps.build-and-push.outputs.digest }}
echo "${{ env.IMAGE_REPOSITORY }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }}
32 changes: 29 additions & 3 deletions Applications/ConsoleReferenceServer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0
FROM mcr.microsoft.com/dotnet/runtime:6.0-bullseye-slim AS base
WORKDIR /app

COPY ./publish /publish
WORKDIR /publish
FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim AS build
WORKDIR /src

# copy csproj and restore as distinct layers
COPY *.* .
COPY ["Applications/ConsoleReferenceServer/ConsoleReferenceServer.csproj", "Applications/ConsoleReferenceServer/"]
COPY ["Stack/Opc.Ua.Core/Opc.Ua.Core.csproj", "Stack/Opc.Ua.Core/"]
COPY ["Libraries/Opc.Ua.Security.Certificates/Opc.Ua.Security.Certificates.csproj", "Libraries/Opc.Ua.Security.Certificates/"]
COPY ["Libraries/Opc.Ua.Configuration/Opc.Ua.Configuration.csproj", "Libraries/Opc.Ua.Configuration/"]
COPY ["Libraries/Opc.Ua.Server/Opc.Ua.Server.csproj", "Libraries/Opc.Ua.Server/"]
COPY ["Applications/Quickstarts.Servers/Quickstarts.Servers.csproj", "Applications/Quickstarts.Servers/"]
RUN dotnet restore "Applications/ConsoleReferenceServer/ConsoleReferenceServer.csproj" -p:NoHttps=true
COPY . .

WORKDIR "/src/Applications/ConsoleReferenceServer"
ARG Version
ARG SimpleVersion
ARG InformationalVersion
ENV OPTIONS="-c Release -f net6.0 -p:NoHttps=true -p:Dockerbuild=true -p:Version=${Version} -p:AssemblyVersion=${SimpleVersion} -p:FileVersion=${Version} -p:InformationalVersion=${InformationalVersion}"
RUN dotnet build "ConsoleReferenceServer.csproj" --no-restore ${OPTIONS} -o /app/build

FROM build AS publish
RUN dotnet publish "ConsoleReferenceServer.csproj" ${OPTIONS} -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
EXPOSE 62541
ENTRYPOINT ["dotnet", "ConsoleReferenceServer.dll"]
10 changes: 6 additions & 4 deletions Applications/ConsoleReferenceServer/dockerbuild.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
REM build a docker container of the console reference server
set buildoptions=--configuration Release -p:NoHttps=true --framework net6.0
dotnet build %buildoptions% ConsoleReferenceServer.csproj
dotnet publish %buildoptions% ConsoleReferenceServer.csproj -o ./publish
docker build -f Dockerfile -t consolerefserver .
dotnet tool install --global nbgv
for /f "delims=" %%a in ('nbgv get-version -v Version') do @set Version=%%a
for /f "delims=" %%a in ('nbgv get-version -v SimpleVersion') do @set SimpleVersion=%%a
for /f "delims=" %%a in ('nbgv get-version -v AssemblyInformationalVersion') do @set InformationalVersion=%%a
echo Version Info for docker build: %Version% %SimpleVersion% %InformationalVersion%
docker build --build-arg Version=%Version% --build-arg SimpleVersion=%SimpleVersion% --build-arg InformationalVersion=%InformationalVersion% -f .\Dockerfile -t consolerefserver .\..\..
5 changes: 1 addition & 4 deletions Applications/ConsoleReferenceServer/dockerbuild.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/bin/bash
echo build a docker container of the .NET Core reference server, without https support
buildoptions="--configuration Release -p:NoHttps=true --framework net6.0"
dotnet build $buildoptions ConsoleReferenceServer.csproj
dotnet publish $buildoptions ConsoleReferenceServer.csproj -o ./publish
sudo docker build -f Dockerfile -t consolerefserver .
sudo docker build -f Dockerfile -t consolerefserver ./../..
8 changes: 7 additions & 1 deletion version.props
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<Project>
<ItemGroup Condition="$(NBGV_PublicRelease) != 'True' AND '$(CODEQL_RUNNER)' == ''">
<ItemGroup Condition="$(NBGV_PublicRelease) != 'True' AND '$(CODEQL_RUNNER)' == '' AND '$(Dockerbuild)' != 'true'">
<!-- Note: Keep Version 3.5.119, it is known working for 4 digit versioning -->
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.119">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<!-- set version manually in docker builds -->
<PropertyGroup Condition="'$(Dockerbuild)' == 'true'">
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
<!-- NBGV_ variables are only available in cloud build -->
<PropertyGroup Condition="'$(NBGV_PublicRelease)' == 'True'">
<Version>$(NBGV_Version)$(NBGV_PrereleaseVersion)</Version>
<VersionPrefix>$(NBGV_SimpleVersion)</VersionPrefix>
Expand All @@ -14,5 +19,6 @@
<FileVersion>$(NBGV_Version)</FileVersion>
<PackageVersion>$(NBGV_Version)$(NBGV_PrereleaseVersion)</PackageVersion>
</PropertyGroup>

</Project>

0 comments on commit 61a6108

Please sign in to comment.