Skip to content

Commit

Permalink
Remove redundant LBHPACKAGESTOKEN from GitHub Actions workflows
Browse files Browse the repository at this point in the history
Historically we've published packages from our local machines, which requires a token
to authenticate with the GitHub Packages NuGet Registry. Now we use CI to publish
packages there is a GitHub-managed token we can use instead..

> If you're using a registry that supports granular permissions, and your workflow is using a personal access token to authenticate to the registry, then we highly recommend you update your workflow to use the GITHUB_TOKEN.
>  ~ from https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions#authenticating-to-package-registries-with-granular-permissions

This change removes both `LBHPACKAGESTOKEN` and `NUGET_KEY` tokens from
the GitHub Actions workflow, replacing them where needed with the
managed `GITHUB_TOKEN` token that's automatically made available to all
jobs.

In order to keep the local development/management experience the same,
references to `LBHPACKAGESTOKEN` have been kept as-is in the Docker and
Docker Compose setup.

Docker's documentation
[suggests](https://docs.docker.com/reference/dockerfile/#arg) not to use
build arguments to pass secrets, so this change updates the `Dockerfile`
to use [secret
mounts](https://docs.docker.com/build/building/secrets/#secret-mounts),
and the recommended way to [manage secrets in docker
compose](https://docs.docker.com/compose/how-tos/use-secrets/).

Consequences

This will allow us to remove the shared secrets in GitHub Actions:

- `NUGET_KEY`
- `LBHPACKAGESTOKEN`

At the same time, this doesn't affect the local development
workflow.
  • Loading branch information
spikeheap committed Oct 1, 2024
1 parent c70fd93 commit 21d383c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ jobs:
calculate-version:
name: Calculate Version
runs-on: ubuntu-latest
env:
LBHPACKAGESTOKEN: ${{ secrets.LBHPACKAGESTOKEN }}
outputs:
version: ${{ steps.gitversion.outputs.nuGetVersionV2 }}
steps:
Expand All @@ -35,8 +33,6 @@ jobs:
name: Check code formatting
runs-on: ubuntu-latest
needs: calculate-version
env:
LBHPACKAGESTOKEN: ${{ secrets.LBHPACKAGESTOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -50,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
needs: calculate-version
env:
LBHPACKAGESTOKEN: ${{secrets.LBHPACKAGESTOKEN }}
LBHPACKAGESTOKEN: ${{secrets.GITHUB_TOKEN }}
outputs:
version: ${{ needs.calculate-version.outputs.version }}
steps:
Expand All @@ -66,7 +62,7 @@ jobs:
runs-on: ubuntu-latest
needs: build-and-test
env:
LBHPACKAGESTOKEN: ${{secrets.LBHPACKAGESTOKEN }}
LBHPACKAGESTOKEN: ${{secrets.GITHUB_TOKEN }}
VERSION: ${{ needs.build-and-test.outputs.version }}
steps:
- name: Checkout
Expand All @@ -76,4 +72,4 @@ jobs:
- name: Publish the Package
run: |
cd Hackney.Shared.HousingSearch/bin/Release
dotnet nuget push Hackney.Shared.HousingSearch.*.nupkg -s https://nuget.pkg.github.com/LBHackney-IT/index.json --api-key ${{secrets.NUGET_KEY }}
dotnet nuget push Hackney.Shared.HousingSearch.*.nupkg -s https://nuget.pkg.github.com/LBHackney-IT/index.json --api-key ${{ secrets.GITHUB_TOKEN }}
15 changes: 10 additions & 5 deletions Hackney.Shared.HousingSearch.Tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ FROM mcr.microsoft.com/dotnet/sdk:6.0

# disable microsoft telematry
ENV DOTNET_CLI_TELEMETRY_OPTOUT='true'

ARG LBHPACKAGESTOKEN
ENV LBHPACKAGESTOKEN=$LBHPACKAGESTOKEN
WORKDIR /app

# Copy csproj and restore as distinct layers
Expand All @@ -13,8 +10,16 @@ COPY ./Hackney.Shared.HousingSearch/Hackney.Shared.HousingSearch.csproj ./Hackne
COPY ./Hackney.Shared.HousingSearch.Tests/Hackney.Shared.HousingSearch.Tests.csproj ./Hackney.Shared.HousingSearch.Tests/
COPY /nuget.config /root/.nuget/NuGet/NuGet.Config

RUN dotnet restore ./Hackney.Shared.HousingSearch/Hackney.Shared.HousingSearch.csproj
RUN dotnet restore ./Hackney.Shared.HousingSearch.Tests/Hackney.Shared.HousingSearch.Tests.csproj
# We mount secrets so they can't end up in logs or build layers.
# We chain both restore commands so we only make the token available
# once and don't store it elsewhere.
# see:
# - https://docs.docker.com/reference/dockerfile/#arg
# - https://docs.docker.com/compose/how-tos/use-secrets/
RUN --mount=type=secret,id=LBHPACKAGESTOKEN \
export LBHPACKAGESTOKEN=$(cat /run/secrets/LBHPACKAGESTOKEN) && \
dotnet restore ./Hackney.Shared.HousingSearch/Hackney.Shared.HousingSearch.csproj && \
dotnet restore ./Hackney.Shared.HousingSearch.Tests/Hackney.Shared.HousingSearch.Tests.csproj

# Copy everything else and build
COPY . .
Expand Down
10 changes: 8 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ services:
build:
context: .
dockerfile: Hackney.Shared.HousingSearch.Tests/Dockerfile
args:
- LBHPACKAGESTOKEN=${LBHPACKAGESTOKEN}

# Mounts the secret at /run/secrets/LBHPACKAGESTOKEN
secrets:
- LBHPACKAGESTOKEN

secrets:
LBHPACKAGESTOKEN:
environment: LBHPACKAGESTOKEN

0 comments on commit 21d383c

Please sign in to comment.