Skip to content

Commit b9f24de

Browse files
committed
Create a GitHub action to create a release
This should replace the script that I have locally to test swift-format for a release and at the same time expand the configurations that are being tested.
1 parent 26fbd13 commit b9f24de

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
SWIFT_SYNTAX_TAG="$1"
6+
SWIFT_FORMAT_VERSION="$2"
7+
8+
if [[ -z "$SWIFT_SYNTAX_TAG" || -z "$SWIFT_FORMAT_VERSION" ]]; then
9+
echo "Update the Package manifest to reference a specific version of swift-syntax and embed the given version in the swift-format --version command"
10+
echo "Usage create-release-commits.sh <SWIFT_SYNTAX_TAG> <SWIFT_FORMAT_VERSION>"
11+
echo " SWIFT_SYNTAX_TAG: The tag of swift-syntax to depend on"
12+
echo " SWIFT_FORMAT_VERSION: The version of swift-format that is about to be released"
13+
exit 1
14+
fi
15+
16+
git config --global user.name 'swift-ci'
17+
git config --global user.email '[email protected]'
18+
git config --global --add safe.directory $(realpath .)
19+
20+
sed -E -i "s#branch: \"(main|release/[0-9]+\.[0-9]+)\"#from: \"$SWIFT_SYNTAX_TAG\"#" Package.swift
21+
git add Package.swift
22+
git commit -m "Change swift-syntax dependency to $SWIFT_SYNTAX_TAG"
23+
24+
sed -E -i "s#print\(\".*\"\)#print\(\"$SWIFT_FORMAT_VERSION\"\)#" Sources/swift-format/PrintVersion.swift
25+
git add Sources/swift-format/PrintVersion.swift
26+
git commit -m "Change version to $SWIFT_FORMAT_VERSION"

.github/workflows/publish_release.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Publish Release
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
prerelease:
7+
type: boolean
8+
description: "Prerelease"
9+
# Whether to create a prerelease or proper release
10+
default: true
11+
required: true
12+
swift_format_version:
13+
type: string
14+
default: 601.0.0
15+
description: "swift-format version"
16+
# The version of swift-format to tag. If this is a prerelease, `-prerelease-<date>` is added to this version.
17+
required: true
18+
swift_syntax_tag:
19+
type: string
20+
default: 601.0.0
21+
description: "swift-syntax version"
22+
# The swift-syntax version to depend on. If this is a prerelease, the latest swift-syntax prerelease tag for this version is used.
23+
required: true
24+
25+
jobs:
26+
check_triggering_actor:
27+
name: Check user is allowed to create release
28+
# Only a single user should be allowed to create releases to avoid two people triggering the creation of a release
29+
# at the same time. If the release manager changes between users, update this condition.
30+
runs-on: ubuntu-latest
31+
steps:
32+
- run: |
33+
if [[ "${{ github.triggering_actor }}" != "ahoppen" ]]; then
34+
echo "${{ github.triggering_actor }} is not allowed to create a release"
35+
exit 1
36+
fi
37+
define_tags:
38+
name: Determine dependent swift-syntax version and prerelease date
39+
runs-on: ubuntu-latest
40+
outputs:
41+
swift_syntax_tag: ${{ steps.swift_syntax_tag.outputs.swift_syntax_tag }}
42+
swift_format_version: ${{ steps.swift_format_version.outputs.swift_format_version }}
43+
steps:
44+
- name: Determine swift-syntax tag to depend on
45+
id: swift_syntax_tag
46+
shell: bash
47+
run: |
48+
if [[ "${{ github.event.inputs.prerelease }}" == "false" ]]; then
49+
SWIFT_SYNTAX_TAG="${{ github.event.inputs.swift_syntax_tag }}"
50+
else
51+
git clone https://github.com/swiftlang/swift-syntax.git
52+
cd swift-syntax
53+
SWIFT_SYNTAX_TAG="$(git tag | grep ${{ github.event.inputs.swift_syntax_tag }}-prerelease | sort -r | head -1)"
54+
fi
55+
56+
echo "Using swift-syntax tag: $SWIFT_SYNTAX_TAG"
57+
echo "swift_syntax_tag=$SWIFT_SYNTAX_TAG" >> "$GITHUB_OUTPUT"
58+
- name: Determine swift-format prerelease version
59+
id: swift_format_version
60+
run: |
61+
if [[ "${{ github.event.inputs.prerelease }}" == "false" ]]; then
62+
SWIFT_FORMAT_VERSION="${{ github.event.inputs.swift_format_version }}"
63+
else
64+
SWIFT_FORMAT_VERSION="${{ github.event.inputs.swift_format_version }}-prerelease-$(date +'%Y-%m-%d')"
65+
fi
66+
echo "Using swift-format version: $SWIFT_FORMAT_VERSION"
67+
echo "swift_format_version=$SWIFT_FORMAT_VERSION" >> "$GITHUB_OUTPUT"
68+
test_debug:
69+
name: Test in Debug configuration
70+
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
71+
needs: define_tags
72+
with:
73+
pre_build_command: bash .github/workflows/create-release-commits.sh '${{ needs.define_tags.outputs.swift_syntax_tag }}' '${{ needs.define_tags.outputs.swift_format_version }}'
74+
test_release:
75+
name: Test in Release configuration
76+
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
77+
needs: define_tags
78+
with:
79+
pre_build_command: bash .github/workflows/create-release-commits.sh '${{ needs.define_tags.outputs.swift_syntax_tag }}' '${{ needs.define_tags.outputs.swift_format_version }}'
80+
build_command: swift test -c release
81+
create_tag:
82+
name: Create Tag
83+
runs-on: ubuntu-latest
84+
needs: [check_triggering_actor, test_debug, test_release, define_tags]
85+
steps:
86+
- name: Checkout repository
87+
uses: actions/checkout@v4
88+
- name: Create release commits
89+
run: bash .github/workflows/create-release-commits.sh '${{ needs.define_tags.outputs.swift_syntax_tag }}' '${{ needs.define_tags.outputs.swift_format_version }}'
90+
- name: Tag release
91+
run: |
92+
git tag "${{ needs.define_tags.outputs.swift_format_version }}"
93+
git push origin "${{ needs.define_tags.outputs.swift_format_version }}"
94+
- name: Create release
95+
env:
96+
GH_TOKEN: ${{ github.token }}
97+
run: |
98+
if [[ "${{ github.event.inputs.prerelease }}" != "true" ]]; then
99+
# Only create a release automatically for prereleases. For real releases, release notes should be crafted by hand.
100+
exit
101+
fi
102+
gh release create "${{ needs.define_tags.outputs.swift_format_version }}" \
103+
--title "${{ needs.define_tags.outputs.swift_format_version }}" \
104+
--prerelease
105+

0 commit comments

Comments
 (0)