From b4e4897a37b60d6a9e189b58d4295e9a2d7b6b6a Mon Sep 17 00:00:00 2001 From: Slava <20563034+veaceslavdoina@users.noreply.github.com> Date: Fri, 21 Jul 2023 11:08:29 +0300 Subject: [PATCH] Add Docker builds for Dist-Tests (#63) https://github.com/codex-storage/cs-codex-dist-tests/issues/34 --- .github/workflows/docker-dist-tests.yml | 126 ++++++++++++++++++++++++ .github/workflows/docker.yml | 2 + docker/deploy.Dockerfile | 43 +++++++- 3 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/docker-dist-tests.yml diff --git a/.github/workflows/docker-dist-tests.yml b/.github/workflows/docker-dist-tests.yml new file mode 100644 index 0000000..da1d5b6 --- /dev/null +++ b/.github/workflows/docker-dist-tests.yml @@ -0,0 +1,126 @@ +name: Docker Dist-Tests + + +on: + push: + branches: + - master + tags: + - 'v*.*.*' + paths-ignore: + - '**/*.md' + - '.gitignore' + - 'docker/**' + - '!docker/delpoy.*' + - '.github/**' + - '!.github/workflows/docker-dist-tests.yml' + workflow_dispatch: + + +env: + DOCKER_FILE: docker/deploy.Dockerfile + DOCKER_REPO: codexstorage/dist-tests-codex-contracts-eth + + +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 }} + outputs: + tags-linux-amd64: ${{ steps.tags.outputs.tags-linux-amd64 }} + tags-linux-arm64: ${{ steps.tags.outputs.tags-linux-arm64 }} + env: + PLATFORM: ${{ format('{0}/{1}', 'linux', matrix.target.arch) }} + SUFFIX: ${{ format('{0}-{1}', 'linux', matrix.target.arch) }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Docker - Meta + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.DOCKER_REPO }} + flavor: | + latest=false + tags: | + type=semver,pattern={{version}},suffix=-${{ env.SUFFIX }} + type=sha,suffix=-${{ env.SUFFIX }},enable=${{ !startsWith(github.ref, 'refs/tags/') }} + + - name: Docker - Set tags output + id: tags + run: | + if [[ '${{ matrix.target.os }}' == 'linux' && '${{ matrix.target.arch }}' == 'amd64' ]]; then + echo "tags-linux-amd64=${{ steps.meta.outputs.tags }}" >> "$GITHUB_OUTPUT" + elif [[ '${{ matrix.target.os }}' == 'linux' && '${{ matrix.target.arch }}' == 'arm64' ]]; then + echo "tags-linux-arm64=${{ steps.meta.outputs.tags }}" >> "$GITHUB_OUTPUT" + fi + + - name: Docker - Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Docker - Build and Push + uses: docker/build-push-action@v4 + with: + context: . + file: ${{ env.DOCKER_FILE }} + platforms: ${{ env.PLATFORM }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + # Publish single image + publish: + name: Push single image + runs-on: ubuntu-latest + needs: build + steps: + - name: Docker - Meta + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.DOCKER_REPO }} + tags: | + type=semver,pattern={{version}} + type=sha,enable=${{ !startsWith(github.ref, 'refs/tags/') }} + + - name: Docker - Set tags + run: | + # Transform multi-line tags in to the comma-seperated + TAGS=$(echo "${{ steps.meta.outputs.tags }}" | tr '\n' ',' | awk '{gsub(/,$/,"");}1') + echo "TAGS=${TAGS}" >>$GITHUB_ENV + + - name: Docker - Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Docker - Create and push manifest images + uses: Noelware/docker-manifest-action@master + with: + inputs: ${{ env.TAGS }} + images: ${{ needs.build.outputs.tags-linux-amd64 }},${{ needs.build.outputs.tags-linux-arm64 }} + push: true diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 6cdeb69..f0bb904 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -10,6 +10,8 @@ on: paths-ignore: - '**/*.md' - '.gitignore' + - 'docker/**' + - '!docker/Dockerfile' - '.github/**' - '!.github/workflows/docker.yml' workflow_dispatch: diff --git a/docker/deploy.Dockerfile b/docker/deploy.Dockerfile index e17c90e..06ec4dd 100644 --- a/docker/deploy.Dockerfile +++ b/docker/deploy.Dockerfile @@ -1,5 +1,40 @@ -FROM node:18.16.0-alpine3.17 -WORKDIR /usr/app -COPY . . -RUN ["npm", "install"] +# Variables +ARG BUILDER=node:18.16.0-alpine3.17 +ARG IMAGE=${BUILDER} +ARG APP_USER=root +ARG APP_HOME=/hardhat + + +# Build +FROM ${BUILDER} AS builder + +ARG APP_USER +ARG APP_HOME + +# Install fail on arm64 without additional packages +RUN if [ $(uname -m) = "aarch64" ] ; then \ + if [ $(awk -F '=' '/^ID/ {print $2}' /etc/os-release) = "alpine" ] ; then \ + apk add --update --no-cache python3 python3 make g++ ; \ + else \ + apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/* ; \ + fi \ + fi + +WORKDIR ${APP_HOME} +COPY --chown=${APP_USER}:${APP_USER} . . + +RUN npm install + + +# Create +FROM ${IMAGE} + +ARG APP_USER +ARG APP_HOME + +WORKDIR ${APP_HOME} +COPY --chown=${APP_USER}:${APP_USER} --from=builder ${APP_HOME} . + +EXPOSE 8545 + CMD ["sh", "docker/deploy.sh"]