Skip to content

Commit dfa4ead

Browse files
Updating Jenkins CI to create nightly tag and upload artifacts
1 parent 4ec6970 commit dfa4ead

File tree

1 file changed

+171
-4
lines changed

1 file changed

+171
-4
lines changed

Jenkinsfile

Lines changed: 171 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,102 @@ def SetGithubStatus(githubToken, context, targetUrl, desc, status, repoOwner, re
2424
"""
2525
}
2626

27+
def UpdateNightlyTag(githubToken, repoOwner, repoName, expectedHash)
28+
{
29+
// Install jq and zip if not already installed
30+
bash """
31+
if ! command -v jq &> /dev/null; then
32+
echo "Installing jq..."
33+
apt-get update
34+
apt-get install -y jq
35+
fi
36+
37+
if ! command -v zip &> /dev/null; then
38+
echo "Installing zip..."
39+
apt-get update
40+
apt-get install -y jq zip
41+
fi
42+
"""
43+
44+
def response = bash """
45+
curl -L --fail-with-body \\
46+
-X PATCH \\
47+
-H "Accept: application/vnd.github+json" \\
48+
-H "Authorization: Bearer ${githubToken}" \\
49+
-H "X-GitHub-Api-Version: 2022-11-28" \\
50+
-d '{
51+
"sha":"${expectedHash}",
52+
"force":true
53+
}' \\
54+
https://api.github.com/repos/${repoOwner}/${repoName}/git/refs/tags/nightly
55+
"""
56+
57+
echo "response: ${response}"
58+
}
59+
60+
def UploadArtifactToGithub(githubToken, repoOwner, repoName, tagName, artifactPath, artifactName)
61+
{
62+
bash """
63+
# Create a release if it doesn't exist
64+
RELEASE_ID=\$(curl -s -L --fail-with-body \\
65+
-H "Authorization: Bearer ${githubToken}" \\
66+
"https://api.github.com/repos/${repoOwner}/${repoName}/releases/tags/${tagName}" \\
67+
| jq -r '.id')
68+
69+
if [ "\$RELEASE_ID" = "null" ]; then
70+
RELEASE_ID=\$(curl -s -L --fail-with-body \\
71+
-X POST \\
72+
-H "Accept: application/vnd.github+json" \\
73+
-H "Authorization: Bearer ${githubToken}" \\
74+
-H "X-GitHub-Api-Version: 2022-11-28" \\
75+
-d '{
76+
"tag_name": "${tagName}",
77+
"name": "Nightly Build",
78+
"body": "Automated nightly build from CI"
79+
}' \\
80+
"https://api.github.com/repos/${repoOwner}/${repoName}/releases" | jq -r '.id')
81+
fi
82+
83+
curl -L --fail-with-body \\
84+
-X PATCH \\
85+
-H "Accept: application/vnd.github+json" \\
86+
-H "Authorization: Bearer ${githubToken}" \\
87+
-H "X-GitHub-Api-Version: 2022-11-28" \\
88+
-d '{
89+
"tag_name": "${tagName}",
90+
"name": "Nightly Build",
91+
"body": "Automated nightly build from CI"
92+
}' \\
93+
"https://api.github.com/repos/${repoOwner}/${repoName}/releases/\${RELEASE_ID}"
94+
95+
# Check if the asset already exists
96+
ASSET_ID=\$(curl -s -L --fail-with-body \\
97+
-H "Authorization: Bearer ${githubToken}" \\
98+
"https://api.github.com/repos/${repoOwner}/${repoName}/releases/\${RELEASE_ID}/assets" |
99+
jq -r ".[] | select(.name == \\\"${artifactName}\\\") | .id")
100+
101+
if [ "\$ASSET_ID" != "" ]; then
102+
echo "Asset ${artifactName} already exists with ID \$ASSET_ID. Deleting it..."
103+
curl -L --fail-with-body \\
104+
-X DELETE \\
105+
-H "Accept: application/vnd.github+json" \\
106+
-H "Authorization: Bearer ${githubToken}" \\
107+
-H "X-GitHub-Api-Version: 2022-11-28" \\
108+
"https://api.github.com/repos/${repoOwner}/${repoName}/releases/assets/\${ASSET_ID}"
109+
fi
110+
111+
# Upload the artifact
112+
curl -L --fail-with-body \\
113+
-X POST \\
114+
-H "Accept: application/vnd.github+json" \\
115+
-H "Authorization: Bearer ${githubToken}" \\
116+
-H "Content-Type: application/octet-stream" \\
117+
--data-binary "@${artifactPath}" \\
118+
"https://uploads.github.com/repos/${repoOwner}/${repoName}/releases/\${RELEASE_ID}/assets?name=${artifactName}"
119+
"""
120+
//"""
121+
}
122+
27123
def REPO_OWNER = "Neko-Box-Coder"
28124
def REPO_NAME = "runcpp2"
29125
def TARGET_URL = 'https://github.com/Neko-Box-Coder/runcpp2.git'
@@ -97,9 +193,20 @@ pipeline
97193
error('Receiving non relevant PR action')
98194
else
99195
{
100-
timeout(time: 30, unit: 'MINUTES')
196+
if(env.GITHUB_PR_REPO_OWNER == REPO_OWNER)
197+
{
198+
echo "env.GITHUB_PR_REPO_OWNER (${env.GITHUB_PR_REPO_OWNER}) is " +
199+
"the same as original REPO_OWNER (${REPO_OWNER})"
200+
echo "Skipping approval..."
201+
}
202+
else
101203
{
102-
input 'Approval this job?'
204+
echo "env.GITHUB_PR_REPO_OWNER (${env.GITHUB_PR_REPO_OWNER}) is " +
205+
" not the same as original REPO_OWNER (${REPO_OWNER})"
206+
timeout(time: 30, unit: 'MINUTES')
207+
{
208+
input 'Approval this job?'
209+
}
103210
}
104211
}
105212

