-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
208 lines (205 loc) · 8.69 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
name: 'ASIMOV build rust action'
author: 'ASIMOV by Haltia.AI <[email protected]>'
description: 'Action to help automate building rust crates'
inputs:
target:
description: 'Target to build for.'
required: true
artifact-name:
description: 'Name of the output artifact. Must be unique. Prefixed with artifact-prefix.'
required: true
artifact-prefix:
description: 'Prefix for the output artifact name. Defaults to package name.'
required: false
package-name:
description: 'Name of the package. Default is parsed from Cargo.toml.'
required: false
binary-name:
description: 'Name of the output binary file. Defaults to package name with extension.'
required: false
binary-extension:
description: "Extension for the output binary file. Defaults to empty string."
required: false
working-directory:
description: 'Working directory. Defaults to root.'
required: false
default: '.'
checkout:
description: 'Checkout repository? Defaults to true.'
required: false
default: 'true'
rust-toolchain:
description: 'Rust toolchain to use. Defaults to stable.'
required: false
default: 'stable'
install-macos-sdk:
description: 'Install macOS SDK? Defaults to false.'
required: false
default: 'false'
use-zigbuild:
description: 'Use cargo-zigbuild? Defaults to false.'
required: false
default: 'false'
extra-build-arguments:
description: 'Extra arguments to pass to cargo build. Defaults to empty string.'
required: false
default: ''
strip-artifact:
description: 'Strip symbols? Defaults to false.'
required: false
default: 'false'
compress-artifact:
description: 'Compress artifact? Defaults to true.'
required: false
default: 'true'
upload-artifact:
description: 'Upload artifact? Defaults to true.'
required: false
default: 'true'
outputs:
artifact-path:
description: 'Path to an Artifact.'
value: ${{ steps.finalize.outputs.artifact-path }}
artifact-id:
description: 'GitHub ID of an Artifact, can be used by the REST API.'
value: ${{ steps.upload.outputs.artifact-id }}
artifact-url:
description: 'URL to download an Artifact.'
value: ${{ steps.upload.outputs.artifact-url }}
runs:
using: 'composite'
steps:
- name: Install Rust and target
id: toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ inputs.rust-toolchain }}
target: ${{ inputs.target }}
- name: Checkout repository
if: ${{ inputs.checkout == 'true' }}
uses: actions/checkout@v4
with:
path: ${{ inputs.working-directory }}
- name: Obtain package name
id: jq
if: ${{ inputs.package-name == '' }}
working-directory: ${{ inputs.working-directory }}
shell: bash
run: |
METADATA=$(cargo metadata --format-version 1 --no-deps)
VALID_TARGETS=$(echo "$METADATA" | jq -r '. as $root | .packages[] | select(.id | IN($root.workspace_default_members[])) | select(.targets[].kind[] | contains("bin")) | .name')
VALID_TARGETS_COUNT=$(echo "$VALID_TARGETS" | wc -l)
if [ "$VALID_TARGETS_COUNT" -eq 0 ]; then
echo "::error::No valid targets found. Please specify package-name input."
exit 1
elif [ "$VALID_TARGETS_COUNT" -gt 1 ]; then
echo "::error::Multiple valid targets found. Please specify package-name input."
exit 1
fi
PACKAGE_NAME=$(echo "$VALID_TARGETS" | head -n 1)
echo "result=$PACKAGE_NAME" >> $GITHUB_OUTPUT
- name: Prepare environment
id: globals
working-directory: ${{ inputs.working-directory }}
shell: bash
run: |
EXT="${{ inputs.binary-extension != '' && '.' || '' }}${{ inputs.binary-extension }}"
PACKAGE_NAME="${{ inputs.package-name != '' && inputs.package-name || steps.jq.outputs.result }}"
BINARY_NAME="${{ inputs.binary-name != '' && inputs.binary-name || '$PACKAGE_NAME' }}$EXT"
ARTIFACT_PREFIX="${{ inputs.artifact-prefix != '' && inputs.artifact-prefix || '$PACKAGE_NAME' }}"
ARTIFACT_NAME="${ARTIFACT_PREFIX}-${{ inputs.artifact-name }}"
ARTIFACT_NAME_EXT="$ARTIFACT_NAME$EXT"
IS_TARGET_WINDOWS="${{ contains(inputs.target, 'windows') }}"
OUTPUT_DIR="output"
OUTPUT_BIINARY_PATH="${OUTPUT_DIR}/$BINARY_NAME"
OUTPUT_ARTIFACT_PATH="${OUTPUT_DIR}/$ARTIFACT_NAME"
OUTPUT_ARTIFACT_PATH_EXT="${OUTPUT_DIR}/$ARTIFACT_NAME_EXT"
TARGET_DIR=$(cargo metadata --format-version 1 --no-deps | jq -r '.target_directory')
echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
echo "artifact_name=$ARTIFACT_NAME" >> $GITHUB_OUTPUT
echo "artifact_name_ext=$ARTIFACT_NAME_EXT" >> $GITHUB_OUTPUT
echo "is_target_windows=$IS_TARGET_WINDOWS" >> $GITHUB_OUTPUT
echo "output_dir=$OUTPUT_DIR" >> $GITHUB_OUTPUT
echo "output_binary_path=$OUTPUT_BIINARY_PATH" >> $GITHUB_OUTPUT
echo "output_artifact_path=$OUTPUT_ARTIFACT_PATH" >> $GITHUB_OUTPUT
echo "output_artifact_path_ext=$OUTPUT_ARTIFACT_PATH_EXT" >> $GITHUB_OUTPUT
echo "target_dir=$TARGET_DIR" >> $GITHUB_OUTPUT
- name: Install MinGW
if: ${{ runner.os != 'windows' && inputs.target == 'x86_64-pc-windows-gnu' }}
shell: bash
run: |
sudo apt-get update
sudo apt-get install -yq --no-install-recommends mingw-w64
- name: Install macOS SDK
id: macos-sdk
if: ${{ inputs.install-macos-sdk == 'true' }}
shell: bash
working-directory: ${{ runner.temp }}
run: |
curl -L https://github.com/roblabla/MacOSX-SDKs/releases/download/13.3/MacOSX13.3.sdk.tar.xz | tar xJ
echo "root=$(pwd)/MacOSX13.3.sdk/" >> $GITHUB_OUTPUT
- name: Install zig
if: ${{ inputs.use-zigbuild == 'true' }}
uses: mlugg/setup-zig@v1
- name: Install cargo-zigbuild
if: ${{ inputs.use-zigbuild == 'true' }}
shell: bash
run: cargo install --locked cargo-zigbuild
- name: Build
working-directory: ${{ inputs.working-directory }}
shell: bash
run: |
BUILD_ARG="${{ inputs.use-zigbuild == 'true' && 'zigbuild' || 'build' }}"
export SDKROOT="${{ inputs.use-zigbuild == 'true' && steps.macos-sdk.outputs.root || '' }}"
cargo +${{ steps.toolchain.outputs.name }} $BUILD_ARG --release --package ${{ steps.globals.outputs.package_name }} --target ${{ inputs.target }} ${{ inputs.extra-build-arguments }}
- name: Move artifact
working-directory: ${{ inputs.working-directory }}
shell: bash
run: |
mkdir ${{ steps.globals.outputs.output_dir }}
mv ${{ steps.globals.outputs.target_dir }}/${{ inputs.target }}/release/${{ steps.globals.outputs.binary_name }} \
${{ steps.globals.outputs.output_artifact_path_ext }}
- name: Strip artifact
if: ${{ inputs.strip-artifact == 'true' }}
working-directory: ${{ inputs.working-directory }}
shell: bash
run: strip ${{ steps.globals.outputs.output_artifact_path_ext }}
- name: Compress or archive artifact
id: compress
if: ${{ inputs.compress-artifact == 'true' }}
shell: bash
run: |
ARTIFACT_PATH="${{ inputs.working-directory }}/${{ steps.globals.outputs.output_artifact_path_ext }}"
INPUT_PATH="${{ inputs.working-directory }}/${{ steps.globals.outputs.output_binary_path }}"
OUTPUT_PATH="${{ inputs.working-directory }}/${{ steps.globals.outputs.output_artifact_path }}"
mv "$ARTIFACT_PATH" "$INPUT_PATH"
if [ "$RUNNER_OS" = "Windows" ]; then
OUTPUT_PATH="$OUTPUT_PATH.zip"
7z a -tzip -sdel "$OUTPUT_PATH" "$INPUT_PATH"
elif [ "${{ steps.globals.outputs.is_target_windows }}" = "true" ]; then
OUTPUT_PATH="$OUTPUT_PATH.zip"
zip -mj "$OUTPUT_PATH" "$INPUT_PATH"
else
OUTPUT_PATH="$OUTPUT_PATH.gz"
gzip -f "$INPUT_PATH"
mv "$INPUT_PATH.gz" "$OUTPUT_PATH"
fi
echo "output-path=$OUTPUT_PATH" >> $GITHUB_OUTPUT
- name: Upload artifact
id: upload
if: ${{ inputs.upload-artifact == 'true' }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.artifact-name }}
path: ${{ inputs.working-directory }}/${{ steps.globals.outputs.output_dir }}/*
if-no-files-found: error
- name: Write outputs
id: finalize
shell: bash
run: |
if [ "${{ inputs.compress-artifact }}" == "true" ]; then
echo "artifact-path=${{ steps.compress.outputs.output-path }}" >> $GITHUB_OUTPUT
else
echo "artifact-path=${{ inputs.working-directory }}/${{ steps.globals.outputs.output_artifact_path_ext }}" >> $GITHUB_OUTPUT
fi