Skip to content

Commit

Permalink
Publish docker images to DockerHub (#958)
Browse files Browse the repository at this point in the history
  - Adds feature to publish the docker images of
    react website and libms to docker hub as well.
    Correctly sets the README for docker images
    on Docker Hub.
  - Refactors Github actions to improve the readability
  • Loading branch information
aryanpingle authored Oct 11, 2024
1 parent ea7ef18 commit caa92c2
Show file tree
Hide file tree
Showing 6 changed files with 546 additions and 28 deletions.
29 changes: 24 additions & 5 deletions .github/workflows/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
run: |
yarn test:int
yarn test:unit
yarn test:preview:unit
yarn test:preview:int
- name: Upload unit and integration test coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down Expand Up @@ -89,16 +91,33 @@ jobs:
outputs:
version: ${{ steps.get_version.outputs.version }}

publish-docker-image:
publish-docker-image-ghcr:
if: |
github.event_name == 'push' &&
(startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))
name: Publish Docker image
name: Publish Docker image (GHCR)
needs: [client, get_version]
uses: ./.github/workflows/docker.yml
uses: ./.github/workflows/docker-ghcr.yml
with:
registry: ghcr.io
image-name: into-cps-association/dtaas-web
version: ${{ needs.get_version.outputs.version }}
dockerfile: client.dockerfile
dockerfile: client.dockerfile
secrets: inherit

publish-docker-image-dockerhub:
if: |
github.event_name == 'push' &&
(startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))
name: Publish Docker image (DockerHub)
needs: [client, get_version]
uses: ./.github/workflows/docker-dockerhub.yml
with:
image-name: intocps/dtaas-web
version: ${{ needs.get_version.outputs.version }}
dockerfile: client.dockerfile
readme-file: client/DOCKER.md
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
80 changes: 80 additions & 0 deletions .github/workflows/docker-dockerhub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Reusable workflow for building and pushing a Docker Image to DockerHub.
#
# Username is taken from the repository secret, "DOCKERHUB_USERNAME"
# Password is taken from the repository secret, "DOCKERHUB_TOKEN"

name: Build and Push Docker Image (DockerHub)

on:
workflow_call:
inputs:
image-name:
required: true
type: string
version:
required: true
type: string
default: "latest"
dockerfile:
required: true
type: string
readme-file:
required: true
type: string
secrets:
DOCKERHUB_USERNAME:
required: true
DOCKERHUB_TOKEN:
required: true

env:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

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

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

- name: Check if version exists
id: check_version
run: |
if docker manifest inspect ${{ env.registry }}/${{ env.username }}/${{ inputs.image-name }}:${{ inputs.version }} > /dev/null 2>&1; then
echo "Version ${{ inputs.version }} already exists."
echo "exists=true" >> $GITHUB_ENV
else
echo "Version ${{ inputs.version }} does not exist."
echo "exists=false" >> $GITHUB_ENV
fi
- name: Build and push Docker image
if: env.exists == 'false'
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/${{ inputs.dockerfile }}
push: true
tags: ${{ env.registry }}/${{ env.username }}/${{ inputs.image-name }}:${{ inputs.version }}, ${{ env.registry }}/${{ env.username }}/${{ inputs.image-name }}:latest

- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ env.username }}
password: ${{ env.password }}
repository: ${{ env.username }}/${{ inputs.image-name }}
readme-filepath: ${{ inputs.readme-file }}
28 changes: 17 additions & 11 deletions .github/workflows/docker.yml → .github/workflows/docker-ghcr.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: Build and Push Docker Image
# Reusable workflow for building and pushing a Docker Image to GHCR.
#
# Username is taken from the user who scheduled the workflow
# Password is taken from the auto-generated GitHub token

name: Build and Push Docker Image (GHCR)

on:
workflow_call:
inputs:
registry:
required: false
type: string
default: "ghcr.io"
image-name:
required: true
type: string
Expand All @@ -18,6 +19,11 @@ on:
required: true
type: string

env:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
Expand All @@ -33,14 +39,14 @@ jobs:
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: ${{ env.registry }}
username: ${{ env.username }}
password: ${{ env.password }}

- name: Check if version exists
id: check_version
run: |
if docker manifest inspect ${{ inputs.registry }}/${{ inputs.image-name }}:${{ inputs.version }} > /dev/null 2>&1; then
if docker manifest inspect ${{ env.registry }}/${{ env.username }}/${{ inputs.image-name }}:${{ inputs.version }} > /dev/null 2>&1; then
echo "Version ${{ inputs.version }} already exists."
echo "exists=true" >> $GITHUB_ENV
else
Expand All @@ -50,9 +56,9 @@ jobs:
- name: Build and push Docker image
if: env.exists == 'false'
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/${{ inputs.dockerfile }}
push: true
tags: ${{ inputs.registry }}/${{ inputs.image-name }}:${{ inputs.version }}, ${{ inputs.registry }}/${{ inputs.image-name }}:latest
tags: ${{ env.registry }}/${{ env.username }}/${{ inputs.image-name }}:${{ inputs.version }}, ${{ env.registry }}/${{ env.username }}/${{ inputs.image-name }}:latest
43 changes: 31 additions & 12 deletions .github/workflows/lib-ms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,12 @@ jobs:
files: servers/lib/coverage/clover.xml
flags: lib-microservice-tests


publish-package:

publish-npm-package:
if: |
github.event_name == 'push' &&
(startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))
name: Publish to GitHub Packages
name: Publish NPM Package to GitHub Packages
runs-on: ubuntu-latest
needs: test-lib-ms
permissions:
Expand Down Expand Up @@ -164,12 +162,33 @@ jobs:
outputs:
version: ${{ steps.get_version.outputs.version }}

publish-docker-image:
name: Publish Docker image
needs: [ publish-package, get_version]
uses: ./.github/workflows/docker.yml
publish-docker-image-ghcr:
if: |
github.event_name == 'push' &&
(startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))
name: Publish Docker image (GHCR)
needs: [get_version, test-lib-ms]
uses: ./.github/workflows/docker-ghcr.yml
with:
image-name: into-cps-association/libms
version: ${{ needs.get_version.outputs.version }}
dockerfile: libms.dockerfile
secrets: inherit

publish-docker-image-dockerhub:
if: |
github.event_name == 'push' &&
(startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))
name: Publish Docker image (DockerHub)
needs: [get_version, test-lib-ms]
uses: ./.github/workflows/docker-dockerhub.yml
with:
registry: ghcr.io
image-name: into-cps-association/libms
version: ${{ needs.get_version.outputs.version }}
dockerfile: libms.dockerfile
image-name: intocps/libms
version: ${{ needs.get_version.outputs.version }}
dockerfile: libms.dockerfile
readme-file: servers/lib/DOCKER.md
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
99 changes: 99 additions & 0 deletions client/DOCKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Introduction

This is a container image for client application of
the Digital Twin as a Service (DTaaS) software.
This image can be used for providing a React single page web
application for the DTaaS.

## Authorization

The react client website uses OAuth authorization.
The [authorization page](https://into-cps-association.github.io/DTaaS/development/admin/client/auth.html)
provides details on setting up OAuth authorization for
the client application.

## Use in Docker Environment

### Create Docker Compose File

Create an empty file named `compose.client.yml` and copy
the following into the file:

```yml
services:
client:
image: intocps/dtaas-web:latest
restart: unless-stopped
volumes:
- ./config.js:/dtaas/client/build/env.js
ports:
- "4000:4000"
```
### Create Configuration
The client application requires configuration.
See the [config page](https://into-cps-association.github.io/DTaaS/development/admin/client/config.html)
for an explanation of client configuration.
The docker version of client application uses configuration
saved in `config.js` file. Please create this file
in the same file system location as that of the `compose.client.yml` file.

Create a file `config.js` with the following contents:

```js
if (typeof window !== 'undefined') {
window.env = {
REACT_APP_ENVIRONMENT: 'test',
REACT_APP_URL: 'http://localhost:4000/',
REACT_APP_URL_BASENAME: '',
REACT_APP_URL_DTLINK: '/lab',
REACT_APP_URL_LIBLINK: '',
REACT_APP_WORKBENCHLINK_VNCDESKTOP: '/tools/vnc/?password=vncpassword',
REACT_APP_WORKBENCHLINK_VSCODE: '/tools/vscode/',
REACT_APP_WORKBENCHLINK_JUPYTERLAB: '/lab',
REACT_APP_WORKBENCHLINK_JUPYTERNOTEBOOK: '',
REACT_APP_CLIENT_ID: '1be55736756190b3ace4c2c4fb19bde386d1dcc748d20b47ea8cfb5935b8446c',
REACT_APP_AUTH_AUTHORITY: 'https://gitlab.com/',
REACT_APP_REDIRECT_URI: 'http://localhost:4000/Library',
REACT_APP_LOGOUT_REDIRECT_URI: 'http://localhost:4000/',
REACT_APP_GITLAB_SCOPES: 'openid profile read_user read_repository api',
};
};
```

This default configuration
works well if you have an account on <https://gitlab.com>.
If you would like to adjust the configuration, please change this file.

### Run

Use the following commands to start and stop the container respectively:

```bash
docker compose -f compose.client.yml up -d
docker compose -f compose.client.yml down
```

The website will become available at <http://localhost:4000>.

## Missing Workspace

The docker compose only brings up the client application.
This development environment does not have user workspaces and
traefik gateway running in the background. As a consequence,
you will see the following error.

```txt
Unexpected Application Error!
404 Not Found
```

This error can be seen on the **Library** and **Digital Twins** pages.
The error is expected.

If you would like to try the complete DTaaS application, please see
localhost installation in
[docs](https://into-cps-association.github.io/DTaaS/development/admin/localhost.html).
Loading

0 comments on commit caa92c2

Please sign in to comment.