From 173bc382f948144bb2832f95f662cf34948c20d8 Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Sun, 17 Sep 2023 16:48:19 -0700 Subject: [PATCH] redux GitHub actions and add Docker builds --- .github/workflows/go.yml | 79 -------------------- .github/workflows/on-push-pr.yml | 123 +++++++++++++++++++++++++++++++ .github/workflows/on-release.yml | 20 +++++ Dockerfile | 12 ++- Dockerfile.buildx | 28 +++++++ Makefile | 11 ++- 6 files changed, 186 insertions(+), 87 deletions(-) delete mode 100644 .github/workflows/go.yml create mode 100644 .github/workflows/on-push-pr.yml create mode 100644 .github/workflows/on-release.yml create mode 100644 Dockerfile.buildx diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index 1614e8a..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,79 +0,0 @@ -name: Go - -on: - push: - branches: [ main ] - pull_request: - types: [opened, reopened, synchronize] - -jobs: - build-test: - name: Build, test, and format - strategy: - matrix: - go-version: [1.17.x] - platform: [ubuntu-latest, macos-latest, windows-latest] - runs-on: ${{ matrix.platform }} - steps: - - uses: actions/checkout@v3 - - - name: setup go - uses: actions/setup-go@v4 - with: - go-version: ${{ matrix.go-version }} - - - name: Build - run: go build -v ./... - - - name: Test - run: go test -v -race ./... - - - name: Format - if: matrix.platform == 'ubuntu-latest' - run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi - - mysql-integration-test: - name: Integration tests for the MySQL backend. - runs-on: 'ubuntu-latest' - needs: build-test - services: - mysql: - image: mysql:8.0 - env: - MYSQL_RANDOM_ROOT_PASSWORD: yes - MYSQL_DATABASE: nanodep - MYSQL_USER: nanodep - MYSQL_PASSWORD: nanodep - ports: - - 3800:3306 - options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3 - defaults: - run: - shell: bash - env: - MYSQL_PWD: nanodep - PORT: 3800 - steps: - - uses: actions/checkout@v3 - - - name: setup go - uses: actions/setup-go@v4 - with: - go-version: '1.17.x' - - - name: Verify MySQL connection - run: | - while ! mysqladmin ping --host=localhost --port=$PORT --protocol=TCP --silent; do - sleep 1 - done - - - name: Set up schema - run: | - mysql --version - mysql --user=nanodep --host=localhost --port=$PORT --protocol=TCP nanodep < ./storage/mysql/schema.sql - - - name: Set - run: echo "NANODEP_MYSQL_STORAGE_TEST_DSN=nanodep:nanodep@tcp(localhost:$PORT)/nanodep" >> $GITHUB_ENV - - - name: Test - run: go test -v ./storage/mysql diff --git a/.github/workflows/on-push-pr.yml b/.github/workflows/on-push-pr.yml new file mode 100644 index 0000000..04269d6 --- /dev/null +++ b/.github/workflows/on-push-pr.yml @@ -0,0 +1,123 @@ +on: + push: + branches: [main] + tags: ["v*.*.*"] + pull_request: + types: [opened, reopened, synchronize] +jobs: + format-build-test: + strategy: + matrix: + go-version: ['1.19.x'] + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + steps: + - uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0 + + - uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 + with: + go-version: ${{ matrix.go-version }} + + - if: matrix.platform == 'ubuntu-latest' + run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi + + - run: go build -v ./... + + - run: make test + docker-build-push: + if: github.event_name != 'pull_request' + needs: mysql-test + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0 + + - uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2.2.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - uses: docker/setup-buildx-action@4c0219f9ac95b02789c1075625400b2acbff50b1 # v2.9.1 + + - uses: docker/metadata-action@818d4b7b91585d195f67373fd9cb0332e31a7175 # v4.6.0 + id: meta + with: + images: | + ghcr.io/${{ github.repository }} + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + + - uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825 # v4.1.1 + with: + context: . + push: true + file: Dockerfile.buildx + platforms: linux/amd64,linux/arm64,linux/arm + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + release-zips: + if: github.event_name != 'pull_request' + needs: mysql-test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0 + + - uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 + with: + go-version: '1.19' + + - run: CGO_ENABLED=0 make release + + - uses: actions/upload-artifact@v3 + with: + name: release-zips + path: "*.zip" + mysql-test: + runs-on: 'ubuntu-latest' + needs: format-build-test + services: + mysql: + image: mysql:8.0 + env: + MYSQL_RANDOM_ROOT_PASSWORD: yes + MYSQL_DATABASE: nanodep + MYSQL_USER: nanodep + MYSQL_PASSWORD: nanodep + ports: + - 3800:3306 + options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3 + defaults: + run: + shell: bash + env: + MYSQL_PWD: nanodep + PORT: 3800 + steps: + - uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0 + + - uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 + with: + go-version: '1.19.x' + + - name: verify mysql + run: | + while ! mysqladmin ping --host=localhost --port=$PORT --protocol=TCP --silent; do + sleep 1 + done + + - name: mysql schema + run: | + mysql --version + mysql --user=nanodep --host=localhost --port=$PORT --protocol=TCP nanodep < ./storage/mysql/schema.sql + + - name: setup test dsn + run: echo "NANODEP_MYSQL_STORAGE_TEST_DSN=nanodep:nanodep@tcp(localhost:$PORT)/nanodep" >> $GITHUB_ENV + + - run: go test -v ./storage/mysql diff --git a/.github/workflows/on-release.yml b/.github/workflows/on-release.yml new file mode 100644 index 0000000..cb7b141 --- /dev/null +++ b/.github/workflows/on-release.yml @@ -0,0 +1,20 @@ +on: + release: + types: [published] +jobs: + release-zips: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0 + + - uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 + with: + go-version: '1.19' + + - run: CGO_ENABLED=0 make release + + - run: gh release upload ${{ github.event.release.tag_name }} *.zip + env: + GH_TOKEN: ${{ github.token }} diff --git a/Dockerfile b/Dockerfile index 0519e97..c979bb8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,14 @@ FROM gcr.io/distroless/static -COPY depserver-linux-amd64 /depserver -COPY depsyncer-linux-amd64 /depsyncer +ARG TARGETOS TARGETARCH + +COPY depserver-$TARGETOS-$TARGETARCH /app/depserver +COPY depsyncer-$TARGETOS-$TARGETARCH /app/depsyncer EXPOSE 9001 -VOLUME ["/db"] +VOLUME ["/app/db"] + +WORKDIR /app -ENTRYPOINT ["/depserver"] +ENTRYPOINT ["/app/depserver"] diff --git a/Dockerfile.buildx b/Dockerfile.buildx new file mode 100644 index 0000000..ec31539 --- /dev/null +++ b/Dockerfile.buildx @@ -0,0 +1,28 @@ +FROM --platform=$BUILDPLATFORM golang:1.19 AS builder + +WORKDIR /go/app + +COPY . . + +ARG TARGETOS TARGETARCH + +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + CGO_ENABLED=0 make \ + depserver-$TARGETOS-$TARGETARCH \ + depsyncer-$TARGETOS-$TARGETARCH + +FROM gcr.io/distroless/static + +ARG TARGETOS TARGETARCH + +COPY --from=builder /go/app/depserver-$TARGETOS-$TARGETARCH /app/depserver +COPY --from=builder /go/app/depsyncer-$TARGETOS-$TARGETARCH /app/depsyncer + +EXPOSE 9001 + +VOLUME ["/app/db"] + +WORKDIR /app + +ENTRYPOINT ["/app/depserver"] diff --git a/Makefile b/Makefile index d18dfa0..cd2ef48 100644 --- a/Makefile +++ b/Makefile @@ -6,18 +6,24 @@ DEPTOKENS=\ deptokens-darwin-arm64 \ deptokens-darwin-amd64 \ deptokens-linux-amd64 \ + deptokens-linux-arm64 \ + deptokens-linux-arm \ deptokens-windows-amd64.exe DEPSERVER=\ depserver-darwin-arm64 \ depserver-darwin-amd64 \ depserver-linux-amd64 \ + depserver-linux-arm64 \ + depserver-linux-arm \ depserver-windows-amd64.exe DEPSYNCER=\ depsyncer-darwin-arm64 \ depsyncer-darwin-amd64 \ depsyncer-linux-amd64 \ + depsyncer-linux-arm64 \ + depsyncer-linux-arm \ depsyncer-windows-amd64.exe SUPPLEMENTAL=\ @@ -54,10 +60,7 @@ nanodep-%-$(VERSION).zip: depserver-%.exe depsyncer-%.exe deptokens-%.exe $(SUPP clean: rm -f deptokens-* depserver-* depsyncer-* nanodep-*.zip -release: \ - nanodep-darwin-amd64-$(VERSION).zip \ - nanodep-darwin-arm64-$(VERSION).zip \ - nanodep-linux-amd64-$(VERSION).zip +release: $(foreach bin,$(DEPSERVER),$(subst .exe,,$(subst depserver,nanodep,$(bin)))-$(VERSION).zip) test: go test -v -cover -race ./...