diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..bf2ff728 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,111 @@ +name: CI +on: + push: + paths: + - .github/workflows/build.yml + - cmd/** + - internal/** + - pkg/** + - .dockerignore + - .golangci.yml + - Dockerfile + - go.mod + - go.sum + release: + types: [published] + 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: 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 + - if: github.event_name == 'push' + name: Build final image + run: docker build . + + publish: + if: github.event_name == 'release' || + github.event_name == 'push' + needs: [verify] + 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 + env: + EVENT_NAME: ${{ github.event_name }} + run: | + BRANCH=${GITHUB_REF#refs/heads/} + 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/heads/} + if [ "$EVENT_NAME" = "push" ] && [ "$BRANCH" != "main" ]; then + echo ::set-output name=version::$BRANCH + echo ::set-output name=platforms::linux/amd64 + elif [ "$EVENT_NAME" = "push" ] && [ "$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::${GITHUB_REF#refs/tags/} + echo ::set-output name=platforms::linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le,linux/riscv64 + 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/gluetun:${{ steps.vars.outputs.version }} + qmcgaw/private-internet-access:${{ steps.vars.outputs.version }} + push: true + + - if: github.event_name == 'push' && github.event.ref == 'refs/heads/main' + name: Microbadger hook + run: curl -X POST https://hooks.microbadger.com/images/qmcgaw/gluetun/l-keGI7p4IhX4QuIDMFYKhsZ1L0= + continue-on-error: true diff --git a/.github/workflows/latest.yml b/.github/workflows/latest.yml deleted file mode 100644 index 70f0db21..00000000 --- a/.github/workflows/latest.yml +++ /dev/null @@ -1,53 +0,0 @@ -name: latest -on: - push: - branches: [master] - paths: - - .github/workflows/latest.yml - - cmd/** - - internal/** - - .dockerignore - - .golangci.yml - - Dockerfile - - go.mod - - go.sum - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - env: - DOCKER_BUILDKIT: "1" - run: docker build --target test . - - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - env: - DOCKER_BUILDKIT: "1" - run: docker build --target lint . - - docker: - runs-on: ubuntu-latest - needs: [test, lint] - steps: - - uses: actions/checkout@v2 - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - name: Dockerhub login - run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u qmcgaw --password-stdin 2>&1 - - name: Docker buildx - run: | - docker buildx build \ - --platform=linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le \ - --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - --build-arg COMMIT=`git rev-parse --short HEAD` \ - --build-arg VERSION=latest \ - -t qmcgaw/private-internet-access:latest \ - -t qmcgaw/gluetun:latest \ - --push \ - . - - run: curl -X POST https://hooks.microbadger.com/images/qmcgaw/gluetun/l-keGI7p4IhX4QuIDMFYKhsZ1L0= - continue-on-error: true diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml deleted file mode 100644 index 29ee5934..00000000 --- a/.github/workflows/pr.yml +++ /dev/null @@ -1,50 +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: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - env: - DOCKER_BUILDKIT: "1" - run: docker build --target test . - - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - env: - DOCKER_BUILDKIT: "1" - run: docker build --target lint . - - docker: - runs-on: ubuntu-latest - needs: [test, lint] - steps: - - uses: actions/checkout@v2 - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - name: Dockerhub login - run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u qmcgaw --password-stdin 2>&1 - - name: Docker build - run: | - docker buildx build \ - --platform=linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le \ - --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - --build-arg COMMIT=`git rev-parse --short HEAD` \ - --build-arg VERSION="branch-${GITHUB_REF##*/}" \ - -t qmcgaw/gluetun:branch-${GITHUB_REF##*/} \ - --push \ - . diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 504a42a4..00000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: release -on: - release: - types: [published] - paths: - - .github/workflows/release.yml - - cmd/** - - internal/** - - .dockerignore - - .golangci.yml - - Dockerfile - - go.mod - - go.sum - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - env: - DOCKER_BUILDKIT: "1" - run: docker build --target test . - - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - env: - DOCKER_BUILDKIT: "1" - run: docker build --target lint . - - docker: - runs-on: ubuntu-latest - needs: [test, lint] - steps: - - uses: actions/checkout@v2 - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - name: Dockerhub login - run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u qmcgaw --password-stdin 2>&1 - - name: Docker buildx - run: | - docker buildx build \ - --platform=linux/amd64,linux/386,linux/arm64,linux/arm/v6,linux/arm/v7,linux/s390x,linux/ppc64le \ - --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ - --build-arg COMMIT=`git rev-parse --short HEAD` \ - --build-arg VERSION=${GITHUB_REF##*/} \ - -t qmcgaw/private-internet-access:${GITHUB_REF##*/} \ - -t qmcgaw/gluetun:${GITHUB_REF##*/} \ - --push \ - . diff --git a/Dockerfile b/Dockerfile index 19f4e774..2d7bec22 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,9 +12,12 @@ COPY cmd/ ./cmd/ COPY internal/ ./internal/ FROM --platform=$BUILDPLATFORM base AS test +# Note on the go race detector: +# - we use golang:1.15-alpine and not golang:1.15-alpine3.12 to have the race detector fixed +# - we set CGO_ENABLED=1 to have it enabled +# - we install g++ to support the race detector ENV CGO_ENABLED=1 -RUN apk --update add g++ -RUN go test -race ./... +RUN apk --update --no-cache add g++ FROM --platform=$BUILDPLATFORM base AS lint ARG GOLANGCI_LINT_VERSION=v1.34.1