-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile3
107 lines (95 loc) · 3.52 KB
/
Jenkinsfile3
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
pipeline {
agent {
label "w-1" // w-1 ~ worker-1
}
environment {
// Get the latest Git tag for versioning
GIT_TAG = sh(script: "git describe --tags --abbrev=0 || git rev-parse --short HEAD", returnStdout: true).trim()
GITHUB_TOKEN = credentials('jenkins-github-integration-demo.2025-03-06.private-key.pem')
REPO_URL = 'https://github.com/repo.git'
ZIP_NAME = "gitea-archive-${GIT_TAG}.tar.gz" // tar.gz file name based on tag
}
tools {
go '1.24.0'
}
stages {
// Checkout the repository and fetch tags
stage('Checkout') {
steps {
script {
sh 'git fetch --tags' // Explicitly fetch tags from the remote repository
}
}
}
// Get the Git tag
stage('Get Git Tag') {
steps {
script {
GIT_TAG = sh(script: "git describe --tags --abbrev=0 || git rev-parse --short HEAD", returnStdout: true).trim()
echo "Using Git tag: ${GIT_TAG}"
}
}
}
// Run Go Build
stage('Go Build') {
steps {
script {
// Build the Go binary with minimized size
sh 'go build -ldflags="-s -w" -o gitea'
}
}
}
// Create a tar.gz archive
stage('Create TAR Archive') {
steps {
script {
// Define the files to include in the archive
def filesToInclude = [
"web_src/",
"templates/",
"models/",
"public/",
"options/",
"modules/",
"gitea", // The built Go binary
"custom/"
]
// Build the tar command dynamically, using -C to avoid absolute paths
def tarCommand = "tar -czf ${ZIP_NAME} -C $WORKSPACE "
filesToInclude.each { file ->
tarCommand += "${file} "
}
// Run the tar command to create the archive
sh tarCommand
}
}
}
// Create a GitHub release and upload the tar.gz file
stage('Create GitHub Release') {
when { branch 'main' }
steps {
script {
echo 'Checking if GitHub Release already exists...'
// Check if the release already exists using GitHub CLI
def releaseExists = sh(
script: "gh release view ${GIT_TAG} --repo ${REPO_URL} > /dev/null 2>&1",
returnStatus: true
) == 0
if (releaseExists) {
echo "Release ${GIT_TAG} already exists. Skipping release creation."
} else {
echo 'Creating GitHub Release...'
// Create the GitHub release and upload the tar.gz file
sh """
gh release create \
${GIT_TAG} \
${ZIP_NAME} \
--repo ${REPO_URL} \
--generate-notes
"""
}
}
}
}
}
}