From 06e93cd1f0362141ddf3c363d8ec893361768a09 Mon Sep 17 00:00:00 2001 From: Paul Schmiedmayer Date: Fri, 27 Oct 2023 16:19:52 -0700 Subject: [PATCH] Setup Docker Image Push & Docker Compose Setup (#5) # Setup Docker Image Push & Docker Compose Setup ## :gear: 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). --- .github/workflows/build-and-test.yml | 7 +- .github/workflows/docker-build-and-push.yml | 159 ++++++++++++++++++++ README.md | 8 + 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/docker-build-and-push.yml diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index c40281c..6949e23 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -14,7 +14,7 @@ on: workflow_call: jobs: - reuse_action: + reuseaction: name: REUSE Compliance Check uses: StanfordSpezi/.github/.github/workflows/reuse.yml@v2 eslint: @@ -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 diff --git a/.github/workflows/docker-build-and-push.yml b/.github/workflows/docker-build-and-push.yml new file mode 100644 index 0000000..fba8f35 --- /dev/null +++ b/.github/workflows/docker-build-and-push.yml @@ -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 }} diff --git a/README.md b/README.md index 920119d..48d4a36 100644 --- a/README.md +++ b/README.md @@ -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: