CI: push-containers: build and push container with external toolchain
[openwrt/staging/nbd.git] / .github / workflows / push-containers.yml
1 name: Build and Push prebuilt tools container
2
3 on:
4 push:
5 paths:
6 - 'include/version.mk'
7 - 'tools/**'
8 - '.github/workflows/build-tools.yml'
9 - '.github/workflows/push-containers.yml'
10 - '.github/workflows/Dockerfile.tools'
11 - 'toolchain/**'
12 - '.github/workflows/build.yml'
13 - '.github/workflows/toolchain.yml'
14 - '.github/workflows/Dockerfile.toolchain'
15
16 permissions:
17 contents: read
18
19 concurrency:
20 group: ${{ github.workflow }}
21 cancel-in-progress: true
22
23 jobs:
24 determine-container-info:
25 name: Determine needed info to push containers
26 if: ${{ github.repository_owner == 'openwrt' }}
27 runs-on: ubuntu-latest
28 outputs:
29 owner-lc: ${{ steps.generate-owner-lc.outputs.owner-lc }}
30 container-tag: ${{ steps.determine-container-tag.outputs.container-tag }}
31
32 steps:
33 - name: Set lower case owner name
34 id: generate-owner-lc
35 env:
36 OWNER: ${{ github.repository_owner }}
37 run: |
38 echo "owner-lc=${OWNER,,}" >> "$GITHUB_OUTPUT"
39
40 # Per branch tools container tag
41 # By default stick to latest
42 # For official test targetting openwrt stable branch
43 # Get the branch or parse the tag and push dedicated tools containers
44 # Any branch that will match this pattern openwrt-[0-9][0-9].[0-9][0-9]
45 # will refresh the tools container with the matching tag.
46 # (example branch openwrt-22.03 -> tools:openwrt-22.03)
47 # (example branch openwrt-22.03-test -> tools:openwrt-22.03)
48 - name: Determine tools container tag
49 id: determine-container-tag
50 run: |
51 CONTAINER_TAG=latest
52
53 if [ ${{ github.ref_type }} == "branch" ]; then
54 if echo "${{ github.ref_name }}" | grep -q -E 'openwrt-[0-9][0-9]\.[0-9][0-9]'; then
55 CONTAINER_TAG="$(echo ${{ github.ref_name }} | sed 's/^\(openwrt-[0-9][0-9]\.[0-9][0-9]\).*/\1/')"
56 fi
57 elif [ ${{ github.ref_type }} == "tag" ]; then
58 if echo "${{ github.ref_name }}" | grep -q -E 'v[0-9][0-9]\.[0-9][0-9]\..+'; then
59 CONTAINER_TAG=openwrt-"$(echo ${{ github.ref_name }} | sed 's/v\([0-9][0-9]\.[0-9][0-9]\)\..\+/\1/')"
60 fi
61 fi
62
63 echo "Container tag to push for tools and toolchain is $CONTAINER_TAG"
64 echo "container-tag=$CONTAINER_TAG" >> "$GITHUB_OUTPUT"
65
66 build-linux-buildbot:
67 name: Build tools with buildbot container
68 if: ${{ github.repository_owner == 'openwrt' }}
69 uses: ./.github/workflows/build-tools.yml
70 with:
71 generate_prebuilt_artifacts: true
72
73 push-tools-container:
74 needs: [ determine-container-info, build-linux-buildbot ]
75 if: ${{ github.repository_owner == 'openwrt' }}
76 name: Push prebuilt tools container
77 runs-on: ubuntu-latest
78
79 permissions:
80 contents: read
81 packages: write
82
83 steps:
84 - name: Checkout
85 uses: actions/checkout@v3
86 with:
87 path: 'openwrt'
88
89 - name: Download prebuilt tools from build job
90 uses: actions/download-artifact@v3
91 with:
92 name: linux-buildbot-prebuilt-tools
93 path: openwrt
94
95 - name: Extract prebuild tools
96 working-directory: openwrt
97 run: tar -xf tools.tar
98
99 - name: Login to GitHub Container Registry
100 uses: docker/login-action@v2
101 with:
102 registry: ghcr.io
103 username: ${{ github.actor }}
104 password: ${{ secrets.GITHUB_TOKEN }}
105
106 - name: Build and push
107 uses: docker/build-push-action@v3
108 with:
109 context: openwrt
110 push: true
111 tags: ghcr.io/${{ needs.determine-container-info.outputs.owner-lc }}/tools:${{ needs.determine-container-info.outputs.container-tag }}
112 file: openwrt/.github/workflows/Dockerfile.tools
113
114 determine-targets:
115 name: Set targets
116 if: ${{ github.repository_owner == 'openwrt' }}
117 runs-on: ubuntu-latest
118 outputs:
119 target: ${{ steps.find_targets.outputs.target }}
120
121 steps:
122 - name: Checkout
123 uses: actions/checkout@v3
124
125 - name: Set targets
126 id: find_targets
127 run: |
128 export TARGETS="$(perl ./scripts/dump-target-info.pl targets 2>/dev/null \
129 | awk '{ print $1 }')"
130
131 JSON='['
132 FIRST=1
133 for TARGET in $TARGETS; do
134 TUPLE='{"target":"'"$(echo $TARGET | cut -d "/" -f 1)"'","subtarget":"'"$(echo $TARGET | cut -d "/" -f 2)"'"}'
135 [[ $FIRST -ne 1 ]] && JSON="$JSON"','
136 JSON="$JSON""$TUPLE"
137 FIRST=0
138 done
139 JSON="$JSON"']'
140
141 echo -e "\n---- targets ----\n"
142 echo "$JSON"
143 echo -e "\n---- targets ----\n"
144
145 echo "target=$JSON" >> $GITHUB_OUTPUT
146
147 build:
148 name: Build Target Toolchain
149 if: ${{ github.repository_owner == 'openwrt' }}
150 needs: [ determine-targets, push-tools-container ]
151 permissions:
152 contents: read
153 packages: read
154 strategy:
155 fail-fast: False
156 matrix:
157 include: ${{fromJson(needs.determine-targets.outputs.target)}}
158 uses: ./.github/workflows/build.yml
159 with:
160 target: ${{ matrix.target }}
161 subtarget: ${{ matrix.subtarget }}
162 build_toolchain: true
163 build_external_toolchain: true
164 upload_external_toolchain: true
165
166 push-toolchain-container:
167 name: Push Target Toolchain container
168 if: ${{ github.repository_owner == 'openwrt' }}
169 needs: [ determine-container-info, determine-targets, build ]
170 runs-on: ubuntu-latest
171
172 strategy:
173 fail-fast: False
174 matrix:
175 include: ${{fromJson(needs.determine-targets.outputs.target)}}
176
177 permissions:
178 contents: read
179 packages: write
180
181 steps:
182 - name: Checkout
183 uses: actions/checkout@v3
184 with:
185 path: 'openwrt'
186
187 - name: Download external toolchain from build job
188 uses: actions/download-artifact@v3
189 with:
190 name: ${{ matrix.target }}-${{ matrix.subtarget }}-external-toolchain
191 path: openwrt
192
193 - name: Find external toolchain name
194 id: get-toolchain-name
195 working-directory: openwrt
196 run: |
197 TOOLCHAIN_NAME=$(ls | grep toolchain-${{ matrix.target }}-${{ matrix.subtarget }})
198 echo "toolchain-name=$TOOLCHAIN_NAME" >> $GITHUB_OUTPUT
199
200 - name: Login to GitHub Container Registry
201 uses: docker/login-action@v2
202 with:
203 registry: ghcr.io
204 username: ${{ github.actor }}
205 password: ${{ secrets.GITHUB_TOKEN }}
206
207 - name: Build and push
208 uses: docker/build-push-action@v3
209 with:
210 context: openwrt
211 push: true
212 tags: ghcr.io/${{ needs.determine-container-info.outputs.owner-lc }}/toolchain:${{ matrix.target }}-${{ matrix.subtarget }}-${{ needs.determine-container-info.outputs.container-tag }}
213 file: openwrt/.github/workflows/Dockerfile.toolchain
214 build-args: |
215 OWNER_LC=${{ needs.determine-container-info.outputs.owner-lc }}
216 CONTAINER_TAG=${{ needs.determine-container-info.outputs.container-tag }}
217 TOOLCHAIN_NAME=${{ steps.get-toolchain-name.outputs.toolchain-name }}