ci: rewrite #3453
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
name: build | |
on: | |
push: | |
branches: ["**"] | |
tags: [v*.*.*] | |
pull_request: | |
workflow_call: | |
inputs: | |
tag-name: | |
required: true | |
type: string | |
outputs: | |
image_tags: | |
value: ${{ jobs.docker.outputs.image_tags }} | |
image_url: | |
value: https://ghcr.io/${{ github.repository }} | |
env: | |
IMAGE_REGISTRY: ghcr.io | |
IMAGE_NAMESPACE: ${{ github.repository_owner }} | |
IMAGE_NAME: ${{ github.event.repository.name }} | |
# renovate: datasource=github-releases depName=docker/buildx | |
BUILDX_VERSION: v0.16.2 | |
jobs: | |
env: | |
# release-please によるコミットの時は workflow_call でのみ実行する | |
if: ${{ !( github.workflow == 'build' && startsWith(github.event.head_commit.message, 'release:') && github.event.head_commit.author.name == 'github-actions[bot]' ) }} | |
runs-on: ubuntu-latest | |
outputs: | |
BINARY_NAME: ${{ steps.meta.outputs.BINARY_NAME }} | |
IMAGE_NAMESPACE: ${{ steps.lowercase.outputs.IMAGE_NAMESPACE }} | |
PUSH: ${{ github.event_name != 'pull_request' && github.ref_name == github.event.repository.default_branch }} | |
steps: | |
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | |
- id: meta | |
run: | | |
echo "BINARY_NAME=$(cargo metadata --offline --no-deps --format-version=1 | jq -r '.packages[].name')" >> $GITHUB_OUTPUT | |
- id: lowercase | |
run: | | |
image_namespace='${{ env.IMAGE_NAMESPACE }}' | |
echo "IMAGE_NAMESPACE=${image_namespace@L}" >> $GITHUB_OUTPUT | |
build: | |
needs: [env] | |
permissions: | |
contents: write | |
packages: write | |
strategy: | |
fail-fast: false | |
matrix: | |
target: [aarch64-unknown-linux-gnu, aarch64-unknown-linux-musl] | |
use-cross: [true] | |
include: | |
- target: x86_64-unknown-linux-gnu | |
use-cross: false | |
- target: x86_64-unknown-linux-musl | |
use-cross: false | |
# - os: macos-latest | |
# target: aarch64-apple-darwin | |
# - os: macos-latest | |
# target: x86_64-apple-darwin | |
# - os: windows-latest | |
# target: x86_64-pc-windows-msvc | |
# - os: windows-latest | |
# target: x86_64-pc-windows-gnu | |
runs-on: ${{ matrix.os || 'ubuntu-latest'}} | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GH_REPO: ${{ github.repository }} | |
defaults: | |
run: | |
shell: bash -xe {0} | |
steps: | |
- name: Install musl tools | |
if: matrix.target == 'x86_64-unknown-linux-musl' | |
run: | | |
sudo apt-get install -y musl-tools --no-install-recommends | |
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | |
- uses: dtolnay/rust-toolchain@master | |
with: | |
toolchain: stable | |
target: ${{ matrix.target }} | |
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 | |
with: | |
key: ${{ matrix.target }} | |
- name: Install cross | |
if: ${{ !matrix.os && matrix.use-cross }} | |
uses: taiki-e/install-action@0593da407881e996340c22f60617ba9d95f2b995 # v2.42.27 | |
with: | |
tool: cross | |
- run: mkdir dist | |
- run: ${{ (!matrix.os && matrix.use-cross) && 'cross' || 'cargo' }} rustc --locked --release --target=${{ matrix.target }} -- --emit=link=dist/${{ needs.env.outputs.BINARY_NAME }} | |
- uses: actions/upload-artifact@834a144ee995460fba8ed112a2fc961b36a5ec5a # v4.3.6 | |
with: | |
name: ${{ matrix.target }} | |
path: | | |
dist/${{ needs.env.outputs.BINARY_NAME }} | |
dist/${{ needs.env.outputs.BINARY_NAME }}.exe | |
- name: Upload to release | |
if: ${{ inputs.tag-name }} | |
working-directory: dist/ | |
run: | | |
if [ -e ${{ needs.env.outputs.BINARY_NAME }}.exe ]; then | |
filename="${{ needs.env.outputs.BINARY_NAME }}-${{ inputs.tag-name }}-${{ matrix.target }}.exe" | |
mv ${{ needs.env.outputs.BINARY_NAME }}.exe "$filename" | |
gh release upload ${{ inputs.tag-name }} "$filename"#${{ matrix.target }} --clobber | |
else | |
filename="${{ needs.env.outputs.BINARY_NAME }}-${{ inputs.tag-name }}-${{ matrix.target }}" | |
mv ${{ needs.env.outputs.BINARY_NAME }} "$filename" | |
gh release upload ${{ inputs.tag-name }} "$filename"#${{ matrix.target }} --clobber | |
fi | |
image: | |
permissions: | |
packages: write | |
needs: [env, build] | |
if: ${{ fromJson(needs.env.outputs.PUSH) }} | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash -xe {0} | |
steps: | |
- name: Download build artifact | |
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 | |
with: | |
path: artifact | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1 | |
with: | |
images: ${{ env.IMAGE_REGISTRY }}/${{ needs.env.outputs.IMAGE_NAMESPACE }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=semver,pattern={{version}},value=${{ inputs.tag-name }} | |
type=semver,pattern={{major}}.{{minor}},value=${{ inputs.tag-name }} | |
type=semver,pattern={{major}},value=${{ inputs.tag-name }} | |
type=edge | |
type=ref,event=branch | |
- name: Build Image (linux/amd64) | |
run: | | |
newcontainer=$(buildah from --platform=linux/amd64 scratch) | |
buildah copy $newcontainer ./artifact/x86_64-unknown-linux-musl/${{ needs.env.outputs.BINARY_NAME }} /${{ needs.env.outputs.BINARY_NAME }} | |
buildah config --entrypoint="./${{ needs.env.outputs.BINARY_NAME }}" $newcontainer | |
buildah config --author="Mogyuchi" $(awk '{print "--label=" $0}' <<< "${{ steps.meta.outputs.labels }}") $newcontainer | |
buildah inspect $newcontainer | |
buildah commit --manifest=localhost/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} $newcontainer | |
buildah rm $newcontainer | |
- name: Build Image (linux/arm64) | |
run: | | |
newcontainer=$(buildah from --platform=linux/arm64 scratch) | |
buildah copy $newcontainer ./artifact/aarch64-unknown-linux-musl/${{ needs.env.outputs.BINARY_NAME }} /${{ needs.env.outputs.BINARY_NAME }} | |
buildah config --entrypoint="./${{ needs.env.outputs.BINARY_NAME }}" $newcontainer | |
buildah config --author="Mogyuchi" $(awk '{print "--label=" $0}' <<< "${{ steps.meta.outputs.labels }}") $newcontainer | |
buildah inspect $newcontainer | |
buildah commit --manifest=localhost/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} $newcontainer | |
buildah rm $newcontainer | |
- id: push | |
name: Push To GHCR | |
run: | | |
buildah manifest inspect localhost/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} | |
echo "${{ steps.meta.outputs.tags }}" | xargs -I{} --max-args=1 buildah --log-level=trace manifest push --creds=${{ github.actor }}:${{ secrets.GITHUB_TOKEN }} --all localhost/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} docker://{} |