Skip to content

Commit

Permalink
refactor(ci): better release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Dec 22, 2023
1 parent 67280f2 commit d86cd73
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 178 deletions.
25 changes: 25 additions & 0 deletions .github/actions/comment/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Comment issue
description: Create comment on a issue

inputs:
issue_number:
required: true
description: The issue number you want to comment
content:
required: true
description: The content of comment

runs:
using: "composite"
steps:
- uses: actions/github-script@v7
env:
STDOUT: ${{ inputs.content }}
with:
script: |
github.rest.issues.createComment({
issue_number: ${{ inputs.issue_number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.STDOUT
})
22 changes: 12 additions & 10 deletions .github/workflows/build-app.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
name: Build App
run-name: Build Go App and Publish (${{ github.event.client_payload.version }}) ${{ github.event.client_payload.dry_run == 'false' && '' || '(🧪 dry-run)' }}

on:
repository_dispatch:
types: [artalk-release]
workflow_call:
inputs:
version:
required: true
type: string
dry_run:
required: true
type: boolean

env:
GO_VERSION: '1.21.3'
PKG_NAME: 'github.com/ArtalkJS/Artalk'
GO_VERSION: 1.21.3
PKG_NAME: github.com/ArtalkJS/Artalk
DOCKER_IMG: ghcr.io/goreleaser/goreleaser-cross
CACHE_DIR: /tmp/cache/docker-image
VERSION: ${{ github.event.client_payload.version }}
DRY_RUN: ${{ github.event.client_payload.dry_run }}

jobs:
build_publish:
Expand All @@ -21,7 +24,6 @@ jobs:
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: ${{ env.VERSION }}
fetch-depth: 0

# disable cache because the err `no space left on device`
Expand Down Expand Up @@ -58,7 +60,7 @@ jobs:
echo "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" > local/.release-env
# changelog
git-chglog --config .github/chglog/config.yml ${VERSION} > local/release-notes.md
git-chglog --config .github/chglog/config.yml ${{ inputs.version }} > local/release-notes.md
# copy config file
cp conf/artalk.example.yml artalk.yml
Expand All @@ -78,4 +80,4 @@ jobs:
-e CGO_ENABLED=1 \
--env-file local/.release-env \
ghcr.io/goreleaser/goreleaser-cross:v${GO_VERSION} \
release --release-notes local/release-notes.md ${{ env.DRY_RUN == 'false' && '' || '--skip=publish' }}
release --release-notes local/release-notes.md ${{ inputs.dry_run && '--skip=publish' || '' }}
89 changes: 33 additions & 56 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
name: Build Docker
run-name: Build Docker Image and Publish (${{ github.event.client_payload.version }}) ${{ github.event.client_payload.dry_run == 'false' && '' || '(🧪 dry-run)' }}

on:
repository_dispatch:
types: [artalk-release]
workflow_call:
inputs:
version:
required: true
type: string
dry_run:
required: true
type: boolean
target:
required: true
type: string

env:
DOCKER_IMG: artalk/artalk-go
VERSION: ${{ github.event.client_payload.version }}
DRY_RUN: ${{ github.event.client_payload.dry_run }}

jobs:
build_publish:
Expand All @@ -17,89 +23,60 @@ jobs:
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: ${{ env.VERSION }}
fetch-depth: 0

- name: Get commit hash
run: |
echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
echo "COMMIT_HASH_FULL=$(git rev-parse HEAD)" >> $GITHUB_ENV
# https://github.com/docker/metadata-action
- name: Gen docker meta
id: meta
uses: docker/metadata-action@v5
with:
context: git
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/login-action
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
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@v3
with:
platforms: 'amd64,arm64'
platforms: ${{ inputs.target }}

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

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
key: ${{ runner.os }}-buildx-${{ inputs.target }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
# https://github.com/docker/metadata-action
- name: Gen docker meta
id: meta
uses: docker/metadata-action@v5
with:
context: git
images: |
${{ env.DOCKER_IMG }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
${{ runner.os }}-buildx-${{ inputs.target }}-
# https://github.com/docker/build-push-action
- name: Build and Push
id: docker_build
id: build
uses: docker/build-push-action@v5
with:
builder: ${{ steps.buildx.outputs.name }}
context: ./
push: ${{ !inputs.dry_run }}
context: .
file: ./Dockerfile
platforms: 'linux/amd64,linux/arm64'
push: ${{ env.DRY_RUN == 'false' }}
platforms: ${{ inputs.target }}
tags: ${{ steps.meta.outputs.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@v7
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: |
const comment = {
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: process.env.COMMIT_HASH_FULL, // context.sha
body: process.env.STDOUT
}
if (process.env.DRY_RUN == 'false')
github.rest.repos.createCommitComment(comment)
else console.log(comment)
- name: Print image digest
run: echo ${{ steps.docker_build.outputs.digest }}
run: echo ${{ steps.build.outputs.digest }}
30 changes: 30 additions & 0 deletions .github/workflows/build-done.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build Done

on:
workflow_call:
inputs:
version:
required: true
type: string
dry_run:
required: true
type: boolean
issue_number:
required: true
type: number

jobs:
tagging:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Comment done info
uses: ./.github/actions/comment
if: ${{ inputs.issue_number != 0 }}
with:
issue_number: ${{ inputs.issue_number }}
content: |
🎉 Finished build and publish version ${{ inputs.version }}.
55 changes: 0 additions & 55 deletions .github/workflows/build-frontend.yml

This file was deleted.

60 changes: 60 additions & 0 deletions .github/workflows/build-tagging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build Pre Tagging

on:
workflow_call:
inputs:
dry_run:
required: true
type: boolean
issue_number:
required: true
type: number
outputs:
version:
description: 'Version'
value: ${{ jobs.tagging.outputs.version }}

jobs:
tagging:
runs-on: ubuntu-latest
steps:
- name: Get release version
run: |
# `github.head_ref` only exists for pull_request events
REF="${{ github.head_ref || github.ref }}"
VERSION="${REF#release/}"
VERSION="${VERSION#refs/tags/}"
echo "Version: $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV
if [[ ! "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Invalid version format: $VERSION, please check git ref or tag."
exit 1
fi
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Create Tag
run: |
if [ ! $(git tag -l "${VERSION}") ]; then
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git tag -a $VERSION -m "Release $VERSION"
git push origin $VERSION
else
echo "Skip creating tag $VERSION, already exists."
fi
- name: Comment tag info to PR
uses: ./.github/actions/comment
if: ${{ inputs.issue_number != 0 }}
with:
issue_number: ${{ inputs.issue_number }}
content: |
🔖 Tagged version [${{ env.VERSION }}](https://github.com/ArtalkJS/Artalk/tree/${{ env.VERSION }}), also see [CHANGELOG.md](https://github.com/ArtalkJS/Artalk/blob/master/CHANGELOG.md).
outputs:
version: ${{ env.VERSION }}
Loading

0 comments on commit d86cd73

Please sign in to comment.