Skip to content

Commit

Permalink
ci: rewrite ci workflow (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
book000 committed Jan 26, 2023
1 parent 6133c1c commit b763dff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 85 deletions.
110 changes: 51 additions & 59 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ on:
- closed

env:
# イメージをアップロードするレジストリ
REGISTRY: ghcr.io
# レジストリのログインユーザー名
DOCKER_USERNAME: ${{ github.actor }}
# レジストリのログインパスワード
DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
# ビルドするプラットフォーム
PLATFORMS: linux/amd64,linux/arm64
# ビルド対象情報の情報
TARGETS: >-
[
{ imageName: "tomacheese/pixiv-public-to-private", context: ".", file: "Dockerfile", packageName: "pixiv-public-to-private" }
]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.merged == true && 'master' || github.event.pull_request.head.sha }}
Expand All @@ -47,6 +56,8 @@ jobs:
platform-matrix: ${{ steps.platform-matrix.outputs.matrix }}
# latestタグへのマージが必要かどうか (複数のプラットフォームがある時は必要)
need-merge: ${{ steps.platform-matrix.outputs.need-merge }}
# ビルド対象情報のマトリクス
targets-matrix: ${{ steps.targets-matrix.outputs.matrix }}

steps:
- name: Check out the repo
Expand All @@ -61,8 +72,8 @@ jobs:
uses: mathieudutour/github-tag-action@v6.1
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'
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
Expand All @@ -73,26 +84,33 @@ jobs:
# ,があるときは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 }}

# Dockerイメージをビルドする
build:
name: Docker build (${{ fromJson(matrix.target).file }}, ${{ matrix.platform }})
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) }}
platform: ${{ fromJSON(needs.calc-version.outputs.platform-matrix) }}
# ビルドするDockerfile
target:
- '{ imageName: "tomacheese/pixiv-public-to-private", file: "Dockerfile", packageName: "pixiv-public-to-private" }'
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
run: echo "shortPlatform=$(echo "${{ matrix.platform }}" | sed -r -e 's/^[^\/]+\///')" >> $GITHUB_OUTPUT

- name: Create GitHub deployment
uses: chrnorm/deployment-action@v2.0.5
Expand All @@ -101,8 +119,8 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ needs.calc-version.outputs.tag }}
environment: '${{ fromJson(matrix.target).packageName }}:${{ steps.platform.outputs.shortPlatform }}'
environment-url: https://github.com/${{ github.repository }}/pkgs/container/${{ fromJson(matrix.target).packageName }}
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/deployment-status@v2.0.1
Expand Down Expand Up @@ -134,13 +152,13 @@ jobs:

- name: package.json update version
run: |
sed -r -i "1,/version/s/\"version\": \".+\"/\"version\": \"${{ needs.calc-version.outputs.tag }}\"/" package.json
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 [ "${{ fromJson(needs.calc-version.outputs.need-merge) }}" = "true" ]; then
if [ "${{ needs.calc-version.outputs.need-merge }}" = "true" ]; then
echo "suffix=-${{ steps.platform.outputs.shortPlatform }}" >> $GITHUB_OUTPUT
else
echo "suffix=" >> $GITHUB_OUTPUT
Expand All @@ -150,7 +168,7 @@ jobs:
id: meta
uses: docker/metadata-action@v4.3.0
with:
images: ${{ env.REGISTRY }}/${{ fromJson(matrix.target).imageName }}
images: ${{ env.REGISTRY }}/${{ matrix.target.imageName }}
tags: |
# need-mergeがtrueの時はshortPlatformを含める (latest-arm64)
# need-mergeがfalseの時はshortPlatformを含めない (latest)
Expand All @@ -160,14 +178,14 @@ jobs:
- name: Build and push Docker image
uses: docker/build-push-action@v3.3.0
with:
context: .
file: ${{ fromJson(matrix.target).file }}
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=${{ fromJson(matrix.target).packageName }}-${{ steps.platform.outputs.shortPlatform }}
cache-to: type=gha,mode=max,scope=${{ fromJson(matrix.target).packageName }}-${{ steps.platform.outputs.shortPlatform }}
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)
Expand All @@ -186,9 +204,20 @@ jobs:
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 (${{ fromJson(matrix.target).file }})
name: Merge latest (${{ matrix.target.packageName }})
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true
needs:
Expand All @@ -199,8 +228,7 @@ jobs:
fail-fast: false
matrix:
# ビルドするDockerfile
target:
- '{ imageName: "tomacheese/pixiv-public-to-private", file: "Dockerfile", packageName: "pixiv-public-to-private" }'
target: ${{ fromJSON(needs.calc-version.outputs.targets-matrix) }}

steps:
- name: Login to ${{ env.REGISTRY }}
Expand All @@ -215,14 +243,14 @@ jobs:
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 }}\/${{ fromJson(matrix.target).imageName }}:latest-\2#g')" >> $GITHUB_OUTPUT
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/docker-manifest-action@v0.2.3
# プラットフォームがひとつの場合はマージ処理を行わない
if: ${{ needs.calc-version.outputs.need-merge == 'true' }}
with:
base-image: ${{ env.REGISTRY }}/${{ fromJson(matrix.target).imageName }}:latest
base-image: ${{ env.REGISTRY }}/${{ matrix.target.imageName }}:latest
extra-images: ${{ steps.extra-images.outputs.images }}
push: true

Expand All @@ -231,7 +259,7 @@ jobs:
# プラットフォームがひとつの場合はマージ処理を行わない
if: ${{ needs.calc-version.outputs.need-merge == 'true' }}
with:
base-image: ${{ env.REGISTRY }}/${{ fromJson(matrix.target).imageName }}:${{ needs.calc-version.outputs.tag }}
base-image: ${{ env.REGISTRY }}/${{ matrix.target.imageName }}:${{ needs.calc-version.outputs.tag }}
extra-images: ${{ steps.extra-images.outputs.images }}
push: true

Expand Down Expand Up @@ -259,39 +287,3 @@ jobs:
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
- 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: Upload Release Asset
uses: shogo82148/actions-upload-release-asset@v1.6.3
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: output/index.js
asset_name: index.js
asset_content_type: text/javascript
21 changes: 21 additions & 0 deletions .github/workflows/hadolint-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: hadolint

on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

jobs:
hadolint:
runs-on: ubuntu-latest

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

- uses: hadolint/hadolint-action@v3.0.0
33 changes: 7 additions & 26 deletions .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Node.js v14 と v16 でビルド・テストを実行する
# Node.js でビルド・テストを実行する。バージョンは .node-version に記載されているものを利用する

name: Node CI

Expand All @@ -13,25 +13,20 @@ on:
- master

jobs:
ci:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest]
node: [14, 16]
fail-fast: false
node-ci:
runs-on: ubuntu-latest

steps:
- name: 🛎 Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: 🏗 Setup node env
- name: 🏗 Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
node-version-file: .node-version
cache: yarn

- name: 📃 Check package.json definition
id: package-json
Expand All @@ -57,22 +52,8 @@ jobs:
echo "lint=$lint" >> $GITHUB_OUTPUT
echo "test=$test" >> $GITHUB_OUTPUT
- name: 🛠 Get yarn cache directory path
id: yarn-cache-dir-path
run: |
echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: 📦 Cache node_modules
uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: 👨🏻‍💻 Install dependencies
run: yarn
run: yarn install --frozen-lockfile

- name: 👀 Run linter
if: steps.package-json.outputs.lint == 'true'
Expand Down

0 comments on commit b763dff

Please sign in to comment.