Skip to content

Commit

Permalink
Setup Docker Image Push & Docker Compose Setup (#5)
Browse files Browse the repository at this point in the history
# Setup Docker Image Push & Docker Compose Setup

## ⚙️ Release Notes 
- Setup Docker Image Push
- Docker Compose Setup

### Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
PSchmiedmayer committed Oct 27, 2023
1 parent cbda384 commit 06e93cd
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
workflow_call:

jobs:
reuse_action:
reuseaction:
name: REUSE Compliance Check
uses: StanfordSpezi/.github/.github/workflows/reuse.yml@v2
eslint:
Expand All @@ -23,3 +23,8 @@ jobs:
testandcoverage:
name: Test and Coverage
uses: ./.github/workflows/npm-test-and-coverage.yml
dockerimage:
name: Docker Build and Push
uses: ./.github/workflows/docker-build-and-push.yml
with:
imageName: stanfordbdhg/typescripttemplate
159 changes: 159 additions & 0 deletions .github/workflows/docker-build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#
# This source file is part of the Stanford Biodesign Digital Health TypeScript Template open-source project
# Based on the Apodini workflow found at: https://github.com/Apodini/.github/workflows/docker-build-and-push.yml
# and the docker documentation found at https://docs.docker.com/build/ci/github-actions/multi-platform/
#
# SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
#
# SPDX-License-Identifier: MIT
#

name: Docker Build and Push

on:
workflow_call:
inputs:
dockerFile:
description: 'Path or name of the Docker file. The default values is `Dockerfile`. The docker file can use the `baseimage` to get an architecture specific Swift base image'
required: false
type: string
default: 'Dockerfile'
imageName:
description: 'The name used to tag the docker image on the defined registry containing the organzation/account name and the name of the image, e.g.: stanfordbdhg/example'
required: true
type: string
registry:
description: 'Server address of Docker registry. If not set then will default to ghcr.io'
required: false
type: string
default: 'ghcr.io'
workingDirectory:
description: 'The working-directory of the GitHub Action. Defaults to $GITHUB_WORKSPACE'
required: false
type: string
default: '.'
secrets:
username:
description: 'Username for authenticating to the Docker registry. Uses the GitHub actor by default.'
required: false
password:
description: 'Password or personal access token for authenticating the Docker registry. Uses the GitHub token by default.'
required: false

permissions:
contents: read
packages: write

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.workingDirectory }}
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
steps:
- uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ inputs.imageName }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Setup Credentials
id: credentials
run: |
USERNAME=${{ secrets.username }}
PASSWORD=${{ secrets.password }}
if [ -z "$USERNAME" ]; then
USERNAME=${{ github.actor }}
fi
if [ -z "$PASSWORD" ]; then
PASSWORD=${{ secrets.GITHUB_TOKEN }}
fi
echo "username=$USERNAME" >> "$GITHUB_OUTPUT"
echo "password=$PASSWORD" >> "$GITHUB_OUTPUT"
- name: Log in to ${{ inputs.registry }}
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ steps.credentials.outputs.username }}
password: ${{ steps.credentials.outputs.password }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ${{ inputs.dockerFile }}
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ inputs.registry }}/${{ inputs.imageName }},push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v3
with:
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: ubuntu-latest
needs:
- build
steps:
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ inputs.imageName }}
- name: Setup Credentials
id: credentials
run: |
USERNAME=${{ secrets.username }}
PASSWORD=${{ secrets.password }}
if [ -z "$USERNAME" ]; then
USERNAME=${{ github.actor }}
fi
if [ -z "$PASSWORD" ]; then
PASSWORD=${{ secrets.GITHUB_TOKEN }}
fi
echo "username=$USERNAME" >> "$GITHUB_OUTPUT"
echo "password=$PASSWORD" >> "$GITHUB_OUTPUT"
- name: Log in to ${{ inputs.registry }}
uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ steps.credentials.outputs.username }}
password: ${{ steps.credentials.outputs.password }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t ${{ inputs.registry }}/" + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ inputs.registry }}/${{ inputs.imageName }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ inputs.registry }}/${{ inputs.imageName }}:${{ steps.meta.outputs.version }}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the

You can edit the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

## Using Docker

1. [Install Docker](https://docs.docker.com/get-docker/) on your machine.
2. Build your container: `docker build -t nextjs-docker .`.
3. Run your container: `docker run -p 3000:3000 nextjs-docker`.

You can view your images created with `docker images`.

## Learn More

To learn more about Next.js, take a look at the following resources:
Expand Down

0 comments on commit 06e93cd

Please sign in to comment.