We have a build pipeline running in GitHub Actions that builds a .NET 6 web in a Docker container and then publishes it to AWS. Within this pipeline we have a manual step where we can chose the source branch or tag and the environment it should be deployed to (dev/stg/prd). In order to make it more obvious what has been deployed where we were looking to pass the branch/tag name through to our web app where it could be displayed on the Swagger page.
Although this is pretty straightforward it took a while to figure out all the steps required.
First we need to pass the github.ref_name
through as a build argument to our Docker container. For some reason this only worked for me when it was double quoted and done as a multi-line input.
- name: Build and push
id: buildpush
uses: docker/build-push-action@v2
if: ${{ steps.setup.outputs.imageExists == 'false' }}
with:
push: true
secrets: ${{ fromJson(steps.setup.outputs.dockerParams).secrets }}
build-args: |
"DEPLOYMENT_SOURCE=${{ github.ref_name }}"
tags: ${{ fromJson(steps.setup.outputs.dockerParams).tags }}
context: ${{ fromJson(steps.setup.outputs.dockerParams).context }}
file: ./MyApi/Dockerfile
This then passes a build argument called DEPLOYMENT_SOURCE
through to the Dockerfile, within the Dockerfile this then needs to be set as an environment variable.
FROM base AS final
ARG DEPLOYMENT_SOURCE
ENV Swagger__DeploymentSource ${DEPLOYMENT_SOURCE}
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "./MyApi.Api.dll"]
This maps the build argument DEPLOYMENT_SOURCE
to an environment variable called Swagger__DeploymentSource
, this will then overwrite the value Swagger.DeploymentSource
in our appsettings.json file.
private OpenApiInfo CreateVersionInfo(ApiVersionDescription description)
{
var info = new OpenApiInfo()
{
Title = _swaggerOptions.Title,
Description = $"Deployment Source: {_swaggerOptions.DeploymentSource}</br>Commit: {Environment.GetEnvironmentVariable("BOSUN_IMAGE_TAG")}",
Version = description.ApiVersion.ToString()
};
if (description.IsDeprecated)
{
info.Description += " This API version has been deprecated.";
}
return info;
}
0 Comments