Skip to content

Update GitHub Actions workflow to build & publish Docker image to GHCR #229

@nanotaboada

Description

@nanotaboada

Description

We want to automate Docker image builds and publish them to GitHub Container Registry (GHCR) on every push to the master branch. This improves traceability, deployment consistency, and aligns with our CI/CD efforts.

Proposed Solution

  • Extend our existing GitHub Actions workflow with a new container job.
  • This job will:
    • Log in to GHCR using the GITHUB_TOKEN.
    • Build the Docker image from the root Dockerfile.
    • Tag the image with latest, main, and a sha-<commit> tag.
    • Push the image to ghcr.io/nanotaboada/dotnet-samples-aspnetcore-webapi.

Suggested Implementation

Add a new job to the CI workflow (.github/workflows/dotnet.yml):

container:
  needs: coverage
  runs-on: ubuntu-latest

  permissions:
    contents: read
    packages: write

  steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Log in to GitHub Container Registry
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3

    - name: Build and push Docker image to GHCR
      uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        tags: |
          ghcr.io/${{ github.repository }}:latest
          ghcr.io/${{ github.repository }}:main
          ghcr.io/${{ github.repository }}:sha-${{ github.sha }}

Acceptance Criteria

  • Docker job runs after coverage
  • Image is pushed to GHCR with latest, main, and sha-<commit> tags
  • Public package appears under https://github.com/users/nanotaboada/packages
  • Any errors during build or push fail the CI pipeline

Metadata

Metadata

Assignees

Labels

.NETPull requests that update .NET codecontainersPull requests that update containers codegithub_actionsPull requests that update GitHub Actions code

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions