diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000..5c7d66055 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,107 @@ +name: CI +on: + push: + paths: + - .github/workflows/build.yml + - cmd/** + - internal/** + - pkg/** + - .dockerignore + - .golangci.yml + - Dockerfile + - go.mod + - go.sum + pull_request: + paths: + - .github/workflows/build.yml + - cmd/** + - internal/** + - pkg/** + - .dockerignore + - .golangci.yml + - Dockerfile + - go.mod + - go.sum + +jobs: + verify: + runs-on: ubuntu-latest + env: + DOCKER_BUILDKIT: "1" + steps: + - uses: actions/checkout@v2 + + - name: Linting + run: docker build --target lint . + + - name: Go mod tidy check + run: docker build --target tidy . + + - name: Build test image + run: docker build --target test -t test-container . + + - name: Run tests in test container + run: | + touch coverage.txt + docker run --rm \ + -v "$(pwd)/coverage.txt:/tmp/gobuild/coverage.txt" \ + test-container \ + go test \ + -race \ + -coverpkg=./... \ + -coverprofile=coverage.txt \ + -covermode=atomic \ + ./... + + # We run this here to use the caching of the previous steps + - name: Build final image + run: docker build . + + publish: + needs: [verify] + if: github.event_name == 'push' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: docker/setup-qemu-action@v1 + - uses: docker/setup-buildx-action@v1 + + - uses: docker/login-action@v1 + with: + username: qmcgaw + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Set variables + id: vars + run: | + BRANCH=${GITHUB_REF#refs/heads/} + TAG=${GITHUB_REF#refs/tags/} + echo ::set-output name=commit::$(git rev-parse --short HEAD) + echo ::set-output name=build_date::$(date -u +%Y-%m-%dT%H:%M:%SZ) + if [ "$TAG" != "$GITHUB_REF" ]; then + echo ::set-output name=version::$TAG + echo ::set-output name=platforms::linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le,linux/riscv64 + elif [ "$BRANCH" = "main" ]; then + echo ::set-output name=version::latest + echo ::set-output name=platforms::linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le,linux/riscv64 + else + echo ::set-output name=version::$BRANCH + echo ::set-output name=platforms::linux/amd64 + fi + + - name: Build and push final image + uses: docker/build-push-action@v2 + with: + platforms: ${{ steps.vars.outputs.platforms }} + build-args: | + BUILD_DATE=${{ steps.vars.outputs.build_date }} + COMMIT=${{ steps.vars.outputs.commit }} + VERSION=${{ steps.vars.outputs.version }} + tags: qmcgaw/ddns-updater:${{ steps.vars.outputs.version }} + push: true + + - if: github.event.ref == 'refs/heads/main' + name: Microbadger hook + run: curl -X POST https://hooks.microbadger.com/images/qmcgaw/ddns-updater/t2fcZxog8ce_kJYJ61JjkYwHF5s= + continue-on-error: true diff --git a/.github/workflows/latest.yml b/.github/workflows/latest.yml deleted file mode 100644 index 93a068bd7..000000000 --- a/.github/workflows/latest.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: latest -on: - push: - branches: [master] - paths: - - .github/workflows/latest.yml - - cmd/** - - internal/** - - .dockerignore - - .golangci.yml - - Dockerfile - - go.mod - - go.sum - -jobs: - job: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Build base image - run: docker build --target base -t base . - env: - DOCKER_BUILDKIT: "1" - - name: Run tests in container - run: | - docker run --rm -e CGO_ENABLED=1 base \ - go test \ - -race \ - -coverpkg=./... \ - -coverprofile=coverage.txt \ - -covermode=atomic \ - ./... - - name: Linting - run: docker build --target lint . - env: - DOCKER_BUILDKIT: "1" - - name: Go mod tidy check - run: docker build --target tidy . - env: - DOCKER_BUILDKIT: "1" - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - uses: docker/login-action@v1 - with: - username: qmcgaw - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - name: Set variables - id: vars - run: | - echo ::set-output name=commit::$(git rev-parse --short HEAD) - echo ::set-output name=build_date::$(date -u +%Y-%m-%dT%H:%M:%SZ) - echo ::set-output name=version::latest - - name: Build and push image - uses: docker/build-push-action@v2 - with: - platforms: linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le,linux/riscv64 - build-args: | - BUILD_DATE=${{ steps.vars.outputs.build_date }} - COMMIT=${{ steps.vars.outputs.commit }} - VERSION=${{ steps.vars.outputs.version }} - tags: qmcgaw/ddns-updater:${{ steps.vars.outputs.version }} - push: true - - name: Microbadger hook - run: curl -X POST https://hooks.microbadger.com/images/qmcgaw/ddns-updater/t2fcZxog8ce_kJYJ61JjkYwHF5s= - continue-on-error: true diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml deleted file mode 100644 index 51fdef36d..000000000 --- a/.github/workflows/pr.yml +++ /dev/null @@ -1,61 +0,0 @@ -name: pull request -on: - pull_request: - branches: [master] - paths: - - .github/workflows/pr.yml - - cmd/** - - internal/** - - .dockerignore - - .golangci.yml - - Dockerfile - - go.mod - - go.sum -jobs: - job: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Build base image - run: docker build --target base -t base . - env: - DOCKER_BUILDKIT: "1" - - name: Run tests in container - run: | - docker run --rm -e CGO_ENABLED=1 base \ - go test \ - -race \ - -coverpkg=./... \ - -coverprofile=coverage.txt \ - -covermode=atomic \ - ./... - - name: Linting - run: docker build --target lint . - env: - DOCKER_BUILDKIT: "1" - - name: Go mod tidy check - run: docker build --target tidy . - env: - DOCKER_BUILDKIT: "1" - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - uses: docker/login-action@v1 - with: - username: qmcgaw - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - name: Set variables - id: vars - run: | - echo ::set-output name=commit::$(git rev-parse --short HEAD) - echo ::set-output name=build_date::$(date -u +%Y-%m-%dT%H:%M:%SZ) - echo ::set-output name=version::${{ github.event.pull_request.head.ref }} - - name: Build and push image - uses: docker/build-push-action@v2 - with: - platforms: linux/amd64 - build-args: | - BUILD_DATE=${{ steps.vars.outputs.build_date }} - COMMIT=${{ steps.vars.outputs.commit }} - VERSION=${{ steps.vars.outputs.version }} - tags: qmcgaw/ddns-updater:${{ steps.vars.outputs.version }} - push: true \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 4e4fbfdb4..000000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,66 +0,0 @@ -name: release -on: - release: - types: [published] - paths: - - .github/workflows/release.yml - - cmd/** - - internal/** - - .dockerignore - - .golangci.yml - - Dockerfile - - go.mod - - go.sum - -jobs: - job: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Build base image - run: docker build --target base -t base . - env: - DOCKER_BUILDKIT: "1" - - name: Run tests in container - run: | - docker run --rm -e CGO_ENABLED=1 base \ - go test \ - -race \ - -coverpkg=./... \ - -coverprofile=coverage.txt \ - -covermode=atomic \ - ./... - - name: Linting - run: docker build --target lint . - env: - DOCKER_BUILDKIT: "1" - - name: Go mod tidy check - run: docker build --target tidy . - env: - DOCKER_BUILDKIT: "1" - - uses: docker/login-action@v1 - with: - username: qmcgaw - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - uses: docker/login-action@v1 - with: - username: qmcgaw - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - name: Set variables - id: vars - run: | - echo ::set-output name=commit::$(git rev-parse --short HEAD) - echo ::set-output name=build_date::$(date -u +%Y-%m-%dT%H:%M:%SZ) - echo ::set-output name=version::${GITHUB_REF#refs/tags/} - - name: Build and push image - uses: docker/build-push-action@v2 - with: - platforms: linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le,linux/riscv64 - build-args: | - BUILD_DATE=${{ steps.vars.outputs.build_date }} - COMMIT=${{ steps.vars.outputs.commit }} - VERSION=${{ steps.vars.outputs.version }} - tags: qmcgaw/ddns-updater:${{ steps.vars.outputs.version }} - push: true diff --git a/Dockerfile b/Dockerfile index 5ae46d213..55898d11c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,19 +10,23 @@ RUN mkdir /tmp/data && \ FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base ENV CGO_ENABLED=0 -# g++ is installed for the -race detector in go test -RUN apk --update add git g++ +RUN apk --update add git WORKDIR /tmp/gobuild -ARG GOLANGCI_LINT_VERSION=v1.33.0 -RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \ - sh -s -- -b /usr/local/bin ${GOLANGCI_LINT_VERSION} # Copy repository code and install Go dependencies COPY go.mod go.sum ./ RUN go mod download COPY cmd/ ./cmd/ COPY internal/ ./internal/ +FROM --platform=$BUILDPLATFORM base AS test +ENV CGO_ENABLED=1 +# g++ is installed for the -race detector in go test +RUN apk --update add g++ + FROM --platform=$BUILDPLATFORM base AS lint +ARG GOLANGCI_LINT_VERSION=v1.33.0 +RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \ + sh -s -- -b /usr/local/bin ${GOLANGCI_LINT_VERSION} COPY .golangci.yml ./ RUN golangci-lint run --timeout=10m