Skip to content

Commit

Permalink
refactor(CI): improve build and release workflows (#358)
Browse files Browse the repository at this point in the history
* refactor(CI): build and publish workflows

Signed-off-by: qwqcode <[email protected]>

* fix(docs): version in cdn link output issue

Signed-off-by: qwqcode <[email protected]>

Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode authored Jan 26, 2023
1 parent 2eaf3e8 commit 34e59e6
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 327 deletions.
21 changes: 21 additions & 0 deletions .chglog/CHANGELOG.tpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ range .Versions }}
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }})

{{ range .CommitGroups -}}
### {{ .Title }}

{{ range .Commits -}}
* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}

{{- if .NoteGroups -}}
{{ range .NoteGroups -}}
### {{ .Title }}

{{ range .Notes }}
{{ .Body }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}
32 changes: 32 additions & 0 deletions .chglog/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
style: github
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/ArtalkJS/Artalk
options:
tag_filter_pattern: '^v'
sort: "date"
commits:
filters:
Type:
- feat
- fix
- perf
- refactor
- docs
commit_groups:
title_maps:
feat: Features
fix: Bug Fixes
perf: Performance Improvements
refactor: Code Refactoring
docs: Documentation
header:
pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$"
pattern_maps:
- Type
- Scope
- Subject
notes:
keywords:
- BREAKING CHANGE
77 changes: 77 additions & 0 deletions .github/workflows/build-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Build App
run-name: Build Go App and Publish

on:
repository_dispatch:
types: [artalk-release]

env:
GO_VERSION: '1.19.4'
PKG_NAME: 'github.com/ArtalkJS/Artalk'
DOCKER_IMG: ghcr.io/goreleaser/goreleaser-cross
CACHE_DIR: /tmp/cache/docker-image

jobs:
build_publish:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: master

- name: Docker Image Cache
id: docker-cache
uses: actions/cache@v3
with:
path: ${{ env.CACHE_DIR }}
key: docker-image-go-cross-${{ env.GO_VERSION }}

- name: Pull Docker Image
if: steps.docker-cache.outputs.cache-hit != 'true'
run: |
docker pull "${DOCKER_IMG}:v${GO_VERSION}"
mkdir -p "${CACHE_DIR}"
docker save -o "${CACHE_DIR}/go-cross.tar" "${DOCKER_IMG}:v${GO_VERSION}"
- name: Restore Docker Image from Cache
if: steps.docker-cache.outputs.cache-hit == 'true'
run: docker load -i ${CACHE_DIR}/go-cross.tar

- name: Setup git-chglog
run: |
curl -sL $(curl -s https://api.github.com/repos/git-chglog/git-chglog/releases/latest \
| grep -oP '"https://.+linux_amd64.tar.gz"' | tr -d \") | tar -C /usr/local/bin -xz git-chglog
git-chglog --version
- name: Pre Build
run: |-
# get latest version by tag
VERSION=$(git describe --tags --abbrev=0)
echo "VERSION=${VERSION}" >> $GITHUB_ENV
# changelog
mkdir -p local
git-chglog ${VERSION} > local/release-notes.md
# copy config file
cp conf/artalk.example.yml artalk.yml
# github token
echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" > local/.release-env
- name: Build and Release
run: |
docker run \
--rm \
--privileged \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd)/sysroot:/sysroot \
-v $(pwd):/go/src/${PKG_NAME} \
-w /go/src/${PKG_NAME} \
-e CGO_ENABLED=1 \
--env-file local/.release-env \
ghcr.io/goreleaser/goreleaser-cross:v${GO_VERSION} \
release --release-notes local/release-notes.md
116 changes: 116 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Build Docker
run-name: Build Docker Image and Publish

on:
repository_dispatch:
types: [artalk-release]

env:
DOCKER_IMG: artalk/artalk-go

jobs:
build_publish:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Checkout latest release tag
run: |
# fetch tags
git fetch --prune --unshallow --tags -f
# checkout latest version
VERSION=$(git describe --tags --abbrev=0)
git checkout ${VERSION}
VERSION_SHORT="$(cut -d '.' -f 1,2 <<< ${VERSION#v})" # (i.e "v1.2.3" -> "1.2")
COMMIT_HASH="$(git rev-parse --short HEAD)"
COMMIT_HASH_FULL="$(git rev-parse HEAD)"
echo "VERSION=${VERSION}" >> $GITHUB_ENV
echo "VERSION_SHORT=${VERSION_SHORT}" >> $GITHUB_ENV
echo "COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV
echo "COMMIT_HASH_FULL=${COMMIT_HASH_FULL}" >> $GITHUB_ENV
# https://github.com/docker/login-action
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# https://github.com/docker/setup-qemu-action
- name: Setup QEMU
id: qemu
uses: docker/setup-qemu-action@v2
with:
platforms: 'amd64,arm64'

# https://github.com/docker/setup-buildx-action
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
# TODO: https://github.com/docker/metadata-action/pull/248
- name: Get Docker Tags and Labels
run: |
TAGS="${DOCKER_IMG}:latest,${DOCKER_IMG}:${VERSION#v},${DOCKER_IMG}:${VERSION_SHORT},${DOCKER_IMG}:sha-${COMMIT_HASH}"
echo "DOCKER_TAGS=${TAGS}" >> $GITHUB_ENV
# https://github.com/docker/metadata-action
- name: Gen docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ env.DOCKER_IMG }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
# https://github.com/docker/build-push-action
- name: Build and Push
id: docker_build
uses: docker/build-push-action@v3
with:
builder: ${{ steps.buildx.outputs.name }}
context: ./
file: ./Dockerfile
platforms: 'linux/amd64,linux/arm64'
push: true
# tags: ${{ steps.meta.outputs.tags }}
tags: ${{ env.DOCKER_TAGS }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

- name: Comment build info in commit
uses: actions/github-script@v6
env:
STDOUT: "🐳 Published new docker image: [${{ env.DOCKER_IMG }}](https://hub.docker.com/r/${{ env.DOCKER_IMG }}) (${{ env.VERSION }} / sha-${{ env.COMMIT_HASH }})"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.repos.createCommitComment({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: process.env.COMMIT_HASH_FULL, // context.sha
body: process.env.STDOUT
})
- name: Print image digest
run: echo ${{ steps.docker_build.outputs.digest }}
52 changes: 52 additions & 0 deletions .github/workflows/build-frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build Frontend
run-name: Build Frontend and Publish

on:
repository_dispatch:
types: [artalk-release]

jobs:
build_publish:
runs-on: ubuntu-latest

defaults:
run:
working-directory: ./ui # set for building frontend

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Use pnpm
uses: pnpm/action-setup@v2
with:
version: 7

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16.x
registry-url: https://registry.npmjs.org/
cache: 'pnpm'

- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm build

- name: Publish
run: pnpm publish -F artalk --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

# GitHub Packages
#
# Setup .npmrc file to publish to GitHub Packages
# - uses: actions/setup-node@v2
# with:
# registry-url: 'https://npm.pkg.github.com'
# # Publish to GitHub Packages
# - run: npm publish
# env:
# NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84 changes: 0 additions & 84 deletions .github/workflows/docker.yml

This file was deleted.

Loading

0 comments on commit 34e59e6

Please sign in to comment.