Codechange: change DestinationID into class with conversion helpers
[openttd-github.git] / .github / workflows / release-source.yml
blobc2d414266b2b4b11f081d97962ac4e2db72cab1c
1 name: Release (Source)
3 on:
4   workflow_call:
5     outputs:
6       version:
7         value: ${{ jobs.source.outputs.version }}
8       is_tag:
9         value: ${{ jobs.source.outputs.is_tag }}
10       trigger_type:
11         value: ${{ jobs.source.outputs.trigger_type }}
12       folder:
13         value: ${{ jobs.source.outputs.folder }}
14       survey_key:
15         value: ${{ jobs.source.outputs.survey_key }}
17 jobs:
18   source:
19     name: Source
21     runs-on: ubuntu-latest
23     outputs:
24       version: ${{ steps.metadata.outputs.version }}
25       is_tag: ${{ steps.metadata.outputs.is_tag }}
26       trigger_type: ${{ steps.metadata.outputs.trigger_type }}
27       folder: ${{ steps.metadata.outputs.folder }}
28       survey_key: ${{ steps.survey_key.outputs.survey_key }}
30     steps:
31     - name: Checkout (Release)
32       if: github.event_name == 'release'
33       uses: actions/checkout@v4
34       with:
35         # We generate a changelog; for this we need the full git log.
36         fetch-depth: 0
38     - name: Checkout (Manual)
39       if: github.event_name == 'workflow_dispatch'
40       uses: actions/checkout@v4
41       with:
42         ref: ${{ github.event.inputs.ref }}
43         # We generate a changelog; for this we need the full git log.
44         fetch-depth: 0
46     - name: Checkout (Trigger)
47       if: github.event_name == 'repository_dispatch'
48       uses: actions/checkout@v4
49       with:
50         ref: ${{ github.event.client_payload.ref }}
51         # We generate a changelog; for this we need the full git log.
52         fetch-depth: 0
54     - name: Check valid branch name
55       run: |
56         if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
57           REF="${{ github.event.inputs.ref }}"
58         elif [ "${{ github.event_name }}" = "repository_dispatch" ]; then
59           REF="${{ github.event.client_payload.ref }}"
60         else
61           REF="${{ github.ref }}"
62         fi
64         # Check if we are a tag.
65         if [ -n "$(git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || false)" ]; then
66           exit 0
67         fi
69         # Check if the checkout caused the branch to be named.
70         if [ "$(git rev-parse --abbrev-ref HEAD)" != "HEAD" ]; then
71           exit 0
72         fi
74         # Check if this was a pull request.
75         if [ -n "$(echo ${REF} | grep '^refs/pull/[0-9]*')" ]; then
76           PULL=$(echo ${REF} | cut -d/ -f3)
77           git checkout -b pr${PULL}
78         fi
80         # Are we still in a detached state? Error out.
81         if [ "$(git rev-parse --abbrev-ref HEAD)" == "HEAD" ]; then
82           echo "The 'ref' given resulted in a checkout of a detached HEAD."
83           echo "We cannot detect the version for these checkout accurate."
84           echo ""
85           echo "If you want to build a Pull Request, make sure you use 'refs/pull/NNN/head'."
86           echo ""
87           echo "Cancelling build, as without a version we cannot store the artifacts."
88           exit 1
89         fi
91     - name: Generate metadata
92       id: metadata
93       shell: bash
94       run: |
95         echo "::group::Prepare metadata files"
96         cmake -DGENERATE_OTTDREV=1 -P cmake/scripts/FindVersion.cmake
97         TZ='UTC' date +"%Y-%m-%d %H:%M UTC" > .release_date
98         cat .ottdrev | cut -f 1 -d$'\t' > .version
100         if [ $(cat .ottdrev | cut -f 5 -d$'\t') = '1' ]; then
101           # Assume that all tags are always releases. Why else make a tag?
102           IS_TAG="true"
104           FOLDER="${{ env.FOLDER_RELEASES }}"
105           TRIGGER_TYPE="new-tag"
107           python3 ./.github/changelog.py "$(cat .version)" > .changelog
108         else
109           IS_TAG="false"
111           BRANCH=$(git symbolic-ref -q HEAD | sed 's@.*/@@')
112           if [ -z "${BRANCH}" ]; then
113             echo "Internal error: branch name is empty."
114             echo "An earlier step should have prevented this from happening."
115             echo "Cancelling build, as without a branch name we cannot store the artifacts"
116             exit 1
117           fi
119           if [ "${BRANCH}" = "${{ env.NIGHTLIES_BRANCH }}" ]; then
120             # The "master" branch is special, and we call a nightly.
121             FOLDER="${{ env.FOLDER_NIGHTLIES }}/$(date +%Y)"
122             TRIGGER_TYPE="new-master"
123           else
124             # All other branches, which can be builds of Pull Requests, are
125             # put in their own folder.
126             FOLDER="${{ env.FOLDER_BRANCHES }}/${BRANCH}"
127             TRIGGER_TYPE="new-branch"
128           fi
130           # For nightlies / branches, use the git log of the last 7 days as changelog.
131           revdate=$(git log -1 --pretty=format:"%ci")
132           last_week=$(date -d "$revdate -7days" +"%Y-%m-%d %H:%M")
133           echo "## Version $(cat .version) - changes since ${last_week}" > .changelog
134           echo "" >> .changelog
135           git log --oneline --after="${last_week}" >> .changelog
136         fi
138         mkdir -p build/bundles
139         cp .changelog build/bundles/changelog.md
140         cp .release_date build/bundles/released.txt
141         cp README.md build/bundles/README.md
142         echo "::endgroup::"
144         echo "Release Date: $(cat .release_date)"
145         echo "Revision: $(cat .ottdrev)"
146         echo "Version: $(cat .version)"
147         echo "Is tag: ${IS_TAG}"
148         echo "Folder on CDN: ${FOLDER}"
149         echo "Workflow trigger: ${TRIGGER_TYPE}"
151         echo "version=$(cat .version)" >> $GITHUB_OUTPUT
152         echo "is_tag=${IS_TAG}" >> $GITHUB_OUTPUT
153         echo "folder=${FOLDER}" >> $GITHUB_OUTPUT
154         echo "trigger_type=${TRIGGER_TYPE}" >> $GITHUB_OUTPUT
155       env:
156         NIGHTLIES_BRANCH: master
157         FOLDER_RELEASES: openttd-releases
158         FOLDER_NIGHTLIES: openttd-nightlies
159         FOLDER_BRANCHES: openttd-branches
161     - name: Generate survey key
162       id: survey_key
163       run: |
164         if [ -z "${{ vars.SURVEY_TYPE }}" ]; then
165           echo "SURVEY_TYPE variable not found; most likely running in a fork. Skipping step."
166           SURVEY_KEY=""
167         else
168           PAYLOAD='{"version":"${{ steps.metadata.outputs.version }}","type":"${{ vars.SURVEY_TYPE }}"}'
170           echo "${{ secrets.SURVEY_SIGNING_KEY }}" > survey_signing_key.pem
171           SIGNATURE=$(echo -n "${PAYLOAD}" | openssl dgst -sha256 -sign survey_signing_key.pem | base64 -w0)
172           rm -f survey_signing_key.pem
174           SURVEY_KEY=$(curl -f -s -X POST -d "${PAYLOAD}" -H "Content-Type: application/json" -H "X-Signature: ${SIGNATURE}" https://survey-participate.openttd.org/create-survey-key/${{ vars.SURVEY_TYPE }})
175         fi
177         echo "survey_key=${SURVEY_KEY}" >> $GITHUB_OUTPUT
179     - name: Remove VCS information
180       run: |
181         rm -rf .git
183     - name: Create bundles
184       run: |
185         FOLDER_NAME=openttd-${{ steps.metadata.outputs.version }}
187         # Rename the folder to openttd-NNN
188         mkdir ${FOLDER_NAME}
189         find . -maxdepth 1 -not -name . -not -name build -not -name ${FOLDER_NAME} -exec mv {} ${FOLDER_NAME}/ \;
191         echo "::group::Create tarball (xz) bundle"
192         tar --xz -cvf build/bundles/${FOLDER_NAME}-source.tar.xz ${FOLDER_NAME}
193         echo "::endgroup::"
195         # This tarball is only to be used within this workflow.
196         echo "::group::Create tarball (gz) bundle"
197         tar --gzip -cvf source.tar.gz ${FOLDER_NAME}
198         echo "::endgroup::"
200         echo "::group::Create zip bundle"
201         zip -9 -r build/bundles/${FOLDER_NAME}-source.zip ${FOLDER_NAME}
202         echo "::endgroup::"
204     - name: Store bundles
205       uses: actions/upload-artifact@v4
206       with:
207         name: openttd-source
208         path: build/bundles/*
209         retention-days: 5
211     - name: Store source (for other jobs)
212       uses: actions/upload-artifact@v4
213       with:
214         name: internal-source
215         path: source.tar.gz
216         retention-days: 1