From 5222263484f2a5889653859db6aeb7438f116357 Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Tue, 18 Apr 2023 14:19:22 +0100 Subject: [PATCH] chore(build): improve build process, add release please (#2414) --- .github/workflows/ci.yml | 146 ++++++++++++++++++++++------------ .release-please-manifest.json | 3 + .schmersion.yaml | 14 ---- Dockerfile | 4 +- Makefile | 115 -------------------------- release-please-config.json | 15 ++++ 6 files changed, 117 insertions(+), 180 deletions(-) create mode 100644 .release-please-manifest.json delete mode 100644 .schmersion.yaml delete mode 100644 Makefile create mode 100644 release-please-config.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 556576b..0705ebd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,63 +3,111 @@ name: CI on: [push] jobs: - # lint: - # name: Lint - # runs-on: self-hosted - # steps: - # - uses: actions/checkout@v3 - # - run: make ci-lint + release-please: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + outputs: + release_created: ${{ steps.release-please.outputs.release_created }} + tag_name: ${{ steps.release-please.outputs.tag_name }} # e.g. v1.0.0 + version: ${{ steps.release-please.outputs.version }} # e.g. 1.0.0 + all: ${{ toJSON(steps.release-please.outputs) }} + steps: + - uses: google-github-actions/release-please-action@v3 + id: release-please + with: + command: manifest + + build: + name: CI Image Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: docker/setup-buildx-action@v2 + - uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - uses: docker/build-push-action@v4 + with: + push: true + tags: ghcr.io/postalserver/postal:ci-${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + target: ci test: - name: Test - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v3 - - name: Run tests - run: make ci-test - - build-latest: - runs-on: ubuntu-20.04 - needs: [test] - if: github.ref == 'refs/heads/main' + name: Test Suite + runs-on: ubuntu-latest + needs: build steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Parse git ref - run: | - echo "DOCKER_TAG_NAME=$(basename $GITHUB_REF)" >> $GITHUB_ENV - - - name: Log in to the Container registry - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + - uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - - name: Build and push Docker image - run: make docker-release - - build-release: - runs-on: ubuntu-20.04 - needs: [test] - if: contains(github.ref, 'refs/tags/') - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: Parse git ref - run: | - echo "DOCKER_TAG_NAME=$(basename $GITHUB_REF)" >> $GITHUB_ENV - - - name: Log in to the Container registry - uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Build and push Docker image - run: make docker-release + - run: docker-compose pull env: - DOCKER_TAG_NAME: "latest" + POSTAL_IMAGE: ghcr.io/postalserver/postal:ci-${{ github.sha }} + - run: docker-compose run postal sh -c 'bundle exec rspec' + env: + POSTAL_IMAGE: ghcr.io/postalserver/postal:ci-${{ github.sha }} + + release-branch: + name: Release (branch) + runs-on: ubuntu-latest + needs: [build] + if: startsWith(github.ref, 'refs/heads/') + steps: + - uses: actions/checkout@v3 + - uses: docker/setup-buildx-action@v2 + - uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - id: tag + run: | + TAG="${GITHUB_REF#refs/heads/}" + if [ -z "$TAG" ]; then exit 1; fi + if [[ $TAG == "main" ]]; then TAG="latest"; else TAG="branch-${TAG}"; fi + echo "tag=${TAG}" >> $GITHUB_OUTPUT + - uses: docker/build-push-action@v4 + with: + push: true + tags: | + ghcr.io/postalserver/postal:${{ steps.tag.outputs.tag }} + ghcr.io/postalserver/postal:commit-${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + target: full + + publish-image: + name: Publish Image + runs-on: ubuntu-latest + needs: [build, test, release-please] + if: ${{ needs.release-please.outputs.release_created }} + steps: + - uses: actions/checkout@v3 + - uses: docker/setup-buildx-action@v2 + - uses: docker/login-action@v2 + with: + registry: ${{ vars.KRYSTAL_REGISTRY_HOST }} + username: ${{ vars.KRYSTAL_REGISTRY_USERNAME }} + password: ${{ secrets.KRYSTAL_REGISTRY_PASSWORD }} + - uses: docker/build-push-action@v4 + with: + push: true + tags: | + ghcr.io/postalserver/postal:stable + ghcr.io/postalserver/postal:${{ needs.release-please.outputs.version }} + cache-from: type=local,src=/cache/krystal-identity + cache-to: type=local,dest=/cache/krystal-identity,mode=max + target: full + build-args: | + VERSION=${{ needs.release-please.outputs.version }} diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 0000000..38dc7f3 --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,3 @@ +{ + ".": "2.1.4" +} diff --git a/.schmersion.yaml b/.schmersion.yaml deleted file mode 100644 index 794e7c9..0000000 --- a/.schmersion.yaml +++ /dev/null @@ -1,14 +0,0 @@ -exports: - - name: CHANGELOG.md - formatter: markdown - options: - title: CHANGELOG - description: This file contains all the latest changes and updates to Postal. - sections: - - title: Features - types: [feat] - - title: Bug Fixes - types: [fix] -# -# There are additional options you can define, check out the README for details. -# https://github.com/krystal/schmersion diff --git a/Dockerfile b/Dockerfile index d282997..b1b17c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,8 +56,8 @@ CMD ["postal"] # ci target - use --target=ci to skip asset compilation FROM base AS ci -# prod target - default if no --target option is given -FROM base AS prod +# full target - default if no --target option is given +FROM base AS full RUN POSTAL_SKIP_CONFIG_CHECK=1 RAILS_GROUPS=assets bundle exec rake assets:precompile RUN touch /opt/postal/app/public/assets/.prebuilt diff --git a/Makefile b/Makefile deleted file mode 100644 index 8cf41f7..0000000 --- a/Makefile +++ /dev/null @@ -1,115 +0,0 @@ -# ============================================================================== -# Welcome to the Makefile -# ============================================================================== -# -# This Makefile contains a series of tasks which can help with building, testing -# and working with the app. The following tasks are available to you: -# -# Image building & releasing actions: -# -# make docker-build - Builds a production Docker image. -# make docker-ci-build - Builds an image without any asset compilation, -# ideal for running test suites and similar tasks. -# make docker-image - Builds a production Docker image and tags it as -# appropriate based on the current branch & tag. -# make docker-release - Builds a production Docker image and uploads to the -# registry. -# - -# ============================================================================== -# Configuration -# ============================================================================== - -# Docker image name to release the production image as. -DOCKER_IMAGE := ghcr.io/postalserver/postal - -# Path to bundle config -BUNDLE_CONFIG ?= $(HOME)/.bundle/config - -# Detect if a tty is available -TTY := $(shell [ -t 0 ] && echo 1) - -# Tag names -DOCKER_TAG_NAME ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null) -DOCKER_TAG_VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null) - -ifeq ($(DOCKER_TAG_NAME),master) - DOCKER_TAG_NAME = latest -endif - -ifeq ($(DOCKER_TAG_NAME),main) - DOCKER_TAG_NAME = latest -endif - -# Version string to use -VERSION ?= $(DOCKER_TAG_VERSION) -ifeq ($(VERSION),) - VERSION = $(shell git describe --tags 2>/dev/null) -endif - -ifeq ($(VERSION),) - VERSION = 0.0.0-dev -endif - -# ============================================================================== -# Image Building -# ============================================================================== - -DOCKER_BUILD_CMD = DOCKER_BUILDKIT=1 docker \ - build $(if $(TTY),,--progress plain) \ - --build-arg VERSION=$(VERSION) \ - . - -DOCKER_CI_BUILD_CMD = $(DOCKER_BUILD_CMD) --target=ci - -.PHONY: docker-build -docker-build: - $(DOCKER_BUILD_CMD) - -.PHONY: docker-ci-build -docker-ci-build: - $(DOCKER_CI_BUILD_CMD) - -# ============================================================================== -# Image Tagging -# ============================================================================== - -.PHONY: docker-image -docker-image: docker-build - $(eval IMAGE := $(shell $(DOCKER_BUILD_CMD) -q)) -ifeq ($(DOCKER_TAG_NAME),latest) - docker tag "$(IMAGE)" "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" -endif - -# ============================================================================== -# Image Releasing -# ============================================================================== - -.PHONY: docker-release -docker-release: docker-image -ifeq ($(DOCKER_TAG_NAME),latest) - docker push "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" -endif -ifneq ($(DOCKER_TAG_VERSION),) - docker tag "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" \ - "$(DOCKER_IMAGE):$(DOCKER_TAG_VERSION)" && \ - docker push "$(DOCKER_IMAGE):$(DOCKER_TAG_VERSION)" -endif - -# ============================================================================== -# Tests -# ============================================================================== - -.PHONY: ci-test -ci-test: docker-ci-build - $(eval IMAGE := $(shell $(DOCKER_CI_BUILD_CMD) -q)) - $(eval RAND := $(shell echo "$${RANDOM}$$(date +%s)")) - POSTAL_IMAGE=$(IMAGE) \ - docker-compose -p "postal$(RAND)" run --rm postal sh -c 'bundle exec rspec'; \ - EXIT_CODE=$$?; \ - docker-compose -p "postal$(RAND)" down -v; \ - exit $$EXIT_CODE - -.PHONY: test -test: - bundle exec rspec diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000..e9f45c9 --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,15 @@ +{ + "bootstrap-sha": "76f43140ae57964c871bb654d70d88ecc2210cb1", + "packages": { + ".": { + "release-type": "ruby", + "changelog-path": "CHANGELOG.md", + "bump-minor-pre-major": true, + "bump-patch-for-minor-pre-major": true, + "draft": false, + "prerelease": false, + "include-v-in-tag": false + } + }, + "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json" +}