Release 2024.10.22
[yt-dlp.git] / .github / workflows / release.yml
blob8d0bc4026a1b45ab8bc77017dc5e3d853c5f824d
1 name: Release
2 on:
3   workflow_call:
4     inputs:
5       prerelease:
6         required: false
7         default: true
8         type: boolean
9       source:
10         required: false
11         default: ''
12         type: string
13       target:
14         required: false
15         default: ''
16         type: string
17       version:
18         required: false
19         default: ''
20         type: string
21   workflow_dispatch:
22     inputs:
23       source:
24         description: |
25           SOURCE of this release's updates:
26           channel, repo, tag, or channel/repo@tag
27           (default: <current_repo>)
28         required: false
29         default: ''
30         type: string
31       target:
32         description: |
33           TARGET to publish this release to:
34           channel, tag, or channel@tag
35           (default: <source> if writable else <current_repo>[@source_tag])
36         required: false
37         default: ''
38         type: string
39       version:
40         description: |
41           VERSION: yyyy.mm.dd[.rev] or rev
42           (default: auto-generated)
43         required: false
44         default: ''
45         type: string
46       prerelease:
47         description: Pre-release
48         default: false
49         type: boolean
51 permissions:
52   contents: read
54 jobs:
55   prepare:
56     permissions:
57       contents: write
58     runs-on: ubuntu-latest
59     outputs:
60       channel: ${{ steps.setup_variables.outputs.channel }}
61       version: ${{ steps.setup_variables.outputs.version }}
62       target_repo: ${{ steps.setup_variables.outputs.target_repo }}
63       target_repo_token: ${{ steps.setup_variables.outputs.target_repo_token }}
64       target_tag: ${{ steps.setup_variables.outputs.target_tag }}
65       pypi_project: ${{ steps.setup_variables.outputs.pypi_project }}
66       pypi_suffix: ${{ steps.setup_variables.outputs.pypi_suffix }}
67       head_sha: ${{ steps.get_target.outputs.head_sha }}
69     steps:
70       - uses: actions/checkout@v4
71         with:
72           fetch-depth: 0
74       - uses: actions/setup-python@v5
75         with:
76           python-version: "3.10"
78       - name: Process inputs
79         id: process_inputs
80         run: |
81           cat << EOF
82           ::group::Inputs
83           prerelease=${{ inputs.prerelease }}
84           source=${{ inputs.source }}
85           target=${{ inputs.target }}
86           version=${{ inputs.version }}
87           ::endgroup::
88           EOF
89           IFS='@' read -r source_repo source_tag <<<"${{ inputs.source }}"
90           IFS='@' read -r target_repo target_tag <<<"${{ inputs.target }}"
91           cat << EOF >> "$GITHUB_OUTPUT"
92           source_repo=${source_repo}
93           source_tag=${source_tag}
94           target_repo=${target_repo}
95           target_tag=${target_tag}
96           EOF
98       - name: Setup variables
99         id: setup_variables
100         env:
101           source_repo: ${{ steps.process_inputs.outputs.source_repo }}
102           source_tag: ${{ steps.process_inputs.outputs.source_tag }}
103           target_repo: ${{ steps.process_inputs.outputs.target_repo }}
104           target_tag: ${{ steps.process_inputs.outputs.target_tag }}
105         run: |
106           # unholy bash monstrosity (sincere apologies)
107           fallback_token () {
108             if ${{ !secrets.ARCHIVE_REPO_TOKEN }}; then
109               echo "::error::Repository access secret ${target_repo_token^^} not found"
110               exit 1
111             fi
112             target_repo_token=ARCHIVE_REPO_TOKEN
113             return 0
114           }
116           source_is_channel=0
117           [[ "${source_repo}" == 'stable' ]] && source_repo='yt-dlp/yt-dlp'
118           if [[ -z "${source_repo}" ]]; then
119             source_repo='${{ github.repository }}'
120           elif [[ '${{ vars[format('{0}_archive_repo', env.source_repo)] }}' ]]; then
121             source_is_channel=1
122             source_channel='${{ vars[format('{0}_archive_repo', env.source_repo)] }}'
123           elif [[ -z "${source_tag}" && "${source_repo}" != */* ]]; then
124             source_tag="${source_repo}"
125             source_repo='${{ github.repository }}'
126           fi
127           resolved_source="${source_repo}"
128           if [[ "${source_tag}" ]]; then
129             resolved_source="${resolved_source}@${source_tag}"
130           elif [[ "${source_repo}" == 'yt-dlp/yt-dlp' ]]; then
131             resolved_source='stable'
132           fi
134           revision="${{ (inputs.prerelease || !vars.PUSH_VERSION_COMMIT) && '$(date -u +"%H%M%S")' || '' }}"
135           version="$(
136             python devscripts/update-version.py \
137             -c "${resolved_source}" -r "${{ github.repository }}" ${{ inputs.version || '$revision' }} | \
138             grep -Po "version=\K\d+\.\d+\.\d+(\.\d+)?")"
140           if [[ "${target_repo}" ]]; then
141             if [[ -z "${target_tag}" ]]; then
142               if [[ '${{ vars[format('{0}_archive_repo', env.target_repo)] }}' ]]; then
143                 target_tag="${source_tag:-${version}}"
144               else
145                 target_tag="${target_repo}"
146                 target_repo='${{ github.repository }}'
147               fi
148             fi
149             if [[ "${target_repo}" != '${{ github.repository}}' ]]; then
150               target_repo='${{ vars[format('{0}_archive_repo', env.target_repo)] }}'
151               target_repo_token='${{ env.target_repo }}_archive_repo_token'
152               ${{ !!secrets[format('{0}_archive_repo_token', env.target_repo)] }} || fallback_token
153               pypi_project='${{ vars[format('{0}_pypi_project', env.target_repo)] }}'
154               pypi_suffix='${{ vars[format('{0}_pypi_suffix', env.target_repo)] }}'
155             fi
156           else
157             target_tag="${source_tag:-${version}}"
158             if ((source_is_channel)); then
159               target_repo="${source_channel}"
160               target_repo_token='${{ env.source_repo }}_archive_repo_token'
161               ${{ !!secrets[format('{0}_archive_repo_token', env.source_repo)] }} || fallback_token
162               pypi_project='${{ vars[format('{0}_pypi_project', env.source_repo)] }}'
163               pypi_suffix='${{ vars[format('{0}_pypi_suffix', env.source_repo)] }}'
164             else
165               target_repo='${{ github.repository }}'
166             fi
167           fi
169           if [[ "${target_repo}" == '${{ github.repository }}' ]] && ${{ !inputs.prerelease }}; then
170             pypi_project='${{ vars.PYPI_PROJECT }}'
171           fi
173           echo "::group::Output variables"
174           cat << EOF | tee -a "$GITHUB_OUTPUT"
175           channel=${resolved_source}
176           version=${version}
177           target_repo=${target_repo}
178           target_repo_token=${target_repo_token}
179           target_tag=${target_tag}
180           pypi_project=${pypi_project}
181           pypi_suffix=${pypi_suffix}
182           EOF
183           echo "::endgroup::"
185       - name: Update documentation
186         env:
187           version: ${{ steps.setup_variables.outputs.version }}
188           target_repo: ${{ steps.setup_variables.outputs.target_repo }}
189         if: |
190           !inputs.prerelease && env.target_repo == github.repository
191         run: |
192           python devscripts/update_changelog.py -vv
193           make doc
195       - name: Push to release
196         id: push_release
197         env:
198           version: ${{ steps.setup_variables.outputs.version }}
199           target_repo: ${{ steps.setup_variables.outputs.target_repo }}
200         if: |
201           !inputs.prerelease && env.target_repo == github.repository
202         run: |
203           git config --global user.name "github-actions[bot]"
204           git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
205           git add -u
206           git commit -m "Release ${{ env.version }}" \
207             -m "Created by: ${{ github.event.sender.login }}" -m ":ci skip all"
208           git push origin --force ${{ github.event.ref }}:release
210       - name: Get target commitish
211         id: get_target
212         run: |
213           echo "head_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
215       - name: Update master
216         env:
217           target_repo: ${{ steps.setup_variables.outputs.target_repo }}
218         if: |
219           vars.PUSH_VERSION_COMMIT != '' && !inputs.prerelease && env.target_repo == github.repository
220         run: git push origin ${{ github.event.ref }}
222   build:
223     needs: prepare
224     uses: ./.github/workflows/build.yml
225     with:
226       version: ${{ needs.prepare.outputs.version }}
227       channel: ${{ needs.prepare.outputs.channel }}
228       origin: ${{ needs.prepare.outputs.target_repo }}
229     permissions:
230       contents: read
231       packages: write  # For package cache
232       actions: write  # For cleaning up cache
233     secrets:
234       GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }}
236   publish_pypi:
237     needs: [prepare, build]
238     if: ${{ needs.prepare.outputs.pypi_project }}
239     runs-on: ubuntu-latest
240     permissions:
241       id-token: write  # mandatory for trusted publishing
243     steps:
244       - uses: actions/checkout@v4
245         with:
246           fetch-depth: 0
247       - uses: actions/setup-python@v5
248         with:
249           python-version: "3.10"
251       - name: Install Requirements
252         run: |
253           sudo apt -y install pandoc man
254           python devscripts/install_deps.py -o --include build
256       - name: Prepare
257         env:
258           version: ${{ needs.prepare.outputs.version }}
259           suffix: ${{ needs.prepare.outputs.pypi_suffix }}
260           channel: ${{ needs.prepare.outputs.channel }}
261           target_repo: ${{ needs.prepare.outputs.target_repo }}
262           pypi_project: ${{ needs.prepare.outputs.pypi_project }}
263         run: |
264           python devscripts/update-version.py -c "${{ env.channel }}" -r "${{ env.target_repo }}" -s "${{ env.suffix }}" "${{ env.version }}"
265           python devscripts/update_changelog.py -vv
266           python devscripts/make_lazy_extractors.py
267           sed -i -E '0,/(name = ")[^"]+(")/s//\1${{ env.pypi_project }}\2/' pyproject.toml
269       - name: Build
270         run: |
271           rm -rf dist/*
272           make pypi-files
273           printf '%s\n\n' \
274             'Official repository: <https://github.com/yt-dlp/yt-dlp>' \
275             '**PS**: Some links in this document will not work since this is a copy of the README.md from Github' > ./README.md.new
276           cat ./README.md >> ./README.md.new && mv -f ./README.md.new ./README.md
277           python devscripts/set-variant.py pip -M "You installed yt-dlp with pip or using the wheel from PyPi; Use that to update"
278           make clean-cache
279           python -m build --no-isolation .
281       - name: Publish to PyPI
282         uses: pypa/gh-action-pypi-publish@release/v1
283         with:
284           verbose: true
286   publish:
287     needs: [prepare, build]
288     permissions:
289       contents: write
290     runs-on: ubuntu-latest
292     steps:
293       - uses: actions/checkout@v4
294         with:
295           fetch-depth: 0
296       - uses: actions/download-artifact@v4
297         with:
298           path: artifact
299           pattern: build-*
300           merge-multiple: true
301       - uses: actions/setup-python@v5
302         with:
303           python-version: "3.10"
305       - name: Generate release notes
306         env:
307           head_sha: ${{ needs.prepare.outputs.head_sha }}
308           target_repo: ${{ needs.prepare.outputs.target_repo }}
309           target_tag: ${{ needs.prepare.outputs.target_tag }}
310         run: |
311           printf '%s' \
312             '[![Installation](https://img.shields.io/badge/-Which%20file%20to%20download%3F-white.svg?style=for-the-badge)]' \
313               '(https://github.com/${{ github.repository }}#installation "Installation instructions") ' \
314             '[![Discord](https://img.shields.io/discord/807245652072857610?color=blue&labelColor=555555&label=&logo=discord&style=for-the-badge)]' \
315               '(https://discord.gg/H5MNcFW63r "Discord") ' \
316             '[![Donate](https://img.shields.io/badge/_-Donate-red.svg?logo=githubsponsors&labelColor=555555&style=for-the-badge)]' \
317               '(https://github.com/yt-dlp/yt-dlp/blob/master/Collaborators.md#collaborators "Donate") ' \
318             '[![Documentation](https://img.shields.io/badge/-Docs-brightgreen.svg?style=for-the-badge&logo=GitBook&labelColor=555555)]' \
319               '(https://github.com/${{ github.repository }}' \
320               '${{ env.target_repo == github.repository && format('/tree/{0}', env.target_tag) || '' }}#readme "Documentation") ' \
321             ${{ env.target_repo == 'yt-dlp/yt-dlp' && '\
322               "[![Nightly](https://img.shields.io/badge/Nightly%20builds-purple.svg?style=for-the-badge)]" \
323               "(https://github.com/yt-dlp/yt-dlp-nightly-builds/releases/latest \"Nightly builds\") " \
324               "[![Master](https://img.shields.io/badge/Master%20builds-lightblue.svg?style=for-the-badge)]" \
325               "(https://github.com/yt-dlp/yt-dlp-master-builds/releases/latest \"Master builds\")"' || '' }} > ./RELEASE_NOTES
326           printf '\n\n' >> ./RELEASE_NOTES
327           cat >> ./RELEASE_NOTES << EOF
328           #### A description of the various files is in the [README](https://github.com/${{ github.repository }}#release-files)
329           ---
330           $(python ./devscripts/make_changelog.py -vv --collapsible)
331           EOF
332           printf '%s\n\n' '**This is a pre-release build**' >> ./PRERELEASE_NOTES
333           cat ./RELEASE_NOTES >> ./PRERELEASE_NOTES
334           printf '%s\n\n' 'Generated from: https://github.com/${{ github.repository }}/commit/${{ env.head_sha }}' >> ./ARCHIVE_NOTES
335           cat ./RELEASE_NOTES >> ./ARCHIVE_NOTES
337       - name: Publish to archive repo
338         env:
339           GH_TOKEN: ${{ secrets[needs.prepare.outputs.target_repo_token] }}
340           GH_REPO: ${{ needs.prepare.outputs.target_repo }}
341           version: ${{ needs.prepare.outputs.version }}
342           channel: ${{ needs.prepare.outputs.channel }}
343         if: |
344           inputs.prerelease && env.GH_TOKEN != '' && env.GH_REPO != '' && env.GH_REPO != github.repository
345         run: |
346           title="${{ startswith(env.GH_REPO, 'yt-dlp/') && 'yt-dlp ' || '' }}${{ env.channel }}"
347           gh release create \
348             --notes-file ARCHIVE_NOTES \
349             --title "${title} ${{ env.version }}" \
350             ${{ env.version }} \
351             artifact/*
353       - name: Prune old release
354         env:
355           GH_TOKEN: ${{ github.token }}
356           version: ${{ needs.prepare.outputs.version }}
357           target_repo: ${{ needs.prepare.outputs.target_repo }}
358           target_tag: ${{ needs.prepare.outputs.target_tag }}
359         if: |
360           env.target_repo == github.repository && env.target_tag != env.version
361         run: |
362           gh release delete --yes --cleanup-tag "${{ env.target_tag }}" || true
363           git tag --delete "${{ env.target_tag }}" || true
364           sleep 5  # Enough time to cover deletion race condition
366       - name: Publish release
367         env:
368           GH_TOKEN: ${{ github.token }}
369           version: ${{ needs.prepare.outputs.version }}
370           target_repo: ${{ needs.prepare.outputs.target_repo }}
371           target_tag: ${{ needs.prepare.outputs.target_tag }}
372           head_sha: ${{ needs.prepare.outputs.head_sha }}
373         if: |
374           env.target_repo == github.repository
375         run: |
376           title="${{ github.repository == 'yt-dlp/yt-dlp' && 'yt-dlp ' || '' }}"
377           title+="${{ env.target_tag != env.version && format('{0} ', env.target_tag) || '' }}"
378           gh release create \
379             --notes-file ${{ inputs.prerelease && 'PRERELEASE_NOTES' || 'RELEASE_NOTES' }} \
380             --target ${{ env.head_sha }} \
381             --title "${title}${{ env.version }}" \
382             ${{ inputs.prerelease && '--prerelease' || '' }} \
383             ${{ env.target_tag }} \
384             artifact/*