From 30f345be557661486844caeeda55d269eb00cf95 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Wed, 23 Dec 2020 14:13:02 +0000 Subject: [PATCH] Added automated build and test infrastructure Based on docker and github actions Builds a docker image for each commit as well as the lua lib mod Builds the master branch of subspace_storage for use with the tests Tests are based on latest stable factorio branch --- .dockerignore | 23 ++++ .github/workflows/docker-publish.yml | 159 +++++++++++++++++++++++++++ .gitignore | 1 + Dockerfile | 75 ++++++++++--- docker-compose.test.yml | 7 ++ docker-compose.yml | 9 ++ 6 files changed, 257 insertions(+), 17 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker-publish.yml create mode 100644 docker-compose.test.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..2ad21c71 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,23 @@ +# No need to invalidate cache just because we try speeding up the build +.dockerignore +Dockerfile +docker-compose* + +# Install dependencies during the build +node_modules +packages/**/node_modules +plugins/**/node_modules + +# Download factorio during the build +factorio +factorio.tar.gz + +# We build internal mods during build, mods from local test env should not be applied +sharedMods +sharedModsDisabled + +# These files should never be required during the build +.git +.github +dist +temp diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 00000000..24ac50b0 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,159 @@ +name: Docker + +on: + push: + # Publish `master` as Docker `latest` image. + branches: + - master + + # Publish `v1.2.3` tags as releases. + tags: + - v* + + # Run tests for any PRs. + pull_request: + + release: + types: [released, prereleased] + +env: + # TODO: Change variable to your image's name. + IMAGE_NAME: clusterio + +jobs: + # Build dependencies and clusterio image + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Build docker image + run: DOCKER_BUILDKIT=1 docker build . -t $IMAGE_NAME --target clusterio_final + + - name: Export docker image + run: docker save -o clusterio_docker_image $IMAGE_NAME + + - name: Upload docker image + uses: actions/upload-artifact@master + with: + name: clusterio_docker_image + path: clusterio_docker_image + # Build dependencies and clusterio image with test tooling + build_testing: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Build docker image + run: DOCKER_BUILDKIT=1 docker build . -t $IMAGE_NAME --target clusterio_testing + + - name: Export docker image + run: docker save -o clusterio_docker_image_testing $IMAGE_NAME + + - name: Upload docker image + uses: actions/upload-artifact@master + with: + name: clusterio_docker_image_testing + path: clusterio_docker_image_testing + # Upload clusterio_lib and subspace_storage versions + upload_mods: + needs: build + runs-on: ubuntu-latest + steps: + - name: Download docker image + uses: actions/download-artifact@master + with: + name: clusterio_docker_image + path: . + + - name: Load docker image + run: docker load -i clusterio_docker_image + + - name: Create container + run: docker create --name clusterio_container $IMAGE_NAME + + - name: Move mods out of container + run: docker cp clusterio_container:/clusterio/sharedMods ./mods + + - name: Remove container + run: docker rm clusterio_container + + - name: List extracted mods + run: ls mods + + - name: Upload mods + uses: actions/upload-artifact@master + with: + name: mods + path: mods + # Run tests. + # See also https://docs.docker.com/docker-hub/builds/automated-testing/ + test: + needs: build_testing + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Download docker image + uses: actions/download-artifact@master + with: + name: clusterio_docker_image_testing + path: . + - name: ls + run: ls && ls clusterio_docker_image_testing && ls /home/runner/work/factorioClusterio && ls /home/runner/work/factorioClusterio/factorioClusterio + - name: Load docker image + run: docker load -i clusterio_docker_image_testing + + - name: Run tests + run: | + if [ -f docker-compose.test.yml ]; then + docker-compose --file docker-compose.test.yml run unit_test + else + docker build . --file Dockerfile + fi + # Push image to GitHub Packages. + # See also https://docs.docker.com/docker-hub/builds/ + push: + # Ensure test job passes before pushing image. + needs: [test, build] + + runs-on: ubuntu-latest + if: github.event_name == 'push' + + steps: + - uses: actions/checkout@v2 + + - name: Download artifact + uses: actions/download-artifact@master + with: + name: clusterio_docker_image + path: . + + - name: Load docker image + run: docker load -i clusterio_docker_image + + - name: Log into registry + run: echo "${{ secrets.CR_PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + + - name: Push image + run: | + IMAGE_ID=ghcr.io/${{ github.actor }}/$IMAGE_NAME + + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + + # Strip "v" prefix from tag name + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + + # Use Docker `latest` tag convention + [ "$VERSION" == "master" ] && VERSION=latest + + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + + docker tag $IMAGE_NAME $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION diff --git a/.gitignore b/.gitignore index 4fc2dc3a..08f2a543 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ dist/ node_modules Factorio* +factorio* /factorio /instances /sharedMods diff --git a/Dockerfile b/Dockerfile index add0fe0f..a181fc7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,64 @@ -# Warning: This is currently broken as a result of -# changes done in the 2.0 refactoring. -FROM node:12 -RUN apt-get update && apt install git curl tar -y -RUN mkdir factorioClusterio +FROM node:12 as subspace_storage_builder +RUN apt update && apt install -y git +WORKDIR / +RUN git clone https://github.com/clusterio/factorioClusterioMod.git +WORKDIR /factorioClusterioMod +RUN git checkout clusterio-2.0 \ +&& npm install \ +&& node build -RUN git clone -b master https://github.com/clusterio/factorioClusterio.git && cd factorioClusterio && npm install --only=production -RUN cd factorioClusterio && curl -o factorio.tar.gz -L https://www.factorio.com/get-download/latest/headless/linux64 && tar -xf factorio.tar.gz +FROM node:12 as clusterio_builder +RUN apt update \ +&& apt install -y wget \ +&& mkdir /clusterio +WORKDIR /clusterio +RUN wget -q -O factorio.tar.gz https://www.factorio.com/get-download/latest/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz +# Copy project files into the container +COPY . . -WORKDIR factorioClusterio -RUN mkdir instances sharedMods -RUN cp config.json.dist config.json +RUN npm install \ +&& npx lerna bootstrap --hoist -RUN node client manage shared mods add clusterio +# Install plugins. This is intended as a reasonable default, enabling plugins to make for fun gameplay. +# If you want a different set of plugins, consider using this as the base image for your own. +#RUN npm install @clusterio/plugin-subspace_storage +#RUN npx clusteriomaster plugin add @clusterio/plugin-subspace_storage -LABEL maintainer "Sir3lit@gmail.com" +COPY --from=subspace_storage_builder /factorioClusterioMod/dist/ /clusterio/sharedMods/ -EXPOSE 8080 34167 -VOLUME /factorioClusterio/instances -VOLUME /factorioClusterio/sharedMods -VOLUME /factorioClusterio/sharedPlugins +# Build submodules (web UI, libraries, plugins etc) +RUN npx lerna run build +# Build Lua library mod +RUN node packages/lib/build_mod --build --source-dir packages/slave/lua/clusterio_lib \ +&& mv dist/* sharedMods/ \ +&& mkdir temp \ +&& mkdir temp/test \ +&& cp sharedMods/ temp/test/ -r \ +&& ls sharedMods -CMD MODE="$MODE" node $MODE start $INSTANCE +# Remove node_modules +RUN find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \; + +FROM frolvlad/alpine-glibc AS clusterio_final + +RUN apk add --update bash nodejs npm + +COPY --from=clusterio_builder /clusterio /clusterio +WORKDIR /clusterio + +# Install runtime dependencies +RUN npm install --production +RUN npx lerna bootstrap -- --production --no-optional +LABEL maintainer "danielv@danielv.no" + +FROM frolvlad/alpine-glibc AS clusterio_testing + +RUN apk add --update bash nodejs npm + +COPY --from=clusterio_builder /clusterio /clusterio +WORKDIR /clusterio + +# Install runtime dependencies +RUN npm install +RUN npx lerna bootstrap +RUN npm install chalk semver diff --git a/docker-compose.test.yml b/docker-compose.test.yml new file mode 100644 index 00000000..89257436 --- /dev/null +++ b/docker-compose.test.yml @@ -0,0 +1,7 @@ +version: '3' + +services: + unit_test: + image: clusterio + build: . + command: npm run test \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..d173c612 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' + +services: + master: + build: . + command: npx clusteriomaster run + slave: + build: . + command: npx clusterioslave run