Skip to content

Commit 5541513

Browse files
berndmpfz0r
andauthored
Implement Chocolatey build and refactor pipeline script (#430)
The build now creates a Chocolatey .nupkg file on every build and publishes the file when building a release. Refactor the pipeline script to run all stages on the same node. This ensures that all stages have access to the build artifacts and we don't have to stash/unstash the files for each stage. Additional changes: - Update "graylog-sidecar.nuspec" file with latest URLs and author information - Generate the "chocolateyinstall.ps1" file from a template for every build instead of overwriting the file for each release. - Apply consistent formatting for the pipeline script Co-authored-by: Marco Pfatschbacher <marco@graylog.com>
1 parent 0d69657 commit 5541513

7 files changed

+149
-96
lines changed

Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ifeq ($(strip $(COLLECTOR_VERSION)),)
99
$(error COLLECTOR_VERSION is not set)
1010
endif
1111

12-
targets = graylog-sidecar sidecar-collector build dist/cache dist/tmp-build dist/tmp-dest dist/pkg dist/collectors resource_windows.syso
12+
targets = graylog-sidecar sidecar-collector build dist/cache dist/tmp-build dist/tmp-dest dist/pkg dist/collectors resource_windows.syso dist/chocolatey/tools/chocolateyinstall.ps1
1313
dist_targets = vendor
1414

1515
GIT_REV=$(shell git rev-parse --short HEAD)
@@ -111,7 +111,16 @@ package-linux32: ## Create Linux i386 system package
111111
package-windows: prepare-package ## Create Windows installer
112112
@mkdir -p dist/pkg
113113
makensis -DVERSION=$(COLLECTOR_VERSION) -DVERSION_SUFFIX=$(COLLECTOR_VERSION_SUFFIX) -DREVISION=$(COLLECTOR_REVISION) dist/recipe.nsi
114+
115+
package-chocolatey: ## Create Chocolatey .nupkg file
116+
# This needs to run in a Docker container based on the Dockerfile.chocolatey image!
114117
dist/chocolatey/gensha.sh $(COLLECTOR_VERSION) $(COLLECTOR_REVISION) $(COLLECTOR_VERSION_SUFFIX)
118+
cd dist/chocolatey && choco pack graylog-sidecar.nuspec --version $(COLLECTOR_VERSION)$(COLLECTOR_VERSION_SUFFIX) --out ../pkg
119+
120+
push-chocolatey: ## Push Chocolatey .nupkg file
121+
# This needs to run in a Docker container based on the Dockerfile.chocolatey image!
122+
# Escape the CHOCO_API_KEY to avoid printing it in the logs!
123+
choco push dist/pkg/graylog-sidecar.$(COLLECTOR_VERSION)$(COLLECTOR_VERSION_SUFFIX).nupkg -k=$$CHOCO_API_KEY
115124

116125
package-tar: ## Create tar archive for all platforms
117126
@mkdir -p dist/pkg

dist/chocolatey/gensha.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ else
1111
COLLECTOR_CHECKSUM=$(sha256sum dist/pkg/graylog_sidecar_installer_${COLLECTOR_VERSION}-${COLLECTOR_REVISION}.exe | cut -d" " -f1)
1212
fi
1313

14-
sed -i "s/checksum = '.*'/checksum = '$COLLECTOR_CHECKSUM'/" dist/chocolatey/tools/chocolateyinstall.ps1
15-
sed -i "s/url = '.*'/url = 'https:\/\/downloads.graylog.org\/releases\/graylog-collector-sidecar\/${COLLECTOR_VERSION}\/graylog_sidecar_installer_${COLLECTOR_VERSION}-${COLLECTOR_REVISION}.exe'/" dist/chocolatey/tools/chocolateyinstall.ps1
14+
root_url="https://downloads.graylog.org/releases/graylog-collector-sidecar"
15+
version_url="${root_url}/${COLLECTOR_VERSION}/graylog_sidecar_installer_${COLLECTOR_VERSION}-${COLLECTOR_REVISION}.exe"
16+
17+
sed -e "s,%%CHECKSUM%%,$COLLECTOR_CHECKSUM,g" \
18+
-e "s,%%URL%%,$version_url,g" \
19+
"dist/chocolatey/tools/chocolateyinstall.ps1.template" \
20+
> "dist/chocolatey/tools/chocolateyinstall.ps1"
1621

1722
find dist/pkg -name "graylog_sidecar_installer*.exe" -exec /bin/bash -c "sha256sum {} | cut -d' ' -f1 > {}.sha256.txt" \;

dist/chocolatey/graylog-sidecar.nuspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<version>$version$</version>
66
<packageSourceUrl>https://github.com/Graylog2/collector-sidecar</packageSourceUrl>
77
<title>Graylog Sidecar</title>
8-
<authors>juju2112</authors>
8+
<authors>graylog-bernd</authors>
99
<projectUrl>https://github.com/Graylog2/collector-sidecar</projectUrl>
1010
<iconUrl>https://rawcdn.githack.com/Graylog2/collector-sidecar/1ec7260410caddfabe4c972f347d0237b2f5f323/images/graylog-icon.svg</iconUrl>
1111
<licenseUrl>https://github.com/Graylog2/collector-sidecar/blob/master/LICENSE</licenseUrl>
1212
<requireLicenseAcceptance>true</requireLicenseAcceptance>
1313
<projectSourceUrl>https://github.com/Graylog2/collector-sidecar</projectSourceUrl>
14-
<docsUrl>https://docs.graylog.org/en/latest/pages/sidecar.html</docsUrl>
14+
<docsUrl>https://docs.graylog.org/docs/sidecar</docsUrl>
1515
<bugTrackerUrl>https://github.com/Graylog2/collector-sidecar/issues</bugTrackerUrl>
1616
<tags>graylog graylog-sidecar log</tags>
1717
<summary>Manage log collectors through Graylog</summary>
@@ -24,6 +24,6 @@
2424
<releaseNotes></releaseNotes>
2525
</metadata>
2626
<files>
27-
<file src="tools/**" target="tools" />
27+
<file src="tools/*.ps1" target="tools" />
2828
</files>
2929
</package>

dist/chocolatey/tools/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/chocolateyinstall.ps1

dist/chocolatey/tools/chocolateyinstall.ps1 dist/chocolatey/tools/chocolateyinstall.ps1.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ $packageArgs = @{
55
packageName = $env:ChocolateyPackageName
66
unzipLocation = $toolsDir
77
fileType = 'EXE'
8-
url = 'https://downloads.graylog.org/releases/graylog-collector-sidecar/1.1.0/graylog_sidecar_installer_1.1.0-1.exe'
8+
url = '%%URL%%'
99
softwareName = 'graylog-sidecar*'
10-
checksum = '08ee296fa6970fec2026d5956bdfcf9eb622dba92c8e5f811ddf3a765041b810'
10+
checksum = '%%CHECKSUM%%'
1111
checksumType = 'sha256'
1212
silentArgs = '/S'
1313
}

docker/Dockerfile.chocolatey

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM chocolatey/choco:v1.1.0
2+
3+
# The choco binary wants to write to /opt/chocolatey and Jenkins is running
4+
# the container as non-root user.
5+
RUN chmod 777 /opt/chocolatey
6+
7+
RUN apt-get update \
8+
&& apt-get install --no-install-recommends -y make \
9+
&& apt-get clean

jenkins.groovy

+117-88
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,149 @@
11
pipeline
22
{
3-
agent none
4-
5-
options
6-
{
7-
buildDiscarder logRotator(artifactDaysToKeepStr: '30', artifactNumToKeepStr: '10', daysToKeepStr: '30', numToKeepStr: '10')
8-
timestamps()
9-
withAWS(region:'eu-west-1', credentials:'aws-key-releases')
10-
}
11-
12-
tools
13-
{
14-
go 'Go 1.19'
15-
}
16-
17-
environment
18-
{
19-
GOPATH = '/home/jenkins/go'
20-
GO15VENDOREXPERIMENT=1
21-
}
22-
23-
stages
24-
{
25-
stage('Build')
3+
agent none
4+
5+
options
6+
{
7+
buildDiscarder logRotator(artifactDaysToKeepStr: '30', artifactNumToKeepStr: '10', daysToKeepStr: '30', numToKeepStr: '10')
8+
timestamps()
9+
withAWS(region:'eu-west-1', credentials:'aws-key-releases')
10+
}
11+
12+
tools
13+
{
14+
go 'Go 1.19'
15+
}
16+
17+
environment
18+
{
19+
GOPATH = '/home/jenkins/go'
20+
GO15VENDOREXPERIMENT=1
21+
CHOCO_API_KEY = credentials('chocolatey-api-key')
22+
}
23+
24+
stages
25+
{
26+
stage('Build')
27+
{
28+
agent
2629
{
27-
agent
28-
{
29-
label 'linux'
30-
}
30+
// Select the node for all nested stages.
31+
label 'linux'
32+
}
33+
34+
// All nested stages run on the same node because the nested stages
35+
// don't have an agent label selector.
36+
// That ensures we share the workspace between the different stages.
37+
stages
38+
{
39+
stage('Compile')
40+
{
3141
steps
3242
{
33-
sh 'go version'
34-
sh 'go mod vendor'
35-
sh "make test"
36-
sh 'make build-all'
37-
stash name: 'build artifacts', includes: 'build/**'
43+
sh 'go version'
44+
sh 'go mod vendor'
45+
sh "make test"
46+
sh 'make build-all'
3847
}
48+
}
3949

40-
post
50+
stage('Package')
51+
{
52+
agent
4153
{
42-
cleanup
54+
docker
4355
{
44-
cleanWs()
56+
image 'torch/jenkins-fpm-cook:latest'
57+
args '-u jenkins:docker'
58+
reuseNode true
4559
}
4660
}
47-
}
4861

49-
stage('Package')
50-
{
51-
agent
52-
{
53-
docker
54-
{
55-
label 'linux'
56-
image 'torch/jenkins-fpm-cook:latest'
57-
args '-u jenkins:docker'
58-
}
59-
}
60-
61-
steps
62-
{
63-
unstash 'build artifacts'
62+
steps
63+
{
6464
sh 'make package-all'
65+
}
66+
}
6567

66-
stash name: 'package artifacts', includes: 'dist/pkg/**'
67-
}
68-
69-
post
70-
{
71-
success
68+
stage('Chocolatey Pack')
69+
{
70+
agent
71+
{
72+
dockerfile
7273
{
73-
archiveArtifacts 'dist/pkg/*'
74+
label 'linux'
75+
filename 'Dockerfile.chocolatey'
76+
dir 'docker'
77+
additionalBuildArgs '-t local/sidecar-chocolatey'
78+
reuseNode true
7479
}
80+
}
81+
82+
steps
83+
{
84+
sh 'make package-chocolatey'
85+
}
86+
}
87+
88+
stage('Chocolatey Push')
89+
{
90+
when
91+
{
92+
buildingTag()
93+
}
7594

76-
cleanup
95+
agent
96+
{
97+
dockerfile
7798
{
78-
cleanWs()
99+
label 'linux'
100+
filename 'Dockerfile.chocolatey'
101+
dir 'docker'
102+
additionalBuildArgs '-t local/sidecar-chocolatey'
103+
reuseNode true
79104
}
80-
}
81-
}
105+
}
82106

83-
stage('Upload')
84-
{
85-
when
86-
{
87-
buildingTag()
107+
steps
108+
{
109+
sh 'make push-chocolatey'
110+
}
88111
}
89112

90-
agent
113+
stage('Upload')
91114
{
92-
label 'linux'
115+
when
116+
{
117+
buildingTag()
118+
}
119+
120+
steps
121+
{
122+
echo "==> Artifact checksums:"
123+
sh "sha256sum dist/pkg/*"
124+
125+
s3Upload(
126+
workingDir: '.',
127+
bucket: 'graylog2-releases',
128+
path: "graylog-collector-sidecar/${env.TAG_NAME}/",
129+
file: "dist/pkg"
130+
)
131+
}
93132
}
133+
}
94134

95-
steps
135+
post
136+
{
137+
success
96138
{
97-
// Provide access to "dist/pkg", the previous cleanups removed the files
98-
unstash 'package artifacts'
99-
100-
echo "==> Artifact checksums:"
101-
sh "sha256sum dist/pkg/*"
102-
103-
s3Upload(
104-
workingDir: '.',
105-
bucket: 'graylog2-releases',
106-
path: "graylog-collector-sidecar/${env.TAG_NAME}/",
107-
file: "dist/pkg"
108-
)
139+
archiveArtifacts 'dist/pkg/*'
109140
}
110141

111-
post
142+
cleanup
112143
{
113-
cleanup
114-
{
115-
cleanWs()
116-
}
144+
cleanWs()
117145
}
118146
}
119-
}
147+
}
148+
}
120149
}

0 commit comments

Comments
 (0)