2021-06-15 10:46:56 +02:00
|
|
|
// Git operations
|
2021-03-13 00:07:22 +01:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/dagger"
|
|
|
|
"alpha.dagger.io/dagger/op"
|
|
|
|
"alpha.dagger.io/alpine"
|
2021-03-13 00:07:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// A git repository
|
|
|
|
#Repository: {
|
2021-09-01 18:49:21 +02:00
|
|
|
// Git remote link
|
2021-08-26 16:12:14 +02:00
|
|
|
remote: dagger.#Input & {string}
|
2021-06-03 12:46:14 +02:00
|
|
|
|
|
|
|
// Git ref: can be a commit, tag or branch.
|
|
|
|
// Example: "main"
|
2021-08-26 16:12:14 +02:00
|
|
|
ref: dagger.#Input & {string}
|
2021-06-03 12:46:14 +02:00
|
|
|
|
|
|
|
// (optional) Subdirectory
|
2021-08-26 16:12:14 +02:00
|
|
|
subdir: dagger.#Input & {*null | string}
|
2021-07-06 11:49:47 +02:00
|
|
|
|
|
|
|
// (optional) Keep .git directory
|
|
|
|
keepGitDir: *false | bool
|
2021-03-13 00:07:22 +01:00
|
|
|
|
2021-08-26 16:12:14 +02:00
|
|
|
// (optional) Add Personal Access Token
|
|
|
|
authToken: dagger.#Input & {*null | dagger.#Secret}
|
|
|
|
|
|
|
|
// (optional) Add OAuth Token
|
|
|
|
authHeader: dagger.#Input & {*null | dagger.#Secret}
|
|
|
|
|
2021-04-03 00:00:30 +02:00
|
|
|
#up: [
|
2021-10-04 16:45:20 +02:00
|
|
|
op.#Load & {
|
|
|
|
from: alpine.#Image & {
|
|
|
|
package: git: "=~2.30"
|
2021-08-26 16:12:14 +02:00
|
|
|
}
|
2021-03-13 00:07:22 +01:00
|
|
|
},
|
2021-10-04 16:45:20 +02:00
|
|
|
op.#Copy & {
|
|
|
|
from: [
|
|
|
|
op.#FetchGit & {
|
|
|
|
"remote": remote
|
|
|
|
"ref": ref
|
|
|
|
if (keepGitDir) {
|
|
|
|
keepGitDir: true
|
|
|
|
}
|
|
|
|
if (authToken != null) {
|
|
|
|
"authToken": authToken
|
|
|
|
}
|
|
|
|
if (authHeader != null) {
|
|
|
|
"authHeader": authHeader
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
dest: "/repository"
|
|
|
|
},
|
|
|
|
op.#Exec & {
|
|
|
|
dir: "/repository"
|
|
|
|
args: [
|
|
|
|
"/bin/sh",
|
|
|
|
"--noprofile",
|
|
|
|
"--norc",
|
|
|
|
"-eo",
|
|
|
|
"pipefail",
|
|
|
|
"-c",
|
|
|
|
#"""
|
|
|
|
code=$(git rev-parse --is-inside-work-tree 2>&1)
|
|
|
|
([ "$code" = "true" ] && git remote set-url origin "$REMOTE") || true
|
|
|
|
"""#,
|
|
|
|
]
|
|
|
|
env: REMOTE: remote
|
|
|
|
},
|
|
|
|
op.#Subdir & {
|
|
|
|
dir: "/repository"
|
|
|
|
},
|
2021-06-03 12:46:14 +02:00
|
|
|
if subdir != null {
|
2021-04-13 23:10:51 +02:00
|
|
|
op.#Subdir & {
|
|
|
|
dir: subdir
|
|
|
|
}
|
|
|
|
},
|
2021-03-13 00:07:22 +01:00
|
|
|
]
|
|
|
|
}
|
2021-05-29 05:11:17 +02:00
|
|
|
|
|
|
|
// Get the name of the current checked out branch or tag
|
|
|
|
#CurrentBranch: {
|
2021-06-15 10:46:56 +02:00
|
|
|
// Git repository
|
2021-05-29 05:11:17 +02:00
|
|
|
repository: dagger.#Artifact @dagger(input)
|
2021-06-15 10:46:56 +02:00
|
|
|
|
|
|
|
// Git branch name
|
2021-05-29 05:11:17 +02:00
|
|
|
name: {
|
|
|
|
string
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {
|
|
|
|
from: alpine.#Image & {
|
|
|
|
package: bash: "=~5.1"
|
|
|
|
package: git: "=~2.30"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#Exec & {
|
|
|
|
mount: "/repository": from: repository
|
|
|
|
dir: "/repository"
|
|
|
|
args: [
|
|
|
|
"/bin/bash",
|
|
|
|
"--noprofile",
|
|
|
|
"--norc",
|
|
|
|
"-eo",
|
|
|
|
"pipefail",
|
|
|
|
"-c",
|
|
|
|
#"""
|
|
|
|
printf "$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)" > /name.txt
|
|
|
|
"""#,
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#Export & {
|
|
|
|
source: "/name.txt"
|
|
|
|
format: "string"
|
|
|
|
},
|
|
|
|
]
|
2021-06-16 10:00:01 +02:00
|
|
|
} @dagger(output)
|
2021-05-29 05:11:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// List tags of a repository
|
|
|
|
#Tags: {
|
2021-06-15 10:46:56 +02:00
|
|
|
// Git repository
|
2021-05-29 05:11:17 +02:00
|
|
|
repository: dagger.#Artifact @dagger(input)
|
2021-06-15 10:46:56 +02:00
|
|
|
|
|
|
|
// Repository tags
|
2021-05-29 05:11:17 +02:00
|
|
|
tags: {
|
|
|
|
[...string]
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {
|
|
|
|
from: alpine.#Image & {
|
|
|
|
package: bash: "=~5.1"
|
|
|
|
package: jq: "=~1.6"
|
|
|
|
package: git: "=~2.30"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#Exec & {
|
|
|
|
mount: "/repository": from: repository
|
|
|
|
dir: "/repository"
|
|
|
|
args: [
|
|
|
|
"/bin/bash",
|
|
|
|
"--noprofile",
|
|
|
|
"--norc",
|
|
|
|
"-eo",
|
|
|
|
"pipefail",
|
|
|
|
"-c",
|
|
|
|
#"""
|
|
|
|
git tag -l | jq --raw-input --slurp 'split("\n") | map(select(. != ""))' > /tags.json
|
|
|
|
"""#,
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#Export & {
|
|
|
|
source: "/tags.json"
|
|
|
|
format: "json"
|
|
|
|
},
|
|
|
|
]
|
2021-06-16 10:00:01 +02:00
|
|
|
} @dagger(output)
|
2021-05-29 05:11:17 +02:00
|
|
|
}
|