Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: docker build pipeline for ui #1416

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generic workflow to build a frontend application and push it to the GitHub Container Registry.
name: "Build docker"

on:
workflow_call:
inputs:
# Docker build arg / package name suffix: valid values are "admin-ui" or "ui"
app-name:
required: true
type: string
# Root url for the application relative to hostname
base-url:
required: true
type: string

env:
SKIP_ENV_VALIDATION: "true"
REGISTRY: "ghcr.io"

jobs:
build-and-push:
name: "Build and push Docker images to GitHub Packages"
runs-on: ubuntu-latest
env:
BUILD_APP: ${{ inputs.app-name }}
# Relative location for the package registry
REPO_NAME: ${{ github.repository }}
# Have to be build variable
NEXT_PUBLIC_BASE_URL: ${{ inputs.base-url }}

permissions:
contents: read
packages: write

steps:
- name: "Checkout code"
uses: actions/checkout@v4

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

- name: "Log in to the Container registry"
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Repository name needs to be lowercase
- name: "Create repository name"
id: repo
run: >-
echo "repo=$(
echo ${{ env.REGISTRY }}/${{ env.REPO_NAME }}-${{ env.BUILD_APP }} | tr '[:upper:]' '[:lower:]'
)" >> $GITHUB_OUTPUT

- name: "Extract ui metadata (tags, labels) for Docker"
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ steps.repo.outputs.repo }}
tags: |
type=ref,event=tag,priority=3
type=sha,prefix=,priority=2
type=ref,event=branch,priority=1
labels: |
org.opencontainers.image.title=Tilavarauspalvelu ${{ env.BUILD_APP }}
org.opencontainers.image.description=Tilavarauspalvelu ${{ env.BUILD_APP }} ${{ github.ref_name }} (${{ github.sha }}).
org.opencontainers.image.version=${{ github.ref_name }}
org.opencontainers.image.revision=${{ github.sha }}

- name: "Build container"
uses: docker/build-push-action@v6
with:
context: .
push: true
build-args: APP=${{ env.BUILD_APP }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}
# Use a shared cache to speed up builds
cache-from: type=registry,ref=${{ steps.repo.outputs.repo }}:cache
cache-to: type=registry,ref=${{ steps.repo.outputs.repo }}:cache,mode=max
36 changes: 36 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
on:
push:
branches:
- main
tags:
- "v*"
pull_request:
workflow_dispatch:

# Cancel a workflow from the same PR, branch or tag when a new workflow is triggered.
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
# required because error pages use build time env vars, not runtime env vars
# it's the same on every environment
EMAIL_VARAAMO_EXT_LINK: "https://lomake.hel.fi/varaamo-palaute"

jobs:
# Build both ui and admin-ui dockers
build-ui-docker:
name: "Build ui docker"
uses: ./.github/workflows/build-docker.yml
with:
app-name: "ui"
base-url: ""
secrets: inherit

build-admin-ui-docker:
name: "Build admin ui docker"
uses: ./.github/workflows/build-docker.yml
with:
app-name: "admin-ui"
base-url: "/kasittely"
secrets: inherit
5 changes: 0 additions & 5 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ on:
push:
branches:
- main
# TODO tags is only needed on build jobs (not on test / lint)
# move the build jobs into separate workflow
tags:
- "v*"
pull_request:
workflow_dispatch:

Expand All @@ -17,7 +13,6 @@ concurrency:

env:
SKIP_ENV_VALIDATION: "true"
REGISTRY: "ghcr.io"

jobs:
# Disabled because it broke, and no idea why.
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ COPY --from=installer --chown=nextjs:nodejs /app/apps/$APP/.next/static ./apps/$
COPY --from=installer --chown=nextjs:nodejs /app/apps/$APP/public ./apps/$APP/public
COPY --from=installer --chown=nextjs:nodejs /app/apps/$APP/next-i18next.config.js ./apps/$APP/

ENV BIN "apps/$APP/server.js"
ENV BIN="apps/$APP/server.js"
CMD ["sh", "-c", "dumb-init node $BIN"]
3 changes: 3 additions & 0 deletions Dockerfile.final
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ARG TAG=main
ARG APP=ui
FROM ghcr.io/city-of-helsinki/tilavarauspalvelu-ui-$APP:$TAG AS $APP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be tilavarauspalvelu-$APP instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because of the way the name is created
echo ${{ env.REGISTRY }}/${{ env.REPO_NAME }}-${{ env.BUILD_APP }} | tr '[:upper:]' '[:lower:]'

Otherwise have to do some regex / string magic to strip the -ui part of the repo name which I think is more fragile than repeating.

The package names always include tilavarauspalvelu-ui (as per repo name) and the app name and we end up with tilavarauspalvelu-ui-ui and tilavarauspalvelu-ui-admin-ui. I'd rather rename the apps than not use the repo name as part of the package name.

2 changes: 1 addition & 1 deletion apps/admin-ui/azure-pipelines-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ pool: Default

extends:
# Filename for pipeline to extend from the Azure DevOps Repository.
template: azure-pipelines-build-tilavarauspalvelu-admin-ui-dev.yml@tilavarauspalvelu-pipelines
template: admin-ui-build-dev.yml@tilavarauspalvelu-pipelines
2 changes: 1 addition & 1 deletion apps/admin-ui/azure-pipelines-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pool: Default

extends:
# Filename for pipeline to extend from the Azure DevOps Repository.
template: azure-pipelines-build-tilavarauspalvelu-admin-ui-release.yml@tilavarauspalvelu-pipelines
template: admin-ui-build-release.yml@tilavarauspalvelu-pipelines
2 changes: 1 addition & 1 deletion apps/ui/azure-pipelines-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ pool: Default

extends:
# Filename for pipeline to extend from the Azure DevOps Repository.
template: azure-pipelines-build-tilavarauspalvelu-ui-dev.yml@tilavarauspalvelu-pipelines
template: ui-build-dev.yml@tilavarauspalvelu-pipelines
2 changes: 1 addition & 1 deletion apps/ui/azure-pipelines-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pool: Default

extends:
# Filename for pipeline to extend from the Azure DevOps Repository.
template: azure-pipelines-build-tilavarauspalvelu-ui-release.yml@tilavarauspalvelu-pipelines
template: ui-build-release.yml@tilavarauspalvelu-pipelines
Loading