This repository has been archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reusable docker workflow (#39)
- Loading branch information
Showing
1 changed file
with
11 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,325 +23,18 @@ on: | |
- reopened | ||
- closed | ||
|
||
env: | ||
# イメージをアップロードするレジストリ | ||
REGISTRY: ghcr.io | ||
# レジストリのログインユーザー名 | ||
DOCKER_USERNAME: ${{ github.actor }} | ||
# レジストリのログインパスワード | ||
DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }} | ||
# ビルドするプラットフォーム | ||
PLATFORMS: linux/amd64,linux/arm64 | ||
# ビルド対象情報の情報 | ||
TARGETS: >- | ||
[ | ||
{ imageName: "tomacheese/twitter-dm-memo", context: ".", file: "Dockerfile", packageName: "twitter-dm-memo" } | ||
] | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.merged == true && 'master' || github.event.pull_request.head.sha }} | ||
|
||
jobs: | ||
# バージョンを計算する | ||
calc-version: | ||
name: Calculate next version | ||
runs-on: ubuntu-latest | ||
|
||
outputs: | ||
# バージョンタグ | ||
tag: ${{ steps.tag-version.outputs.new_tag }} | ||
# リリースノート | ||
changelog: ${{ steps.tag-version.outputs.changelog }} | ||
# プラットフォーム(アーキテクチャ)のマトリクス | ||
platform-matrix: ${{ steps.platform-matrix.outputs.matrix }} | ||
# latestタグへのマージが必要かどうか (複数のプラットフォームがある時は必要) | ||
need-merge: ${{ steps.platform-matrix.outputs.need-merge }} | ||
# ビルド対象情報のマトリクス | ||
targets-matrix: ${{ steps.targets-matrix.outputs.matrix }} | ||
# パッケージのアップロードが必要かどうか | ||
need-upload: ${{ steps.need-upload.outputs.need-upload }} | ||
|
||
steps: | ||
- name: Check out the repo | ||
uses: actions/checkout@v3 | ||
with: | ||
# マージされていない時には github.event.pull_request.head.sha を使い、マージされた時にはmasterを使う | ||
ref: ${{ github.event.pull_request.merged == true && 'master' || github.event.pull_request.head.sha }} | ||
fetch-depth: 0 | ||
|
||
- name: Bump version and push tag | ||
id: tag-version | ||
uses: mathieudutour/[email protected] | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
default_bump: "minor" | ||
custom_release_rules: "feat:minor:✨ Features,fix:patch:🐛 Fixes,docs:patch:📰 Docs,chore:patch:🎨 Chore,pref:patch:🎈 Performance improvements,refactor:patch:🧹 Refactoring,build:patch:🔍 Build,ci:patch:🔍 CI,revert:patch:⏪ Revert,style:patch:🧹 Style,test:patch:👀 Test,release:major:📦 Release" | ||
dry_run: ${{ github.event.pull_request.merged == false }} | ||
|
||
- name: Set platform matrix | ||
id: platform-matrix | ||
run: | | ||
# shellcheck disable=SC2312 | ||
echo "matrix=[$(echo "$PLATFORMS" | sed -r -e 's/([^,]+)/\"\1\"/g' -e 's/([^,]+),/\1,/g')]" >> $GITHUB_OUTPUT | ||
# ,があるときはneed-merge=true | ||
echo "need-merge=$(echo "$PLATFORMS" | grep -q ',' && echo true || echo false)" >> $GITHUB_OUTPUT | ||
- name: Set targets matrix | ||
id: targets-matrix | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
core.setOutput('matrix', process.env.TARGETS) | ||
env: | ||
TARGETS: ${{ env.TARGETS }} | ||
|
||
- name: Check if need package upload | ||
id: need-upload | ||
run: | | ||
# package.json がない場合は false | ||
if [ ! -f package.json ]; then | ||
echo "need-upload=false" >> $GITHUB_OUTPUT | ||
exit 0 | ||
fi | ||
# "ncc build" が package.json にある場合は true、ない場合は false | ||
echo "need-upload=$(grep -q 'ncc build' package.json && echo true || echo false)" >> $GITHUB_OUTPUT | ||
# Dockerイメージをビルドする | ||
build: | ||
name: Docker build (${{ matrix.target.packageName }}, ${{ matrix.platform }}) | ||
runs-on: ubuntu-latest | ||
needs: calc-version | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# ビルドするプラットフォーム | ||
platform: ${{ fromJSON(needs.calc-version.outputs.platform-matrix) }} | ||
# ビルドするDockerfile | ||
target: ${{ fromJSON(needs.calc-version.outputs.targets-matrix) }} | ||
|
||
steps: | ||
- name: Remove platform prefix | ||
id: platform | ||
run: echo "shortPlatform=$(echo "${{ matrix.platform }}" | sed -r -e 's/^[^\/]+\///')" >> $GITHUB_OUTPUT | ||
|
||
- name: Create GitHub deployment | ||
uses: chrnorm/[email protected] | ||
if: github.event.pull_request.merged == true | ||
id: deployment | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
ref: ${{ needs.calc-version.outputs.tag }} | ||
environment: "${{ matrix.target.packageName }}:${{ steps.platform.outputs.shortPlatform }}" | ||
environment-url: https://github.com/${{ github.repository }}/pkgs/container/${{ matrix.target.packageName }} | ||
|
||
- name: Update deployment status (in_progress) | ||
uses: chrnorm/[email protected] | ||
if: github.event.pull_request.merged == true | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
state: in_progress | ||
deployment-id: ${{ steps.deployment.outputs.deployment_id }} | ||
|
||
- name: Check out the repo | ||
uses: actions/checkout@v3 | ||
with: | ||
# マージされていない時には github.event.pull_request.head.sha を使い、マージされた時にはmasterを使う | ||
ref: ${{ github.event.pull_request.merged == false && github.event.pull_request.head.sha || 'refs/heads/master' }} | ||
|
||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v2 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
|
||
- name: Login to ${{ env.REGISTRY }} | ||
if: github.event.pull_request.merged == true | ||
uses: docker/[email protected] | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ env.DOCKER_USERNAME }} | ||
password: ${{ env.DOCKER_PASSWORD }} | ||
|
||
- name: package.json update version | ||
run: | | ||
find . -type d -name node_modules -prune -o -type f -name package.json -exec sed -r -i "1,/version/s/\"version\": \".+\"/\"version\": \"${{ needs.calc-version.outputs.tag }}\"/" {} \; | ||
git diff | ||
- name: Set tag suffix value | ||
id: tag | ||
run: | | ||
if [ "${{ needs.calc-version.outputs.need-merge }}" = "true" ]; then | ||
echo "suffix=-${{ steps.platform.outputs.shortPlatform }}" >> $GITHUB_OUTPUT | ||
else | ||
echo "suffix=" >> $GITHUB_OUTPUT | ||
fi | ||
- name: Extract metadata (tags, labels) for Docker | ||
id: meta | ||
uses: docker/[email protected] | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ matrix.target.imageName }} | ||
tags: | | ||
# need-mergeがtrueの時はshortPlatformを含める (latest-arm64) | ||
# need-mergeがfalseの時はshortPlatformを含めない (latest) | ||
type=raw,value=${{ needs.calc-version.outputs.tag }}${{ steps.tag.outputs.suffix }} | ||
type=raw,value=latest${{ steps.tag.outputs.suffix }} | ||
- name: Build and push Docker image | ||
uses: docker/[email protected] | ||
with: | ||
context: ${{ matrix.target.context }} | ||
file: ${{ matrix.target.file }} | ||
push: ${{ github.event.pull_request.merged == true }} | ||
platforms: ${{ matrix.platform }} | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
cache-from: type=gha,scope=${{ matrix.target.packageName }}-${{ steps.platform.outputs.shortPlatform }} | ||
cache-to: type=gha,mode=max,scope=${{ matrix.target.packageName }}-${{ steps.platform.outputs.shortPlatform }} | ||
provenance: false | ||
|
||
- name: Update deployment status (success) | ||
uses: chrnorm/[email protected] | ||
if: ${{ success() && github.event.pull_request.merged == true }} | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
state: success | ||
deployment-id: ${{ steps.deployment.outputs.deployment_id }} | ||
|
||
- name: Update deployment status (failure) | ||
if: ${{ failure() && github.event.pull_request.merged == true }} | ||
uses: chrnorm/[email protected] | ||
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
state: failure | ||
deployment-id: ${{ steps.deployment.outputs.deployment_id }} | ||
|
||
finished-build: | ||
name: Check finished build | ||
runs-on: ubuntu-latest | ||
needs: | ||
- calc-version | ||
- build | ||
|
||
steps: | ||
- name: Check finished build | ||
run: echo ok | ||
|
||
# 各プラットフォームのlatestからlatestタグにマージしてイメージを作成 | ||
merge-latest: | ||
name: Merge latest (${{ matrix.target.packageName }}) | ||
runs-on: ubuntu-latest | ||
if: github.event.pull_request.merged == true | ||
needs: | ||
- calc-version | ||
- build | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# ビルドするDockerfile | ||
target: ${{ fromJSON(needs.calc-version.outputs.targets-matrix) }} | ||
|
||
steps: | ||
- name: Login to ${{ env.REGISTRY }} | ||
if: github.event.pull_request.merged == true | ||
uses: docker/[email protected] | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ env.DOCKER_USERNAME }} | ||
password: ${{ env.DOCKER_PASSWORD }} | ||
|
||
- name: Create extra-images list | ||
id: extra-images | ||
run: | | ||
# linux/amd64,linux/arm64 -> ${{ env.REGISTRY }}/tomacheese/my-pixiv:latest-amd64,${{ env.REGISTRY }}/tomacheese/my-pixiv:latest-arm64 | ||
echo "images=$(echo "$PLATFORMS" | sed -r 's#([a-z]+)\/([a-z0-9]+)#${{ env.REGISTRY }}\/${{ matrix.target.imageName }}:latest-\2#g')" >> $GITHUB_OUTPUT | ||
- name: Create and push manifest images (latest) | ||
uses: Noelware/[email protected] | ||
# プラットフォームがひとつの場合はマージ処理を行わない | ||
if: ${{ needs.calc-version.outputs.need-merge == 'true' }} | ||
with: | ||
base-image: ${{ env.REGISTRY }}/${{ matrix.target.imageName }}:latest | ||
extra-images: ${{ steps.extra-images.outputs.images }} | ||
push: true | ||
|
||
- name: Create and push manifest images (version) | ||
uses: Noelware/[email protected] | ||
# プラットフォームがひとつの場合はマージ処理を行わない | ||
if: ${{ needs.calc-version.outputs.need-merge == 'true' }} | ||
with: | ||
base-image: ${{ env.REGISTRY }}/${{ matrix.target.imageName }}:${{ needs.calc-version.outputs.tag }} | ||
extra-images: ${{ steps.extra-images.outputs.images }} | ||
push: true | ||
|
||
release: | ||
name: Release ${{ needs.calc-version.outputs.tag }} | ||
runs-on: ubuntu-latest | ||
if: github.event.pull_request.merged == true | ||
needs: | ||
- calc-version | ||
- build | ||
- merge-latest | ||
|
||
outputs: | ||
upload_url: ${{ steps.create_release.outputs.upload_url }} | ||
|
||
steps: | ||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ needs.calc-version.outputs.tag }} | ||
release_name: ${{ needs.calc-version.outputs.tag }} | ||
body: ${{ needs.calc-version.outputs.changelog }} | ||
draft: false | ||
prerelease: false | ||
|
||
upload-package: | ||
name: Upload package ${{ needs.calc-version.outputs.tag }} | ||
runs-on: ubuntu-latest | ||
if: ${{ github.event.pull_request.merged == true && needs.calc-version.outputs.need-upload == 'true' }} | ||
needs: | ||
- calc-version | ||
- build | ||
- merge-latest | ||
- release | ||
|
||
steps: | ||
- name: Check out the repo | ||
uses: actions/checkout@v3 | ||
with: | ||
# マージされていない時には github.event.pull_request.head.sha を使い、マージされた時にはmasterを使う | ||
ref: ${{ github.event.pull_request.merged == true && 'master' || github.event.pull_request.head.sha }} | ||
|
||
- name: 🏗 Setup node env | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
|
||
- name: Install dependencies | ||
run: yarn | ||
|
||
- name: 🎁 Run package | ||
run: yarn package | ||
|
||
- name: Create zip | ||
id: create-zip | ||
working-directory: output | ||
run: | | ||
REPO="${GITHUB_REPOSITORY#"${GITHUB_REPOSITORY_OWNER}/"}" | ||
zip -r "${REPO}_${{ needs.calc-version.outputs.tag }}.zip" ./* | ||
echo "zip-path=output/${REPO}-${{ needs.calc-version.outputs.tag }}.zip" >> $GITHUB_OUTPUT | ||
echo "zip-filename=${REPO}-${{ needs.calc-version.outputs.tag }}.zip" >> $GITHUB_OUTPUT | ||
- name: Upload Release Asset | ||
uses: shogo82148/[email protected] | ||
with: | ||
upload_url: ${{ needs.release.outputs.upload_url }} | ||
asset_path: ${{ steps.create-zip.outputs.zip-path }} | ||
asset_name: ${{ steps.create-zip.outputs.zip-filename }} | ||
asset_content_type: application/zip | ||
docker-ci: | ||
name: Docker CI | ||
uses: book000/templates/.github/workflows/reusable-docker.yml@master | ||
with: | ||
targets: >- | ||
[ | ||
{ imageName: "tomacheese/twitter-dm-memo", context: ".", file: "Dockerfile", packageName: "twitter-dm-memo" } | ||
] | ||
secrets: | ||
DOCKER_USERNAME: ${{ github.actor }} | ||
DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |