41 lines
1.6 KiB
Groovy
41 lines
1.6 KiB
Groovy
/**
|
|
* Commits specified files and pushes to the remote Git repo.
|
|
* Must be called inside container('tools') block (needs git).
|
|
*
|
|
* config keys:
|
|
* files (required) - list of file paths to stage, e.g. ['manifest/helm/Chart.yaml', 'manifest/helm/values.yaml']
|
|
* message (required) - commit message
|
|
* credId (optional) - Jenkins credential id for Gitea, default: gitea-credentials
|
|
* email (optional) - git author email, default: jenkins@fireflylab.local
|
|
* name (optional) - git author name, default: Jenkins
|
|
*/
|
|
def call(Map config) {
|
|
def files = config.files
|
|
def message = config.message
|
|
if (!files) error('gitCommitPush: files is required')
|
|
if (!message) error('gitCommitPush: message is required')
|
|
|
|
def credId = config.credId ?: 'gitea-credentials'
|
|
def email = config.email ?: 'jenkins@fireflylab.local'
|
|
def name = config.name ?: 'Jenkins'
|
|
|
|
def fileList = (files instanceof List) ? files.join(' ') : files
|
|
|
|
withCredentials([usernamePassword(
|
|
credentialsId: credId,
|
|
usernameVariable: 'GIT_USER',
|
|
passwordVariable: 'GIT_PASS'
|
|
)]) {
|
|
sh """
|
|
git config user.email "${email}"
|
|
git config user.name "${name}"
|
|
git add ${fileList}
|
|
git commit -m "${message}"
|
|
REMOTE_URL=\$(git remote get-url origin)
|
|
AUTH_URL=\$(echo \$REMOTE_URL | sed "s|https://|https://\${GIT_USER}:\${GIT_PASS}@|")
|
|
BRANCH=\$(git rev-parse --abbrev-ref HEAD)
|
|
git push \$AUTH_URL HEAD:\$BRANCH
|
|
"""
|
|
}
|
|
}
|