Skip to content

Instantly share code, notes, and snippets.

@vova-lantsov-dev
Last active April 12, 2024 01:41
Show Gist options
  • Save vova-lantsov-dev/092f9f700268ddeda4dafc8b9dded837 to your computer and use it in GitHub Desktop.
Save vova-lantsov-dev/092f9f700268ddeda4dafc8b9dded837 to your computer and use it in GitHub Desktop.
Build and publish .NET 8 application to GitHub Container Registry (ghcr.io) for linux/amd64 and linux/arm64
name: Build and publish API service
on:
push:
paths: [ "be/**", ".github/workflows/be.yml" ]
pull_request:
types: [ opened, synchronize, reopened ]
paths: [ "be/**", ".github/workflows/be.yml" ]
permissions:
packages: write
contents: read
jobs:
build_and_test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
working-directory: be/
- name: Run Tests
run: dotnet test --no-restore
working-directory: be/
- name: Publish test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-results
path: be/TestResults/
publish_api:
runs-on: ubuntu-latest
needs: [ build_and_test ]
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare Docker tags
id: prepare
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
ref=$(echo ${GITHUB_HEAD_REF} | tr '/_' '-')
else
ref=$(echo ${GITHUB_REF_NAME} | tr '/_' '-')
fi
echo "::set-output name=tags_list::${{ github.sha }};${ref}"
echo "::set-output name=tags_list_amd64::${{ github.sha }}-amd64;${ref}-amd64"
echo "::set-output name=tags_list_arm64::${{ github.sha }}-arm64;${ref}-arm64"
- name: Checkout code
uses: actions/checkout@v4
- name: Publish linux-x64 image to Docker
run: |
dotnet publish -c Release \
--os linux \
--arch x64 \
-p PublishProfile=DefaultContainer \
-p ContainerImageTags='"${{ steps.prepare.outputs.tags_list_amd64 }}"'
working-directory: be/src/ShareWish.WebApi/
- name: Publish linux-arm64 image to Docker
run: |
dotnet publish -c Release \
--os linux \
--arch arm64 \
-p PublishProfile=DefaultContainer \
-p ContainerImageTags='"${{ steps.prepare.outputs.tags_list_arm64 }}"'
working-directory: be/src/ShareWish.WebApi/
- name: Publish manifest images to Docker (amd64 and arm64 combined together)
run: |
DOCKER_CLI_EXPERIMENTAL=enabled
tags_arr=($(echo "${{ steps.prepare.outputs.tags_list }}" | tr ";" "\n"))
for tag in "${tags_arr[@]}"; do
docker manifest create \
ghcr.io/share-wish/api:${tag} \
ghcr.io/share-wish/api:${tag}-amd64 \
ghcr.io/share-wish/api:${tag}-arm64
docker manifest annotate \
ghcr.io/share-wish/api:${tag} \
--arch amd64 \
ghcr.io/share-wish/api:${tag}-amd64
docker manifest annotate \
ghcr.io/share-wish/api:${tag} \
--arch arm64 \
ghcr.io/share-wish/api:${tag}-arm64
docker manifest push \
ghcr.io/share-wish/api:${tag}
done
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<LangVersion>12</LangVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<!-- https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container?pivots=dotnet-8-0 -->
<PropertyGroup>
<IsPublishable>true</IsPublishable>
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
<ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:8.0-alpine3.19-composite</ContainerBaseImage>
<ContainerRegistry>ghcr.io</ContainerRegistry>
<ContainerRepository>share-wish/api</ContainerRepository>
</PropertyGroup>
<ItemGroup>
<ContainerLabel Include="org.opencontainers.image.source" Value="https://github.com/share-wish/Share-Wish"/>
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment