1+ name : " On Tag Deploy on GitHub"
2+ env :
3+ REPO_NAME : " minecraft_server_status"
4+ RUN_TESTS : true
5+ RUST_VERSION : " 1.80"
6+ on :
7+ push :
8+ tags :
9+ # - "[0-9]+.[0-9]+.[0-9]+"
10+ - " *" # execute every time tag is pushed
11+
12+
13+ jobs :
14+ datetime :
15+ name : " Get Current Datetime"
16+ env :
17+ working-directory : ${{github.workspace}}
18+ runs-on : " ubuntu-latest"
19+
20+ steps :
21+ - name : " NOW"
22+ id : " now"
23+ run : " echo \" NOW=$(date +'%Y-%m-%dT%H:%M:%S')\" >> $GITHUB_OUTPUT" # get datetime, save in NOW, push to output
24+
25+ - name : " TODAY"
26+ id : " today"
27+ run : " echo \" TODAY=$(date +'%Y-%m-%d')\" >> $GITHUB_OUTPUT" # get date, save in TODAY, push to output
28+
29+ outputs : # set step output as job output so other jobs can access
30+ NOW : ${{steps.now.outputs.NOW}}
31+ TODAY : ${{steps.today.outputs.TODAY}}
32+
33+
34+ test :
35+ name : " Run Tests"
36+ env :
37+ working-directory : ${{github.workspace}}
38+ runs-on : " ubuntu-latest"
39+
40+ steps :
41+ - name : " Checkout Repository"
42+ uses : " actions/checkout@v4" # makes repository structure available
43+
44+ - name : " Install Rust"
45+ uses : " actions-rust-lang/setup-rust-toolchain@v1"
46+ with :
47+ toolchain : ${{env.RUST_VERSION}}
48+
49+ - name : " Check Project Version and Tag Match"
50+ run : |
51+ project_version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
52+ tag=${GITHUB_REF#refs/tags/*}
53+ if [ "$project_version" == "$tag" ]; then
54+ exit 0
55+ else
56+ exit 1
57+ fi
58+
59+ - name : " Run Tests"
60+ if : ${{env.RUN_TESTS == 'true'}}
61+ run : " cargo test"
62+
63+
64+ create_release :
65+ name : " Create Release on GitHub"
66+ env :
67+ working-directory : ${{github.workspace}}
68+ needs : ["datetime", "test"]
69+ runs-on : " ubuntu-latest"
70+
71+ steps :
72+ - name : " Create Release"
73+ env :
74+ GITHUB_TOKEN : ${{secrets.GITHUB_TOKEN}}
75+ id : " create_release"
76+ uses : " actions/create-release@v1" # function that creates release
77+ with : # parameters
78+ body : # release text
79+ draft : false
80+ prerelease : false
81+ release_name : " ${{needs.datetime.outputs.TODAY}} ${{env.REPO_NAME}} ${{github.ref}}" # release title
82+ tag_name : ${{github.ref}} # release tag
83+
84+ outputs :
85+ github_release : ${{steps.create_release.outputs.upload_url}}
86+
87+
88+ build_executables :
89+ name : " Build Executable for ${{matrix.os}}"
90+ env :
91+ working-directory : ${{github.workspace}}
92+ runs-on : " ubuntu-latest"
93+ strategy :
94+ matrix :
95+ os : ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu"]
96+
97+ steps :
98+ - name : " Checkout Repository"
99+ uses : " actions/checkout@v4" # makes repository structure available
100+
101+ - name : " Install Rust"
102+ uses : " actions-rust-lang/setup-rust-toolchain@v1"
103+ with :
104+ target : ${{matrix.os}}
105+ toolchain : ${{env.RUST_VERSION}}
106+
107+ - name : " Install cross" # install cross for cross-compilation
108+ run : " cargo install cross"
109+
110+ - name : " Compile"
111+ run : " cross build --release --target ${{matrix.os}}"
112+
113+ - name : " Cache Executable"
114+ if : ${{matrix.os != 'x86_64-pc-windows-gnu'}}
115+ uses : " actions/cache/save@v4"
116+ with :
117+ key : ${{matrix.os}}
118+ path : " ./target/${{matrix.os}}/release/${{env.REPO_NAME}}"
119+
120+ - name : " Cache Executable"
121+ if : ${{matrix.os == 'x86_64-pc-windows-gnu'}}
122+ uses : " actions/cache/save@v4"
123+ with :
124+ key : ${{matrix.os}}
125+ path : " ./target/${{matrix.os}}/release/${{env.REPO_NAME}}.exe" # windows executable has .exe extension
126+
127+
128+ build_docker_image :
129+ name : " Build Docker Image"
130+ env :
131+ working-directory : ${{github.workspace}}
132+ runs-on : " ubuntu-latest"
133+
134+ steps :
135+ - name : " Checkout Repository"
136+ uses : " actions/checkout@v4" # makes repository structure available
137+
138+ - name : " Install Docker"
139+ uses : " docker/setup-buildx-action@v1"
140+
141+ - name : " Create \" ./target/\" Directory"
142+ run : " mkdir -p \" ./target/\" "
143+
144+ - name : " Compile"
145+ run : " IMAGE_NAME=\" 9-FS/${{env.REPO_NAME}}:latest\" && docker build -t \" ${IMAGE_NAME@L}\" --no-cache ."
146+
147+ - name : " Save Docker Image"
148+ run : " IMAGE_NAME=\" 9-FS/${{env.REPO_NAME}}:latest\" && docker save \" ${IMAGE_NAME@L}\" > \" ./target/docker-image.tar\" "
149+
150+ - name : " Cache Docker Image"
151+ uses : " actions/cache/save@v4"
152+ with :
153+ key : " docker"
154+ path : " ./target/docker-image.tar"
155+
156+
157+ deploy_executables :
158+ name : " Deploy Executable for ${{matrix.os}} on GitHub"
159+ env :
160+ working-directory : ${{github.workspace}}
161+ needs : ["build_executables", "create_release", "datetime", "test"]
162+ runs-on : " ubuntu-latest"
163+ strategy :
164+ matrix :
165+ os : ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu"]
166+
167+ steps :
168+ - name : " Load Executable"
169+ if : ${{matrix.os != 'x86_64-pc-windows-gnu'}}
170+ uses : " actions/cache/restore@v4"
171+ with :
172+ key : ${{matrix.os}}
173+ path : " ./target/${{matrix.os}}/release/${{env.REPO_NAME}}"
174+
175+ - name : " Load Executable"
176+ if : ${{matrix.os == 'x86_64-pc-windows-gnu'}}
177+ uses : " actions/cache/restore@v4"
178+ with :
179+ key : ${{matrix.os}}
180+ path : " ./target/${{matrix.os}}/release/${{env.REPO_NAME}}.exe"
181+
182+ - name : " Parse Tag"
183+ id : " parse_tag"
184+ run : " echo \" tag=${GITHUB_REF#refs/tags/*}\" >> $GITHUB_OUTPUT" # parse tag because github.ref provides tag as f"refs/tags/{tag}", in create_release it is parsed automatically idk
185+ shell : " bash" # must be bash even on windows, because command to apply value to variable works differently in powershell
186+
187+ - name : " Attach Executable to Release"
188+ if : ${{matrix.os != 'x86_64-pc-windows-gnu'}}
189+ env :
190+ GITHUB_TOKEN : ${{secrets.GITHUB_TOKEN}}
191+ uses : " actions/upload-release-asset@v1"
192+ with :
193+ asset_content_type : " application"
194+ asset_name : " ${{needs.datetime.outputs.TODAY}} ${{env.REPO_NAME}} ${{steps.parse_tag.outputs.tag}} ${{matrix.os}}"
195+ asset_path : " ./target/${{matrix.os}}/release/${{env.REPO_NAME}}"
196+ upload_url : ${{needs.create_release.outputs.github_release}}
197+
198+ - name : " Attach Executable to Release"
199+ if : ${{matrix.os == 'x86_64-pc-windows-gnu'}}
200+ env :
201+ GITHUB_TOKEN : ${{secrets.GITHUB_TOKEN}}
202+ uses : " actions/upload-release-asset@v1"
203+ with :
204+ asset_content_type : " application"
205+ asset_name : " ${{needs.datetime.outputs.TODAY}} ${{env.REPO_NAME}} ${{steps.parse_tag.outputs.tag}} ${{matrix.os}}.exe"
206+ asset_path : " ./target/${{matrix.os}}/release/${{env.REPO_NAME}}.exe"
207+ upload_url : ${{needs.create_release.outputs.github_release}}
208+
209+
210+ deploy_docker_image :
211+ name : " Deploy Docker Image on GitHub"
212+ env :
213+ working-directory : ${{github.workspace}}
214+ needs : ["build_docker_image", "create_release", "datetime", "test"]
215+ runs-on : " ubuntu-latest"
216+
217+ steps :
218+ - name : " Load Docker Image"
219+ uses : " actions/cache/restore@v4"
220+ with :
221+ key : " docker"
222+ path : " ./target/docker-image.tar"
223+
224+ - name : " Parse Tag"
225+ id : " parse_tag"
226+ run : " echo \" tag=${GITHUB_REF#refs/tags/*}\" >> $GITHUB_OUTPUT" # parse tag because github.ref provides tag as f"refs/tags/{tag}", in create_release it is parsed automatically idk
227+ shell : " bash" # must be bash even on windows, because command to apply value to variable works differently in powershell
228+
229+ - name : " Attach Docker Image to Release"
230+ env :
231+ GITHUB_TOKEN : ${{secrets.GITHUB_TOKEN}}
232+ uses : " actions/upload-release-asset@v1"
233+ with :
234+ asset_content_type : " application"
235+ asset_name : " ${{needs.datetime.outputs.TODAY}} ${{env.REPO_NAME}} ${{steps.parse_tag.outputs.tag}} docker.tar"
236+ asset_path : " ./target/docker-image.tar"
237+ upload_url : ${{needs.create_release.outputs.github_release}}
0 commit comments