-
Notifications
You must be signed in to change notification settings - Fork 5
220 lines (188 loc) · 9.44 KB
/
build_push_image.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
209
210
211
212
213
214
215
216
217
218
219
220
name: build_push_image
run-name: B&D ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:${{ github.event.inputs.tags }}
on:
workflow_dispatch:
inputs:
build_folder:
description: 'From which folder, the Dockerfiles will be built'
type: string
required: false
default: 'cosmos_openwebui'
tags:
description: 'Customize image tags'
type: string
required: false
default: 'latest'
hub_username:
description: 'Docker hub username'
type: string
required: false
default: 'brokenjade'
zip_vol_size:
description: 'Max 7zip volumn size'
type: string
required: false
default: '800m'
jobs:
build_docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: buildx-${{ github.event.inputs.build_folder }}
restore-keys: |
buildx-${{ github.event.inputs.build_folder }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
if: ${{ github.event.inputs.hub_username != 'false' }}
uses: docker/login-action@v3
with:
username: ${{ github.event.inputs.hub_username }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Determine tag name
id: tag
shell: bash
run: |
tags=${{ github.event.inputs.tags }}
IFS=',' read -ra TAG_ARRAY <<< "$tags"
echo "tag0=${TAG_ARRAY[0]}" >> $GITHUB_OUTPUT
BUILD_NUMBER="$(git rev-list --count HEAD)"
SHORT_HASH="$(git rev-parse --short=7 HEAD)"
echo "name=Docker_${{ github.event.inputs.build_folder }}-${TAG_ARRAY[0]}-b${BUILD_NUMBER}-${SHORT_HASH}" >> $GITHUB_OUTPUT
echo "name=Docker_${{ github.event.inputs.build_folder }}-${TAG_ARRAY[0]}-b${BUILD_NUMBER}-${SHORT_HASH}"
- name: Build
run: |
pwd
cd ${{ github.event.inputs.build_folder }}
ls
cat Dockerfile
docker build -t ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:temp .
tags=${{ github.event.inputs.tags }}
IFS=',' read -ra TAG_ARRAY <<< "$tags"
for tag in "${TAG_ARRAY[@]}"; do
docker tag ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:temp ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:$tag
done
- name: list images
run: docker images
- name: publish
if: ${{ github.event.inputs.hub_username != 'false' }}
run: |
tags=${{ github.event.inputs.tags }}
IFS=',' read -ra TAG_ARRAY <<< "$tags"
for tag in "${TAG_ARRAY[@]}"; do
echo ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:$tag
docker image push ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:$tag
done
- name: Update Docker Hub Description
if: ${{ github.event.inputs.hub_username != 'false' }}
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ github.event.inputs.hub_username }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}
readme-filepath: "${{ github.event.inputs.build_folder }}/README.MD"
# only works if built on self-hosted runners. Github runners do not have enough space for this.
# - name: Save image as a tar for later use 💾
# run: docker save ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:${{ github.event.inputs.tag }} -o /tmp/${{ github.event.inputs.hub_username }}-${{ github.event.inputs.build_folder }}.tar
# shell: bash
# - name: Upload image as artifact 💾
# uses: actions/upload-artifact@v3
# with:
# name: ${{ github.event.inputs.hub_username }}-${{ github.event.inputs.build_folder }}
# path: /tmp/${{ github.event.inputs.hub_username }}-${{ github.event.inputs.build_folder }}.tar
# retention-days: 3
- name: Save image as a tar for later use
run: |
# Export the image to a tar file
mkdir -p artifacts
echo "docker save ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:${{ steps.tag.outputs.tag0 }} -o artifacts/docker-${{ github.event.inputs.build_folder }}.tar"
docker save ${{ github.event.inputs.hub_username }}/${{ github.event.inputs.build_folder }}:${{ steps.tag.outputs.tag0 }} -o artifacts/docker-${{ github.event.inputs.build_folder }}.tar
- name: Install 7zip
run: sudo apt-get install -y p7zip-full
- name: Split Docker image into volumes
run: |
cd artifacts
mkdir zipped
7z a -t7z -v${{ github.event.inputs.zip_vol_size }} zipped/docker-${{ github.event.inputs.build_folder }}-volumes.7z docker-${{ github.event.inputs.build_folder }}.tar
- name: check zipped
run: |
pwd
ls artifacts/zipped
- name: Upload compressed parts as artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ github.event.inputs.build_folder }}
path: artifacts/zipped/*.7z.*
retention-days: ${{ github.event.inputs.retention_days }}
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag.outputs.name }}
release_name: ${{ steps.tag.outputs.name }}
draft: false
prerelease: false
- name: Upload Release Assets
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.event.inputs.build_folder }}-${{ github.event.inputs.tags }}
github-token: ${{secrets.GITHUB_TOKEN}}
run: pwd
script: |
const path = require('path');
const fs = require('fs');
// Retrieve necessary information
const release_id = `${{ steps.create_release.outputs.id }}`;
const release_url = `${{ steps.create_release.outputs.html_url }}`;
const buildFolder = `${{ github.event.inputs.build_folder }}`;
const descriptionFilePath = `${buildFolder}/README.MD`;
let releaseDescription = '';
const core = require('@actions/core');
core.info(`Current working directory: ${process.cwd()}`);
// Check for README file and load its content
if (fs.existsSync(descriptionFilePath)) {
releaseDescription = fs.readFileSync(descriptionFilePath, 'utf8');
console.log("Successfully read release description from 'README.MD'");
} else {
console.warn("README.MD not found in the specified build folder. Proceeding without a detailed description.");
}
// Construct the environment.yml GitHub file URL manually
const repoOwner = context.repo.owner;
const repoName = context.repo.repo;
const filePath = `${buildFolder}/Dockerfile`;
const fileUrl = `https://github.com/${repoOwner}/${repoName}/blob/${{ steps.tag.outputs.name }}/${filePath}`;
console.log(`Constructed file URL: ${fileUrl}`);
// Append the file URL to the release description
releaseDescription += `\n\nView the corresponding [Dockerfile](${fileUrl}) file used for this release.`;
releaseDescription += `\n\nTo use it, unzip the folders inside and put them under ${{ github.event.inputs.target_folder }}`;
releaseDescription += `\n\nThis environment was built with following workflow settings: `;
releaseDescription += `\nbuild_folder: ${{ github.event.inputs.build_folder }}`;
releaseDescription += `\ntags: ${{ github.event.inputs.tags }}`;
releaseDescription += `\nhub_username: ${{ github.event.inputs.hub_username }}`;
releaseDescription += `\nzip_vol_size: ${{ github.event.inputs.zip_vol_size }}`;
// Update the release with the description
await github.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id,
body: releaseDescription,
});
for (let file of await fs.readdirSync('/home/runner/work/dl_dockers/dl_dockers/${{ github.event.inputs.build_folder }}/${{ github.event.inputs.build_folder }}/artifacts/zipped')) {
console.log('uploadReleaseAsset', file);
await github.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release_id,
name: file,
data: fs.createReadStream(`/home/runner/work/dl_dockers/dl_dockers/${{ github.event.inputs.build_folder }}/${{ github.event.inputs.build_folder }}/artifacts/zipped/${file}`)
});
}