-
Notifications
You must be signed in to change notification settings - Fork 4
165 lines (154 loc) · 5.17 KB
/
build-gem.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
name: Build gem
on:
workflow_dispatch:
inputs:
push:
description: Push gem
required: true
type: boolean
default: true
push:
branches:
- '**'
env:
GEM_HOST: 'https://rubygems.pkg.github.com/DataDog'
jobs:
build:
strategy:
fail-fast: false
matrix:
type:
- final
- dev
runs-on: ubuntu-latest
name: Build gem (${{ matrix.type }})
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: ruby/setup-ruby@31a7f6d628878b80bc63375a93ae079ec50a1601 # v1.143.0
with:
ruby-version: '3.2'
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Patch version
if: ${{ matrix.type != 'final' }}
run: |
# Obtain context information
git_ref='${{ github.ref }}'
git_branch="$(echo "${git_ref}" | sed -e 's#^refs/heads/##')"
git_sha='${{ github.sha }}'
gha_run_id='${{ github.run_id }}'
# Output info for CI debug
echo git_ref="${git_ref}"
echo git_branch="${git_branch}"
echo git_sha="${git_sha}"
echo gha_run_id="${gha_run_id}"
# Sanitize for ruby version usage
git_branch_sanitized="$(echo "$git_branch" | sed -e 's/[^a-zA-Z0-9+]\{1,\}/./g')"
echo git_branch_sanitized="${git_branch_sanitized}"
# Shorten commit sha
git_sha_short="${git_sha:0:12}"
echo git_sha_short="${git_sha_short}"
# Set component values:
# - PRE is `dev` to denote being a development version and
# act as a categorizer.
# - BUILD starts with CI run id for ordering.
# - BUILD has CI run id for traceability, prefixed by `gha`
# for identification.
# - BUILD has commit next for traceability, prefixed git-describe
# style by `g` for identification.
# - BUILD has branch name last since it has to be separated
# by dots and thus has variable version segment size and
# unpredictable ordering; it can thus be reliably extracted
# and does not impair readability in lists
PRE='${{ matrix.type }}'
BUILD="gha${gha_run_id}.g${git_sha_short}.${git_branch_sanitized}"
# Output info for CI debug
echo PRE="${PRE}"
echo BUILD="${BUILD}"
# Patch in components
sed lib/datadog/ci/version.rb -i -e "s/^\([\t ]*PRE\) *= */\1 = \'${PRE}\' # /"
sed lib/datadog/ci/version.rb -i -e "s/^\([\t ]*BUILD\) *= */\1 = \'${BUILD}\' # /"
# Test result
cat lib/datadog/ci/version.rb | grep -e PRE -e BUILD
ruby -Ilib -rdatadog/ci/version -e 'puts Datadog::CI::VERSION::STRING'
ruby -Ilib -rdatadog/ci/version -e 'puts Gem::Version.new(Datadog::CI::VERSION::STRING).to_s'
- name: Patch gem host
if: ${{ matrix.type != 'final' }}
run: |
# Patch in GEM_HOST
sed datadog-ci.gemspec -i -e "s,^\([\t ]*spec\.metadata\['allowed_push_host'\]\) *= *,\1 = \'${GEM_HOST}\' # ,"
# Test result
cat datadog-ci.gemspec | grep -e allowed_push_host
- name: Build gem
run: bundle exec rake build
- name: List gem
run: |
find pkg
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: 'datadog-ci-gem-${{ matrix.type }}-gha${{ github.run_id }}-g${{ github.sha }}'
path: 'pkg/*.gem'
test:
strategy:
fail-fast: false
matrix:
type:
- final
- dev
runs-on: ubuntu-latest
name: Test gem
needs:
- build
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: 'datadog-ci-gem-${{ matrix.type }}-gha${{ github.run_id }}-g${{ github.sha }}'
path: 'pkg'
- name: List gem
run: |
find pkg
- uses: ruby/setup-ruby@31a7f6d628878b80bc63375a93ae079ec50a1601 # v1.143.0
with:
ruby-version: '3.2'
- name: Install gem
run: |
gem install pkg/*.gem
push:
strategy:
fail-fast: false
matrix:
type:
- dev
runs-on: ubuntu-latest
name: Push gem
needs:
- test
if: ${{ inputs.push }}
steps:
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: 'datadog-ci-gem-${{ matrix.type }}-gha${{ github.run_id }}-g${{ github.sha }}'
path: 'pkg'
- name: List gem
run: |
find pkg
- name: Set up GitHub Packages authentication
run: |
mkdir -p ~/.gem
cat > ~/.gem/credentials <<'CREDENTIALS'
---
:github: Bearer ${{ secrets.GITHUB_TOKEN }}
CREDENTIALS
chmod 0600 ~/.gem/credentials
- name: Push gem
run: |
find pkg -name '*.gem' | while read -r gem; do
echo "=== pushing '${gem}'"
gem push --key github --host ${{ env.GEM_HOST }} "${gem}"
done
- name: Clean up credentials
run: |
rm -rvf ~/.gem/credentials