Skip to content

Commit

Permalink
add: docker build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
joonatank committed Oct 3, 2024
1 parent e8e0f93 commit ef8250c
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 10 deletions.
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 UI
org.opencontainers.image.description=Tilavarauspalvelu Customer UI ${{ 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
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

0 comments on commit ef8250c

Please sign in to comment.