@@ -146,7 +253,7 @@ pipeline
146253
]
147254
)
148255

149-
GIT_HASH = bash("echo \$(git rev-parse --verify HEAD)", true)
256+
GIT_HASH = bash("echo \$(git rev-parse --verify HEAD)", true).trim()
150257
echo "GITHASH: ${GIT_HASH}"
151258

152259
if(!STORE_BUILD)
@@ -221,7 +328,6 @@ pipeline
221328

222329
//NOTE: We use Debug builds for now even for release.
223330
}
224-
225331
stage('Test')
226332
{
227333
parallel
@@ -338,6 +444,67 @@ pipeline
338444
}
339445
}
340446
}
447+
stage('Update GitHub Nightly Release')
448+
{
449+
agent { label 'linux' }
450+
when
451+
{
452+
expression { return env.X_GitHub_Event == 'push' &&
453+
env.GITHUB_PUSH_REF == 'refs/heads/master' }
454+
}
455+
steps
456+
{
457+
script
458+
{
459+
withCredentials([string(credentialsId: 'github-token', variable: 'GITHUB_TOKEN')])
460+
{
461+
//Wait for nightly tag to be updated to the correct commit
462+
UpdateNightlyTag('$GITHUB_TOKEN', REPO_OWNER, REPO_NAME, GIT_HASH)
463+
464+
//Upload Linux executable
465+
dir('WindowsBuild') { unstash 'windows_build' }
466+
dir('LinuxBuild') { unstash 'linux_build' }
467+
if (fileExists('LinuxBuild/Build/runcpp2'))
468+
{
469+
bash """
470+
cd LinuxBuild/Build
471+
zip -9 ../../runcpp2-linux.zip runcpp2
472+
"""
473+
UploadArtifactToGithub( '$GITHUB_TOKEN',
474+
REPO_OWNER,
475+
REPO_NAME,
476+
"nightly",
477+
"runcpp2-linux.zip",
478+
"runcpp2-linux.zip")
479+
}
480+
else
481+
{
482+
error('Failed to find linux build')
483+
}
484+
485+
//Upload Windows executable
486+
if (fileExists('WindowsBuild/Build/Debug/runcpp2.exe'))
487+
{
488+
bash """
489+
cd WindowsBuild/Build/Debug
490+
zip -9 ../../../runcpp2-windows.zip runcpp2.exe
491+
"""
492+
UploadArtifactToGithub( '$GITHUB_TOKEN',
493+
REPO_OWNER,
494+
REPO_NAME,
495+
"nightly",
496+
"runcpp2-windows.zip",
497+
"runcpp2-windows.zip")
498+
}
499+
else
500+
{
501+
error('Failed to find windows build')
502+
}
503+
}
504+
}
505+
}
506+
post { failure { script { FAILED_STAGE = env.STAGE_NAME } } }
507+
}
341508

342509
stage('Notify')
343510
{

0 commit comments

Comments
 (0)