Skip to content

Commit

Permalink
feat(ci): add dry-run mode for build workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Dec 21, 2023
1 parent a8bcd41 commit bfabe18
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 60 deletions.
24 changes: 10 additions & 14 deletions .github/workflows/build-app.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Build App
run-name: Build Go App and Publish
run-name: Build Go App and Publish (${{ github.event.client_payload.version }})

on:
repository_dispatch:
Expand All @@ -10,25 +10,18 @@ env:
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:
runs-on: ubuntu-latest

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

- name: Checkout latest tag
run: |
# fetch tags
git fetch --prune --unshallow --tags -f
# checkout latest version
VERSION=$(git describe --tags --abbrev=0)
git checkout ${VERSION}
echo "VERSION=${VERSION}" >> $GITHUB_ENV
uses: actions/checkout@v4
with:
ref: ${{ env.VERSION }}

# disable cache because the err `no space left on device`
# - name: Docker Image Cache
Expand Down Expand Up @@ -69,6 +62,9 @@ jobs:
# copy config file
cp conf/artalk.example.yml artalk.yml
- name: Print Relase Notes
run: cat local/release-notes.md

- name: Build and Release
run: |
docker run \
Expand All @@ -81,4 +77,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
release --release-notes local/release-notes.md ${{ env.DRY_RUN == 'false' && '' || '--skip-publish' }}
46 changes: 21 additions & 25 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
name: Build Docker
run-name: Build Docker Image and Publish
run-name: Build Docker Image and Publish (${{ github.event.client_payload.version }})

on:
repository_dispatch:
types: [artalk-release]

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

jobs:
build_publish:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ env.VERSION }}

- name: Checkout latest tag
- name: Get commit hash
run: |
# fetch tags
git fetch --prune --unshallow --tags -f
# checkout latest version
VERSION=$(git describe --tags --abbrev=0)
git checkout ${VERSION}
COMMIT_HASH="$(git rev-parse --short HEAD)"
COMMIT_HASH_FULL="$(git rev-parse HEAD)"
echo "VERSION=${VERSION}" >> $GITHUB_ENV
echo "COMMIT_HASH=${COMMIT_HASH}" >> $GITHUB_ENV
echo "COMMIT_HASH_FULL=${COMMIT_HASH_FULL}" >> $GITHUB_ENV
echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
echo "COMMIT_HASH_FULL=$(git rev-parse HEAD)" >> $GITHUB_ENV
# https://github.com/docker/login-action
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
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@v2
uses: docker/setup-qemu-action@v3
with:
platforms: 'amd64,arm64'

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

- name: Cache Docker layers
uses: actions/cache@v3
Expand All @@ -63,7 +56,7 @@ jobs:
# https://github.com/docker/metadata-action
- name: Gen docker meta
id: meta
uses: docker/metadata-action@v4
uses: docker/metadata-action@v5
with:
context: git
images: |
Expand All @@ -78,31 +71,34 @@ jobs:
# https://github.com/docker/build-push-action
- name: Build and Push
id: docker_build
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
builder: ${{ steps.buildx.outputs.name }}
context: ./
file: ./Dockerfile
platforms: 'linux/amd64,linux/arm64'
push: true
push: ${{ env.DRY_RUN == 'false' }}
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@v6
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: |
github.rest.repos.createCommitComment({
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 }}
23 changes: 12 additions & 11 deletions .github/workflows/build-frontend.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
name: Build Frontend
run-name: Build Frontend and Publish
run-name: Build Frontend and Publish (${{ github.event.client_payload.version }})

on:
repository_dispatch:
types: [artalk-release]

env:
VERSION: ${{ github.event.client_payload.version }}
DRY_RUN: ${{ github.event.client_payload.dry_run }}

jobs:
build_publish:
runs-on: ubuntu-latest

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

- name: Checkout latest tag
run: |
git fetch --prune --unshallow --tags -f
git checkout $(git describe --tags --abbrev=0)
uses: actions/checkout@v4
with:
ref: ${{ env.VERSION }}

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

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

- name: Install dependencies
run: pnpm install
run: pnpm install --frozen-lockfile

- name: Build
run: pnpm build

- name: Publish
run: pnpm publish -F artalk --no-git-checks
run: pnpm publish -F artalk ${{ env.DRY_RUN == 'false' && '' || '--dry-run' }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

# GitHub Packages
#
# Setup .npmrc file to publish to GitHub Packages
# - uses: actions/setup-node@v2
# - uses: actions/setup-node@v4
# with:
# registry-url: 'https://npm.pkg.github.com'
# # Publish to GitHub Packages
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs-cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:

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

- name: Use pnpm
uses: pnpm/action-setup@v2

- name: Setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18
cache: "pnpm"
Expand Down
25 changes: 20 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 📦 Release New Version
run-name: 📦 Release ${{ inputs.custom || inputs.semver }} by @${{ github.actor }}
run-name: 📦 Release ${{ inputs.custom || inputs.semver }} by @${{ github.actor }} ${{ inputs.dry-run && '(🧪 dry-run)' || '' }}

on:
workflow_dispatch:
Expand All @@ -16,6 +16,11 @@ on:
# description: 'Custom tag name (i.e v1.0.0-rc.1)'
# required: false
# default: ''
dry-run:
description: 'Dry run?'
type: boolean
required: false
default: false

env:
REPO_DISPATCH_EVENT_TYPE: 'artalk-release'
Expand All @@ -26,13 +31,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Git Fetch
run: git fetch --prune --unshallow --tags -f

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
registry-url: https://registry.npmjs.org/
Expand All @@ -45,6 +50,7 @@ jobs:
npm install -g semver
prev_version="$(git describe --tags --abbrev=0)"
echo "prev_version: ${prev_version}"
echo "PREV_VERSION=${prev_version}" >> $GITHUB_ENV
next_version="v$(semver --increment ${{ inputs.semver }} ${prev_version})"
fi
Expand All @@ -56,6 +62,9 @@ jobs:
echo "next_version: ${next_version}"
echo "VERSION=${next_version}" >> $GITHUB_ENV
- name: Print Next Version
run: echo "${VERSION}"

- name: Setup git-chglog
run: |
curl -sL $(curl -s https://api.github.com/repos/git-chglog/git-chglog/releases/latest \
Expand All @@ -75,14 +84,19 @@ jobs:
# modify the version in docs
perl -pi -e 's#"latest"(\W+)?:(\W+)?".*?"#"latest": "'${VERSION#v}'"#g' docs/code/ArtalkVersion.json
# git commit and push
# git commit
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git add .
git commit -m "chore: release ${VERSION}"
git tag "${VERSION}"
git push origin master "${VERSION}"
# git push
if [ "${{ inputs.dry-run }}" = "false" ]; then
git push origin master "${VERSION}"
else
git diff HEAD^ HEAD
fi
else
echo "skipped because git tag already exists."
fi
Expand All @@ -91,3 +105,4 @@ jobs:
uses: peter-evans/repository-dispatch@v2
with:
event-type: ${{ env.REPO_DISPATCH_EVENT_TYPE }}
client-payload: '{"version": ${{ toJSON(inputs.dry-run && env.PREV_VERSION || env.VERSION) }}, "dry_run": ${{ toJSON(inputs.dry-run) }}}'
2 changes: 1 addition & 1 deletion .github/workflows/test-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

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

- name: Use pnpm
uses: pnpm/action-setup@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

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

- uses: pnpm/action-setup@v2
with:
Expand Down

0 comments on commit bfabe18

Please sign in to comment.