diff --git a/.dockerignore b/.dockerignore index e1087be9..6a02c187 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,8 +1,10 @@ +/.dev /.idea /*.iml -.dev +/.github /bin /dist /site /coverage.txt +/test diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1d3da7e2..04d07294 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -39,6 +39,10 @@ jobs: uses: docker/bake-action@v1 with: targets: validate + pull: true + set: | + *.cache-from=type=gha,scope=diun-validate + *.cache-to=type=gha,scope=diun-validate,mode=max test: runs-on: ubuntu-latest @@ -56,6 +60,10 @@ jobs: uses: docker/bake-action@v1 with: targets: test + pull: true + set: | + *.cache-from=type=gha,scope=diun-test + *.cache-to=type=gha,scope=diun-test,mode=max - name: Upload coverage uses: codecov/codecov-action@v2 @@ -117,6 +125,10 @@ jobs: uses: docker/bake-action@v1 with: targets: artifact-all + pull: true + set: | + *.cache-from=type=gha,scope=diun-artifact + *.cache-to=type=gha,scope=diun-artifact,mode=max - name: Move artifacts run: | @@ -136,7 +148,12 @@ jobs: ./docker-bake.hcl ${{ steps.meta.outputs.bake-file }} targets: image-all + pull: true push: ${{ github.event_name != 'pull_request' }} + set: | + *.cache-from=type=gha,scope=diun-artifact + *.cache-from=type=gha,scope=diun-image + *.cache-to=type=gha,scope=diun-image,mode=max - name: GitHub Release uses: softprops/action-gh-release@v1 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 74b8d4b1..57fc28cf 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -36,6 +36,7 @@ jobs: uses: docker/bake-action@v1 with: targets: docs + pull: true - name: Check GitHub Pages status uses: crazy-max/ghaction-github-status@v2 diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 00000000..8c736816 --- /dev/null +++ b/.github/workflows/e2e.yml @@ -0,0 +1,107 @@ +name: e2e + +on: + push: + branches: + - 'master' + - 'v*' + paths-ignore: + - '**.md' + pull_request: + branches: + - 'master' + - 'v*' + paths-ignore: + - '**.md' + +env: + BUILD_TAG: 'crazymax/diun:local' + CONTAINER_NAME: 'diun' + RUNNING_TIMEOUT: '240' + RUNNING_LOG_CHECK: 'Next run in' + +jobs: + e2e: + runs-on: ubuntu-latest + services: + redis: + image: redis:6.2.3-alpine + cloudflared: + image: crazymax/cloudflared:latest + options: >- + --label "diun.enable=true" + --label "diun.watch_repo=true" + strategy: + fail-fast: false + matrix: + include: + - folder: docker1 + loglevel: debug + - folder: docker2 + loglevel: info + - folder: docker3 + loglevel: info + - folder: dockerfile1 + loglevel: debug + steps: + - + name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Build + uses: docker/bake-action@v1 + with: + targets: image-local + pull: true + set: | + *.cache-from=type=gha,scope=diun-image + *.cache-from=type=gha,scope=diun-e2e-${{ matrix.folder }} + *.cache-to=type=gha,scope=diun-e2e-${{ matrix.folder }},mode=max + - + name: Start container + run: | + volFlags="-v $(pwd)/test/${{ matrix.folder }}/diun.yml:/diun.yml:ro" + volFlags="-v /var/run/docker.sock:/var/run/docker.sock ${volFlags}" + if [ -d "$(pwd)/test/${{ matrix.folder }}/mount" ]; then + volFlags="-v $(pwd)/test/${{ matrix.folder }}/mount:/mount ${volFlags}" + fi + docker run -d --name ${{ env.CONTAINER_NAME }} ${volFlags} \ + -e "TZ=Europe/Paris" \ + -e "LOG_LEVEL=${{ matrix.loglevel }}" \ + ${{ env.BUILD_TAG }} + - + name: Test run + run: | + TIMEOUT=$((SECONDS + ${{ env.RUNNING_TIMEOUT }})) + while read LOGLINE; do + echo ${LOGLINE} + if [[ ${LOGLINE} == *"${{ env.RUNNING_LOG_CHECK }}"* ]]; then + echo "🎉 Container up!" + break + fi + if [[ $SECONDS -gt ${TIMEOUT} ]]; then + >&2 echo "❌ Failed to run ${{ env.CONTAINER_NAME }} container" + exit 1 + fi + done < <(docker logs -f ${{ env.CONTAINER_NAME }} 2>&1) + + CONTAINER_STATUS=$(docker container inspect --format "{{.State.Status}}" ${{ env.CONTAINER_NAME }}) + if [[ ${CONTAINER_STATUS} != "running" ]]; then + >&2 echo "❌ Container ${{ env.CONTAINER_NAME }} returned status '$CONTAINER_STATUS'" + exit 1 + fi + - + name: List images in db + run: | + docker exec -t ${{ env.CONTAINER_NAME }} diun image list + - + name: Container logs + if: always() + run: | + docker logs ${{ env.CONTAINER_NAME }} + docker rm -f ${{ env.CONTAINER_NAME }} > /dev/null 2>&1 || true diff --git a/hack/gen.Dockerfile b/hack/gen.Dockerfile index 44b4a9f1..a544e1f1 100644 --- a/hack/gen.Dockerfile +++ b/hack/gen.Dockerfile @@ -33,8 +33,8 @@ COPY --from=generate /out / FROM generate AS validate RUN --mount=type=bind,target=.,rw \ git add -A && cp -rf /out/* .; \ - if [ -n "$(git status --porcelain)" ]; then \ + if [ -n "$(git status --porcelain -- pb)" ]; then \ echo >&2 'ERROR: Generate result differs. Please update with "docker buildx bake gen-update"'; \ - git status --porcelain; \ + git status --porcelain -- pb; \ exit 1; \ fi diff --git a/test/docker1/diun.yml b/test/docker1/diun.yml new file mode 100644 index 00000000..316c14b0 --- /dev/null +++ b/test/docker1/diun.yml @@ -0,0 +1,6 @@ +watch: + workers: 20 + schedule: "0 */6 * * *" + +providers: + docker: {} diff --git a/test/docker2/diun.yml b/test/docker2/diun.yml new file mode 100644 index 00000000..85e51152 --- /dev/null +++ b/test/docker2/diun.yml @@ -0,0 +1,7 @@ +watch: + workers: 20 + schedule: "0 */6 * * *" + +providers: + docker: + watchByDefault: true diff --git a/test/docker3/diun.yml b/test/docker3/diun.yml new file mode 100644 index 00000000..d7186e28 --- /dev/null +++ b/test/docker3/diun.yml @@ -0,0 +1,8 @@ +watch: + workers: 20 + schedule: "0 */6 * * *" + +providers: + docker: + watchByDefault: true + watchStopped: true diff --git a/test/dockerfile1/diun.yml b/test/dockerfile1/diun.yml new file mode 100644 index 00000000..22880a8a --- /dev/null +++ b/test/dockerfile1/diun.yml @@ -0,0 +1,15 @@ +watch: + workers: 20 + schedule: "0 */6 * * *" + firstCheckNotif: true + +notif: + script: + cmd: "sh" + args: + - "/mount/notif.sh" + +providers: + dockerfile: + patterns: + - /mount/Dockerfile* diff --git a/test/dockerfile1/mount/Dockerfile b/test/dockerfile1/mount/Dockerfile new file mode 100644 index 00000000..4967ad62 --- /dev/null +++ b/test/dockerfile1/mount/Dockerfile @@ -0,0 +1,16 @@ +# syntax=docker/dockerfile:1.2 + +# diun.platform=linux/amd64 +FROM alpine:latest + +# diun.watch_repo=true +# diun.max_tags=10 +# diun.platform=linux/amd64 +COPY --from=crazymax/yasu / / + +# diun.watch_repo=true +# diun.include_tags=^\d+\.\d+\.\d+$ +# diun.platform=linux/amd64 +RUN --mount=type=bind,target=.,rw \ + --mount=type=bind,from=docker/buildx-bin:0.6.0,source=/buildx,target=/usr/bin/buildx \ + yasu --version diff --git a/test/dockerfile1/mount/Dockerfile1 b/test/dockerfile1/mount/Dockerfile1 new file mode 100644 index 00000000..6101ba6a --- /dev/null +++ b/test/dockerfile1/mount/Dockerfile1 @@ -0,0 +1,49 @@ +# syntax=docker/dockerfile:1.2 +ARG GO_VERSION=1.16 + +# diun.watch_repo=true +# diun.max_tags=100 +FROM --platform=$BUILDPLATFORM crazymax/goreleaser-xx:latest AS goreleaser-xx +# diun:"watch_repo=true,max_tags=100" +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base +COPY --from=goreleaser-xx / / +COPY --from=crazymax/goreleaser-xx:0.162.0 / / +RUN apk add --no-cache ca-certificates gcc file git linux-headers musl-dev tar +WORKDIR /src + +FROM base AS build +ARG TARGETPLATFORM +ARG GIT_REF +RUN --mount=type=bind,target=/src,rw \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=target=/go/pkg/mod,type=cache \ + goreleaser-xx --debug \ + --name "diun" \ + --dist "/out" \ + --hooks="go mod tidy" \ + --hooks="go mod download" \ + --main="./cmd/main.go" \ + --ldflags="-s -w -X 'main.version={{.Version}}'" \ + --files="CHANGELOG.md" \ + --files="LICENSE" \ + --files="README.md" + +FROM scratch AS artifacts +COPY --from=build /out/*.tar.gz / +COPY --from=build /out/*.zip / + +FROM alpine +LABEL maintainer="CrazyMax" + +RUN apk --update --no-cache add \ + ca-certificates \ + libressl \ + && rm -rf /tmp/* /var/cache/apk/* + +COPY --from=build /usr/local/bin/diun /usr/local/bin/diun +RUN diun --version + +ENV DIUN_DB_PATH="/data/diun.db" + +VOLUME [ "/data" ] +ENTRYPOINT [ "diun" ] diff --git a/test/dockerfile1/mount/Dockerfile2 b/test/dockerfile1/mount/Dockerfile2 new file mode 100644 index 00000000..470f503c --- /dev/null +++ b/test/dockerfile1/mount/Dockerfile2 @@ -0,0 +1,25 @@ +# syntax=docker/dockerfile:1.2 +ARG NODE_VERSION + +# foo +# diun:"watch_repo=true,max_tags=100" +FROM node:${NODE_VERSION}-alpine AS base +RUN apk add --no-cache git +WORKDIR /src + +FROM base AS deps +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/src/node_modules \ + yarn install + +FROM deps AS test +ENV RUNNER_TEMP=/tmp/github_runner +ENV RUNNER_TOOL_CACHE=/tmp/github_tool_cache +RUN --mount=type=bind,target=.,rw \ + --mount=type=cache,target=/src/node_modules \ + --mount=type=bind,from=crazymax/docker,source=/usr/libexec/docker/cli-plugins/docker-buildx,target=/usr/libexec/docker/cli-plugins/docker-buildx \ + --mount=type=bind,from=crazymax/docker,source=/usr/local/bin/docker,target=/usr/bin/docker \ + yarn run test --coverageDirectory=/tmp/coverage + +FROM scratch AS test-coverage +COPY --from=test /tmp/coverage / diff --git a/test/dockerfile1/mount/notif.sh b/test/dockerfile1/mount/notif.sh new file mode 100644 index 00000000..f1ea27b1 --- /dev/null +++ b/test/dockerfile1/mount/notif.sh @@ -0,0 +1 @@ +env|sort