From 18af3227cc2375d14e3e2ed52ac3c257b46a15f5 Mon Sep 17 00:00:00 2001 From: Adphi Date: Fri, 5 Aug 2022 13:05:37 +0200 Subject: [PATCH] actions: setup tests and releases Signed-off-by: Adphi --- .dockerignore | 3 + .github/workflows/ci.yaml | 245 ++++++++++++++++++++++++++++++++++++++ .gitignore | 3 +- .goreleaser.yaml | 47 ++++++++ Dockerfile | 16 ++- Makefile | 53 ++++++++- 6 files changed, 360 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .goreleaser.yaml diff --git a/.dockerignore b/.dockerignore index ad61352..da46e3e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,3 +3,6 @@ tests disk* qemu.sh **/*.qcow2 +bin +dist +images diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..d9f34da --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,245 @@ +name: Tests and Build + +on: + push: + branches: [ "*" ] + tags: [ "v*" ] + pull_request: + branches: [ main ] +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + # fetching all tags is required for the Makefile to compute the right version + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18 + + - name: Set up QEMU dependency + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Setup dependencies + run: sudo apt update && sudo apt install -y util-linux udev parted e2fsprogs mount tar extlinux qemu-utils + + - name: Share cache with other actions + uses: actions/cache@v2 + with: + path: | + ~/go/pkg/mod + /tmp/.buildx-cache + key: ${{ runner.os }}-tests-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-tests- + + - name: Run tests + run: make tests + + build: + name: Build + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + # fetching all tags is required for the Makefile to compute the right version + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18 + + - name: Set up QEMU dependency + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to Docker Hub + uses: docker/login-action@v1 + if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main' + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Share cache with other actions + uses: actions/cache@v2 + with: + path: | + ~/go/pkg/mod + /tmp/.buildx-cache + key: ${{ runner.os }}-build-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-build- + + - name: Ensure all files were well formatted + run: make check-fmt + + - name: Import GPG key + id: import_gpg + uses: crazy-max/ghaction-import-gpg@v4 + with: + gpg_private_key: ${{ secrets.GPG_KEY }} + passphrase: ${{ secrets.GPG_PASSWORD }} + + - name: Build Snapshot + run: make build-snapshot + + - name: Release Snapshot + env: + GITHUB_TOKEN: ${{ secrets.REPOSITORIES_ACCESS_TOKEN }} + GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }} + GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} + run: make release-snapshot + + build-image: + name: Build Docker Image + runs-on: ubuntu-18.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + # fetching all tags is required for the Makefile to compute the right version + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18 + + - name: Set up QEMU dependency + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to Docker Hub + uses: docker/login-action@v1 + if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main' + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Share cache with other actions + uses: actions/cache@v2 + with: + path: | + ~/go/pkg/mod + /tmp/.buildx-cache + key: ${{ runner.os }}-build-image-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-build-image- + + - name: Build Docker images + run: make docker-build + + - name: Push Docker images + if: github.ref == 'refs/heads/main' + run: make docker-push + + release: + name: Release Binaries + runs-on: ubuntu-18.04 + if: startsWith(github.event.ref, 'refs/tags/v') + needs: + - tests + - build + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + # fetching all tags is required for the Makefile to compute the right version + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18 + + - name: Share cache with other actions + uses: actions/cache@v2 + with: + path: | + ~/go/pkg/mod + /tmp/.buildx-cache + key: ${{ runner.os }}-build-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-build- + + - name: Import GPG key + id: import_gpg + uses: crazy-max/ghaction-import-gpg@v4 + with: + gpg_private_key: ${{ secrets.GPG_KEY }} + passphrase: ${{ secrets.GPG_PASSWORD }} + + - name: Build binaries + run: make build + + - name: Release binaries + env: + GITHUB_TOKEN: ${{ secrets.REPOSITORIES_ACCESS_TOKEN }} + GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }} + GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} + run: make release + + release-image: + name: Release Docker Image + runs-on: ubuntu-18.04 + if: startsWith(github.event.ref, 'refs/tags/v') + needs: + - tests + - build-image + + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + # fetching all tags is required for the Makefile to compute the right version + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.18 + + - name: Set up QEMU dependency + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Share cache with other actions + uses: actions/cache@v2 + with: + path: | + ~/go/pkg/mod + /tmp/.buildx-cache + key: ${{ runner.os }}-build-image-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-build-image- + + - name: Build Docker images + run: make docker-build + + - name: Release Docker images + run: make docker-push diff --git a/.gitignore b/.gitignore index ba84458..2dc576d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ scratch *.vmdk *.vdi +bin/ dist/ -/d2vm -.goreleaser.yaml images diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..e003b64 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,47 @@ +# Copyright 2022 Linka Cloud All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +project_name: d2vm +before: + hooks: + - go mod tidy +builds: +- main: ./cmd/d2vm + env: + - CGO_ENABLED=0 + ldflags: + - -s -w -X "go.linka.cloud/d2vm.Image={{.Env.IMAGE}}" -X "go.linka.cloud/d2vm.Version={{.Env.VERSION}}" -X "go.linka.cloud/d2vm.BuildDate={{.CommitDate}}" + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 +checksum: + name_template: 'checksums.txt' +signs: +- artifacts: all + stdin: '{{ .Env.GPG_PASSWORD }}' +snapshot: + name_template: "{{ .Env.VERSION }}" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + - '^actions:' + - '^Makefile:' + - '^chore:' + - '^goreleaser:' diff --git a/Dockerfile b/Dockerfile index 421f887..2f8f39c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,17 @@ +# Copyright 2022 Linka Cloud All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + FROM golang as builder WORKDIR /d2vm @@ -9,7 +23,7 @@ RUN go mod download COPY . . -RUN make build +RUN make .build FROM ubuntu diff --git a/Makefile b/Makefile index 377e0e8..3387b0c 100644 --- a/Makefile +++ b/Makefile @@ -16,21 +16,40 @@ MODULE = go.linka.cloud/d2vm REPOSITORY = linkacloud +TAG = $(shell git describe --tags --exact-match 2> /dev/null) VERSION_SUFFIX = $(shell git diff --quiet || echo "-dev") VERSION = $(shell git describe --tags --exact-match 2> /dev/null || echo "`git describe --tags $$(git rev-list --tags --max-count=1) 2> /dev/null || echo v0.0.0`-`git rev-parse --short HEAD`")$(VERSION_SUFFIX) show-version: @echo $(VERSION) +GORELEASER_VERSION := v1.10.1 +GORELEASER_URL := https://github.com/goreleaser/goreleaser/releases/download/$(GORELEASER_VERSION)/goreleaser_Linux_x86_64.tar.gz + +BIN := $(PWD)/bin +export PATH := $(BIN):$(PATH) + +bin: + @mkdir -p $(BIN) + @curl -sL $(GORELEASER_URL) | tar -C $(BIN) -xz goreleaser + +clean-bin: + @rm -rf $(BIN) + DOCKER_IMAGE := linkacloud/d2vm docker: docker-build docker-push docker-push: - @docker image push -a $(DOCKER_IMAGE) + @docker image push $(DOCKER_IMAGE) +ifneq ($(TAG),) + @docker image push $(DOCKER_IMAGE):latest +endif docker-build: @docker image build -t $(DOCKER_IMAGE):$(VERSION) . - @echo $(VERSION)|grep -q '-' || docker image tag $(DOCKER_IMAGE):latest $(DOCKER_IMAGE):$(VERSION) +ifneq ($(TAG),) + @docker image tag $(DOCKER_IMAGE):$(TAG) $(DOCKER_IMAGE):latest +endif docker-run: @docker run --rm -i -t \ @@ -40,5 +59,31 @@ docker-run: -w /build \ $(DOCKER_IMAGE) bash -build: - @go build -o d2vm -ldflags "-s -w -X '$(MODULE).Image=$(DOCKER_IMAGE)' -X '$(MODULE).Version=$(VERSION)' -X '$(MODULE).BuildDate=$(shell date)'" ./cmd/d2vm +.PHONY: tests +tests: + @go test -exec sudo -count=1 -v ./... + +check-fmt: + @[ "$(gofmt -l $(find . -name '*.go') 2>&1)" = "" ] + +vet: + @go list ./...|grep -v scratch|GOOS=linux xargs go vet + +.build: + @go build -o d2vm -ldflags "-s -w -X '$(MODULE).Version=$(VERSION)' -X '$(MODULE).BuildDate=$(shell date)'" ./cmd/d2vm + +.PHONY: build-snapshot +build-snapshot: bin + @VERSION=$(VERSION) IMAGE=$(DOCKER_IMAGE) goreleaser build --snapshot --rm-dist --parallelism 8 + +.PHONY: release-snapshot +release-snapshot: bin + @VERSION=$(VERSION) IMAGE=$(DOCKER_IMAGE) goreleaser release --snapshot --rm-dist --skip-announce --skip-publish --parallelism 8 + +.PHONY: build +build: $(BIN) bin + @VERSION=$(VERSION) goreleaser build --rm-dist --parallelism 8 + +.PHONY: release +release: $(BIN) bin + @VERSION=$(VERSION) IMAGE=$(DOCKER_IMAGE) goreleaser release --rm-dist --parallelism 8