Skip to content

Commit 85a3acc

Browse files
committed
ci: add cd.yml
Signed-off-by: Deep Panchal <[email protected]>
1 parent 85d7be5 commit 85a3acc

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

.github/workflows/cd.yml

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
name: CD # Continuous Deployment
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
- master
9+
10+
env:
11+
CARGO_TERM_COLOR: always
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
17+
jobs:
18+
extract-version:
19+
name: Extract Version
20+
runs-on: ubuntu-latest
21+
outputs:
22+
version: ${{ steps.get_version.outputs.version }}
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
- name: Install Rust toolchain
27+
uses: actions-rs/toolchain@v1
28+
with:
29+
toolchain: stable
30+
profile: minimal
31+
- name: Get version from Cargo.toml
32+
id: get_version
33+
run: |
34+
VERSION=$(cargo pkgid | sed 's/.*#//')
35+
echo "version=$VERSION" >> $GITHUB_OUTPUT
36+
37+
release-please:
38+
name: Release Please 🏷️
39+
runs-on: ubuntu-latest
40+
needs: extract-version
41+
permissions:
42+
contents: write
43+
pull-requests: write
44+
outputs:
45+
created: ${{ steps.release.outputs.release_created }}
46+
tag_name: ${{ steps.release.outputs.tag_name }}
47+
steps:
48+
- uses: google-github-actions/release-please-action@v4
49+
id: release
50+
with:
51+
command: manifest
52+
53+
release-assets:
54+
name: Publishing for ${{ matrix.os }}
55+
needs: [release-please, extract-version]
56+
if: needs.release-please.outputs.created
57+
runs-on: ${{ matrix.os }}
58+
strategy:
59+
matrix:
60+
include:
61+
- os: macos-latest
62+
os-name: macos
63+
target: x86_64-apple-darwin
64+
architecture: x86_64
65+
binary-postfix: ""
66+
binary-name: aws-iot-mqtt-cli
67+
use-cross: false
68+
- os: macos-latest
69+
os-name: macos
70+
target: aarch64-apple-darwin
71+
architecture: arm64
72+
binary-postfix: ""
73+
use-cross: false
74+
binary-name: aws-iot-mqtt-cli
75+
- os: ubuntu-latest
76+
os-name: linux
77+
target: x86_64-unknown-linux-gnu
78+
architecture: x86_64
79+
binary-postfix: ""
80+
use-cross: false
81+
binary-name: aws-iot-mqtt-cli
82+
- os: windows-latest
83+
os-name: windows
84+
target: x86_64-pc-windows-msvc
85+
architecture: x86_64
86+
binary-postfix: ".exe"
87+
use-cross: false
88+
binary-name: aws-iot-mqtt-cli
89+
- os: ubuntu-latest
90+
os-name: linux
91+
target: aarch64-unknown-linux-gnu
92+
architecture: arm64
93+
binary-postfix: ""
94+
use-cross: true
95+
binary-name: aws-iot-mqtt-cli
96+
- os: ubuntu-latest
97+
os-name: linux
98+
target: i686-unknown-linux-gnu
99+
architecture: i686
100+
binary-postfix: ""
101+
use-cross: true
102+
binary-name: aws-iot-mqtt-cli
103+
steps:
104+
- name: Checkout repository
105+
uses: actions/checkout@v4
106+
- name: Install Rust toolchain
107+
uses: actions-rs/toolchain@v1
108+
with:
109+
toolchain: stable
110+
target: ${{ matrix.target }}
111+
profile: minimal
112+
override: true
113+
- uses: Swatinem/rust-cache@v2
114+
- name: Cargo build
115+
uses: actions-rs/cargo@v1
116+
with:
117+
command: build
118+
use-cross: ${{ matrix.use-cross }}
119+
toolchain: stable
120+
args: --release --target ${{ matrix.target }}
121+
- name: Install strip command
122+
shell: bash
123+
run: |
124+
if [[ ${{ matrix.target }} == aarch64-unknown-linux-gnu ]]; then
125+
sudo apt update
126+
sudo apt-get install -y binutils-aarch64-linux-gnu
127+
fi
128+
- name: Packaging final binary
129+
shell: bash
130+
env:
131+
VERSION: ${{ needs.extract-version.outputs.version }}
132+
run: |
133+
cd target/${{ matrix.target }}/release
134+
135+
####### reduce binary size by removing debug symbols #######
136+
137+
BINARY_NAME=${{ matrix.binary-name }}${{ matrix.binary-postfix }}
138+
if [[ ${{ matrix.target }} == aarch64-unknown-linux-gnu ]]; then
139+
GCC_PREFIX="aarch64-linux-gnu-"
140+
else
141+
GCC_PREFIX=""
142+
fi
143+
"$GCC_PREFIX"strip $BINARY_NAME
144+
145+
########## create tar.gz ##########
146+
147+
RELEASE_NAME=${{ matrix.binary-name }}-${{ env.VERSION }}-${{ matrix.os-name }}-${{ matrix.architecture }}
148+
149+
tar czvf $RELEASE_NAME.tar.gz $BINARY_NAME
150+
151+
########## create sha256 ##########
152+
153+
if [[ ${{ runner.os }} == 'Windows' ]]; then
154+
certutil -hashfile $RELEASE_NAME.tar.gz sha256 | grep -E [A-Fa-f0-9]{64} > $RELEASE_NAME.sha256
155+
else
156+
shasum -a 256 $RELEASE_NAME.tar.gz > $RELEASE_NAME.sha256
157+
fi
158+
159+
- name: Releasing assets 📦
160+
uses: softprops/action-gh-release@v2
161+
with:
162+
tag_name: ${{ needs.release-please.outputs.tag_name }}
163+
files: |
164+
target/${{ matrix.target }}/release/${{ matrix.binary-name }}-*.tar.gz
165+
target/${{ matrix.target }}/release/${{ matrix.binary-name }}-*.sha256
166+
env:
167+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
168+
169+
publish:
170+
name: Publish to crates.io 📦
171+
runs-on: ubuntu-latest
172+
needs: [release-please]
173+
if: needs.release-please.outputs.created
174+
steps:
175+
- uses: actions/checkout@v4
176+
- uses: swatinem/rust-cache@v2
177+
- name: Publish
178+
# https://doc.rust-lang.org/cargo/reference/config.html?highlight=CARGO_REGISTRY_TOKEN#credentials
179+
run: >
180+
cargo publish
181+
--verbose
182+
--locked
183+
--token ${{ secrets.CARGO_REGISTRY_TOKEN }}
184+

0 commit comments

Comments
 (0)