From 64f4f92f8d215380fae842912d53c6c99c12da1e Mon Sep 17 00:00:00 2001 From: Guru <19eucs071@skcet.ac.in> Date: Tue, 17 Dec 2024 02:06:29 +0530 Subject: [PATCH 1/3] Update README.md --- README.md | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/README.md b/README.md index 612f61a..fdd9328 100644 --- a/README.md +++ b/README.md @@ -44,22 +44,3 @@ npm install ```bash node index.js ``` - -## Docker Image Distribution - -To share the Docker image: - -1. Build the image: -```bash -docker build -t discord-bot . -``` - -2. Save the image to a file: -```bash -docker save discord-bot > discord-bot.tar -``` - -3. Share the `discord-bot.tar` file with others. They can load it using: -```bash -docker load < discord-bot.tar -``` \ No newline at end of file From b9afdad07ee98aca2981a147cf5895965d6d1e81 Mon Sep 17 00:00:00 2001 From: Slava <20563034+veaceslavdoina@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:46:36 +0200 Subject: [PATCH 2/3] Add Docker builds (#1) (#2) --- .github/workflows/docker-reusable.yml | 178 ++++++++++++++++++++++++++ .github/workflows/docker.yml | 29 +++++ Dockerfile | 34 +++-- 3 files changed, 230 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/docker-reusable.yml create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker-reusable.yml b/.github/workflows/docker-reusable.yml new file mode 100644 index 0000000..6047072 --- /dev/null +++ b/.github/workflows/docker-reusable.yml @@ -0,0 +1,178 @@ +name: Docker - Reusable + + +on: + workflow_call: + inputs: + docker_file: + default: Dockerfile + description: Dockerfile + required: false + type: string + docker_repo: + default: codexstorage/discord-bot + description: DockerHub repository + required: false + type: string + tag_latest: + default: true + description: Set latest tag for Docker images + required: false + type: boolean + tag_sha: + default: true + description: Set Git short commit as Docker tag + required: false + type: boolean + tag_suffix: + default: '' + description: Suffix for Docker images tag + required: false + type: string + + +env: + DOCKER_FILE: ${{ inputs.docker_file }} + DOCKER_REPO: ${{ inputs.docker_repo }} + TAG_LATEST: ${{ inputs.tag_latest }} + TAG_SHA: ${{ inputs.tag_sha }} + TAG_SUFFIX: ${{ inputs.tag_suffix }} + + +jobs: + # Build platform specific image + build: + strategy: + fail-fast: true + matrix: + target: + - os: linux + arch: amd64 + - os: linux + arch: arm64 + include: + - target: + os: linux + arch: amd64 + builder: ubuntu-22.04 + - target: + os: linux + arch: arm64 + builder: buildjet-4vcpu-ubuntu-2204-arm + + name: Build ${{ matrix.target.os }}/${{ matrix.target.arch }} + runs-on: ${{ matrix.builder }} + env: + PLATFORM: ${{ format('{0}/{1}', 'linux', matrix.target.arch) }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker - Meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.DOCKER_REPO }} + + - name: Docker - Set up Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker - Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Docker - Build and Push by digest + id: build + uses: docker/build-push-action@v6 + with: + context: . + file: ${{ env.DOCKER_FILE }} + platforms: ${{ env.PLATFORM }} + push: true + labels: ${{ steps.meta.outputs.labels }} + outputs: type=image,name=${{ env.DOCKER_REPO }},push-by-digest=true,name-canonical=true,push=true + + - name: Docker - Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Docker - Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-${{ matrix.target.arch }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + + # Publish multi-platform image + publish: + name: Publish multi-platform image + runs-on: ubuntu-latest + needs: build + steps: + - name: Docker - Variables + run: | + # Adjust custom suffix when set and + if [[ -n "${{ env.TAG_SUFFIX }}" ]]; then + echo "TAG_SUFFIX=-${{ env.TAG_SUFFIX }}" >>$GITHUB_ENV + fi + # Disable SHA tags on tagged release + if [[ ${{ startsWith(github.ref, 'refs/tags/') }} == "true" ]]; then + echo "TAG_SHA=false" >>$GITHUB_ENV + fi + # Handle latest and latest-custom using raw + if [[ ${{ env.TAG_SHA }} == "false" ]]; then + echo "TAG_LATEST=false" >>$GITHUB_ENV + echo "TAG_RAW=true" >>$GITHUB_ENV + if [[ -z "${{ env.TAG_SUFFIX }}" ]]; then + echo "TAG_RAW_VALUE=latest" >>$GITHUB_ENV + else + echo "TAG_RAW_VALUE=latest-{{ env.TAG_SUFFIX }}" >>$GITHUB_ENV + fi + else + echo "TAG_RAW=false" >>$GITHUB_ENV + fi + + - name: Docker - Download digests + uses: actions/download-artifact@v4 + with: + pattern: digests-* + merge-multiple: true + path: /tmp/digests + + - name: Docker - Set up Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker - Meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.DOCKER_REPO }} + flavor: | + latest=${{ env.TAG_LATEST }} + suffix=${{ env.TAG_SUFFIX }},onlatest=true + tags: | + type=semver,pattern={{version}} + type=raw,enable=${{ env.TAG_RAW }},value=latest + type=sha,enable=${{ env.TAG_SHA }} + + - name: Docker - Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Docker - Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf '${{ env.DOCKER_REPO }}@sha256:%s ' *) + + - name: Docker - Inspect image + run: | + docker buildx imagetools inspect ${{ env.DOCKER_REPO }}:${{ steps.meta.outputs.version }} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..71c2a5f --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,29 @@ +name: Docker + + +on: + push: + branches: + - master + tags: + - 'v*.*.*' + paths-ignore: + - '**/*.md' + - '.gitignore' + - '.dockerignore' + - 'docker/**' + - '!docker/Dockerfile' + - '.github/**' + - '!.github/workflows/docker.yml' + - '!.github/workflows/docker-reusable.yml' + - 'LICENSE-*' + workflow_dispatch: + + +jobs: + build-and-push: + name: Build and Push + uses: ./.github/workflows/docker-reusable.yml + with: + tag_latest: ${{ github.ref_name == github.event.repository.default_branch || startsWith(github.ref, 'refs/tags/') }} + secrets: inherit diff --git a/Dockerfile b/Dockerfile index ad1a46c..ce7e39e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,29 @@ -# Use Node.js LTS version -FROM node:20-slim +# Variables +ARG BUILDER=node:20-alpine +ARG IMAGE=${BUILDER} +ARG APP_USER=root +ARG APP_HOME=/app -# Create app directory -WORKDIR /usr/src/app -# Copy package files -COPY package*.json ./ +# Build +FROM ${BUILDER} AS builder + +ARG APP_USER +ARG APP_HOME + +WORKDIR ${APP_HOME} +COPY --chown=${APP_USER}:${APP_USER} . . -# Install dependencies RUN npm install -# Copy source code -COPY . . -# Start the bot -CMD [ "node", "index.js" ] \ No newline at end of file +# Create +FROM ${IMAGE} + +ARG APP_USER +ARG APP_HOME + +WORKDIR ${APP_HOME} +COPY --chown=${APP_USER}:${APP_USER} --from=builder ${APP_HOME} . + +CMD [ "node", "index.js" ] From 95ac4400ab123f1c070b2e5b865f34af5ab7db2e Mon Sep 17 00:00:00 2001 From: Slava <20563034+veaceslavdoina@users.noreply.github.com> Date: Tue, 17 Dec 2024 20:22:49 +0200 Subject: [PATCH 3/3] chore: docker repository name (#3) --- .github/workflows/docker-reusable.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-reusable.yml b/.github/workflows/docker-reusable.yml index 6047072..7a7e9ef 100644 --- a/.github/workflows/docker-reusable.yml +++ b/.github/workflows/docker-reusable.yml @@ -10,7 +10,7 @@ on: required: false type: string docker_repo: - default: codexstorage/discord-bot + default: codexstorage/testnet-bot description: DockerHub repository required: false type: string