Merge pull request #935 from TomChv/feat/improve-git-pkg

Improve git package to commit & push
This commit is contained in:
Sam Alba 2021-09-03 09:55:03 -07:00 committed by GitHub
commit db8a92c371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 272 additions and 20 deletions

View File

@ -10,6 +10,28 @@ Git operations
import "alpha.dagger.io/git" import "alpha.dagger.io/git"
``` ```
## git.#Commit
Commit & push to git repository
### git.#Commit Inputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*repository.remote* | `string` |Repository remote URL |
|*repository.authToken* | `dagger.#Secret` |Authentication token (PAT or password) |
|*repository.branch* | `string` |Git branch |
|*name* | `string` |Username |
|*email* | `string` |Email |
|*message* | `string` |Commit message |
|*force* | `*false \| bool` |Force push options |
### git.#Commit Outputs
| Name | Type | Description |
| ------------- |:-------------: |:-------------: |
|*hash* | `string` |Commit hash |
## git.#CurrentBranch ## git.#CurrentBranch
Get the name of the current checked out branch or tag Get the name of the current checked out branch or tag
@ -26,6 +48,16 @@ Get the name of the current checked out branch or tag
| ------------- |:-------------: |:-------------: | | ------------- |:-------------: |:-------------: |
|*name* | `string` |Git branch name | |*name* | `string` |Git branch name |
## git.#Image
### git.#Image Inputs
_No input._
### git.#Image Outputs
_No output._
## git.#Repository ## git.#Repository
A git repository A git repository
@ -34,7 +66,7 @@ A git repository
| Name | Type | Description | | Name | Type | Description |
| ------------- |:-------------: |:-------------: | | ------------- |:-------------: |:-------------: |
|*remote* | `string` |Git remote. Example: `"https://github.com/dagger/dagger"` | |*remote* | `string` |Git remote link |
|*ref* | `string` |Git ref: can be a commit, tag or branch. Example: "main" | |*ref* | `string` |Git ref: can be a commit, tag or branch. Example: "main" |
|*subdir* | `*null \| string` |(optional) Subdirectory | |*subdir* | `*null \| string` |(optional) Subdirectory |
|*authToken* | `dagger.#Secret` |(optional) Add Personal Access Token | |*authToken* | `dagger.#Secret` |(optional) Add Personal Access Token |

View File

@ -0,0 +1,26 @@
plan:
package: ./git/tests/commit
name: git-commit
inputs:
TestAuthToken:
secret: ENC[AES256_GCM,data:OB4CfijwCnsvmvVhbV9BNlkhmuWtv1hu/nd7+E8+5tCBcXLbwOaMYQ==,iv:6+U+r/mmO07moQoitAr8Fm7bdTErJ5Ij3J21xIbEqIg=,tag:gmWLMFrVU+5l2O2pvb4Rug==,type:str]
sops:
kms: []
gcp_kms: []
azure_kv: []
hc_vault: []
age:
- recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk
enc: |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBnWkIxUExFTnQ2VS9pSHVv
Ykd6eUl4dlRQeU9YRGtLQWhPNExZakZORG5VCkVLN3NnNlNxZ0hmQW9TRGU5WXpN
UmYzZE1BUWZ1WDlPWjA2dURIbDA1bGcKLS0tIHBkR3c2U3IxY2w5Z0YxOWxLRkVv
M3RnUDF5QlhhZUV4NHF5ZWhkcHVrNmcKUJIummOk3FX1Bert7gaMtbMpbosIf/d3
HBATJRng4VNmcSimSh14pDRxyW0NdIPA+oL4tidwLVbQQv/74+IGKg==
-----END AGE ENCRYPTED FILE-----
lastmodified: "2021-09-02T10:18:20Z"
mac: ENC[AES256_GCM,data:RoWci1iCDYMxmK5BIoB822G6U9bcffOGIxMkvY560N8qxjItEb/Syvboa9MOjzDUhNPwxpaUIklzra0sy3u57iRBVZPfJ8tH9WKSQYeQv0ObtBXLAOVv1o8VJQbAd0WFNzZ7K0SSP8hUKQieDRvvIOlCFlNzJs6atrY9q/GEj4o=,iv:MOWALgh400eQSateXVDKY1Xrq/HFaVN+B0yoJxD4X4o=,tag:L0Bz7tD6gUOr9LsPPA4Plw==,type:str]
pgp: []
encrypted_suffix: secret
version: 3.7.1

View File

@ -0,0 +1,2 @@
# dagger state
state/**

View File

@ -1,6 +1,6 @@
plan: plan:
package: ./git/tests package: ./git/tests/git
name: git name: git-repo
inputs: inputs:
TestPAT: TestPAT:
secret: ENC[AES256_GCM,data:7s1tSIpIDNBhAFupdjb7KtPbjKrCd5tXupr3RQF2N3Xu5XGuTZMgoQ==,iv:I+SVYLnjgMffvNg6BMB6m1lj+VVH5sDK0aIEAWPcyLY=,tag:TcfJ6LVps8dXVZGZy3T2ew==,type:str] secret: ENC[AES256_GCM,data:7s1tSIpIDNBhAFupdjb7KtPbjKrCd5tXupr3RQF2N3Xu5XGuTZMgoQ==,iv:I+SVYLnjgMffvNg6BMB6m1lj+VVH5sDK0aIEAWPcyLY=,tag:TcfJ6LVps8dXVZGZy3T2ew==,type:str]
@ -19,8 +19,8 @@ sops:
TmhJNisyamw3d244aGVJSEVFVUVLZGsKvd+nowA0CLXQbdvyI4J0lBjs9vdISWlo TmhJNisyamw3d244aGVJSEVFVUVLZGsKvd+nowA0CLXQbdvyI4J0lBjs9vdISWlo
gGvR49uul3Z8raVWXFUzsyQ8xTvYNg0ovynFG2KdagSKr1DlhKMBEQ== gGvR49uul3Z8raVWXFUzsyQ8xTvYNg0ovynFG2KdagSKr1DlhKMBEQ==
-----END AGE ENCRYPTED FILE----- -----END AGE ENCRYPTED FILE-----
lastmodified: "2021-08-26T13:44:11Z" lastmodified: "2021-09-01T08:40:49Z"
mac: ENC[AES256_GCM,data:ttmpbzhrVFEGh/oJF4TtMvf99rutPBbzp9cIaqakIl+5nxqOkuAakgvf7IIMBG235zdyMvIXZZh6NLYG51PZA1hKNMg5Pqqba9GOSvFCHasWzNJ3pi5SLBGD02ivDfkSMbEHeOCUhnG1X4LxkYL9j+fb4tQt1Btv1hiIAcIa+eY=,iv:WxuW+0yJYtNqAB0y1nji9c3lzn4Pftir8uZojcdphng=,tag:yvcIJxkuqOmCfXoyEnGWow==,type:str] mac: ENC[AES256_GCM,data:UgryTszQf7YgFfXL8YhzJekPIOymtd8adyijOp6AQW8JGXu989JtlXeUKvLpRkbyjZD8+oTTX1iK7Ssj/hJKgo0yklLc7O9sSQGhxjo/3WYTcKUcHd5Tj9yoZp+RFH1qZxpnkAexZoM2rwOT9FAhdV3/AJV2ekY2Qe3JbrGJr6s=,iv:DXiIE1oNDPGhXcL2aPXA2TcvY6Jee4mFypq3bELnAx4=,tag:qfHMnpGr6yFLSQHqIvyglA==,type:str]
pgp: [] pgp: []
encrypted_suffix: secret encrypted_suffix: secret
version: 3.7.1 version: 3.7.1

89
stdlib/git/commit.cue Normal file
View File

@ -0,0 +1,89 @@
package git
import (
"alpha.dagger.io/dagger"
"alpha.dagger.io/os"
)
// Commit & push to git repository
#Commit: {
// Git repository
repository: {
// Repository source code
source: dagger.#Artifact
// Repository remote URL
remote: dagger.#Input & {string}
// Authentication token (PAT or password)
authToken: dagger.#Input & {*null | dagger.#Secret}
// Git branch
branch: dagger.#Input & {string}
}
// Username
name: dagger.#Input & {string}
// Email
email: dagger.#Input & {string}
// Commit message
message: dagger.#Input & {string}
// Content to commit
content: dagger.#Artifact
// Force push options
force: dagger.#Input & {*false | bool}
_ctr: os.#Container & {
image: #Image
command: #"""
# Move changes into repository
mv /input/content/* .
# Commit changes
git add .
git -c user.name="$USER_NAME" -c user.email="$USER_EMAIL" commit -m "$COMMIT_MESSAGE"
# Push
git push "$OPT_FORCE" -u "$GIT_REMOTE" HEAD:refs/heads/"$GIT_BRANCH"
# Retrieve commit hash
git rev-parse --verify HEAD | tr -d '\n' > /commit.txt
"""#
dir: "/input/repo"
mount: {
"/input/repo": from: repository.source
"/input/content": from: content
}
env: {
USER_NAME: name
USER_EMAIL: email
COMMIT_MESSAGE: message
GIT_BRANCH: repository.branch
GIT_REMOTE: repository.remote
if force {
OPT_FORCE: "-f"
}
}
if repository.authToken != null {
env: GIT_ASKPASS: "/get_authToken"
files: "/get_authToken": {
content: "cat /secrets/authToken"
mode: 0o500
}
secret: "/secrets/authToken": repository.authToken
}
}
// Commit hash
hash: {
os.#File & {
from: _ctr
path: "/commit.txt"
}
}.contents & dagger.#Output
}

View File

@ -9,8 +9,7 @@ import (
// A git repository // A git repository
#Repository: { #Repository: {
// Git remote. // Git remote link
// Example: `"https://github.com/dagger/dagger"`
remote: dagger.#Input & {string} remote: dagger.#Input & {string}
// Git ref: can be a commit, tag or branch. // Git ref: can be a commit, tag or branch.

9
stdlib/git/image.cue Normal file
View File

@ -0,0 +1,9 @@
package git
import (
"alpha.dagger.io/alpine"
)
#Image: alpine.#Image & {
package: git: true
}

View File

@ -0,0 +1,79 @@
package git
import (
"alpha.dagger.io/alpine"
"alpha.dagger.io/dagger"
"alpha.dagger.io/os"
"alpha.dagger.io/random"
)
TestAuthToken: dagger.#Input & {dagger.#Secret}
TestRemote: dagger.#Input & {*"https://github.com/dagger/test.git" | string}
TestRepository: #Repository & {
remote: TestRemote
ref: "main"
keepGitDir: true
authToken: TestAuthToken
}
TestData: {
random.#String & {
seed: "git-commit"
}
}.out
TestFile: os.#Dir & {
from: os.#Container & {
image: alpine.#Image
command: #"""
mkdir -p /output
echo "$MESSAGE" >> /output/test.md
"""#
env: MESSAGE: TestData
}
path: "/output"
}
TestCommit: #Commit & {
repository: {
remote: TestRemote
authToken: TestAuthToken
source: TestRepository
branch: "ci/test-commit"
}
content: TestFile
message: "This is a commit from the CI to test the repository"
name: "Dagger CI"
email: "daggerci@dagger.io"
force: true
}
TestCheck: {
_TestRepo: #Repository & {
remote: TestRemote
ref: "ci/test-commit"
keepGitDir: true
authToken: TestAuthToken
}
_ctr: os.#Container & {
image: #Image
command: #"""
# Check commit
git rev-parse --verify HEAD | grep "$GIT_HASH"
# Check file
echo -n "$EXPECTED_MESSAGE" >> expect.md
diff test.md expect.md
"""#
dir: "/input/repo"
mount: "/input/repo": from: _TestRepo
env: {
EXPECTED_MESSAGE: TestData
// Force dependency with interpolation
GIT_HASH: "\(TestCommit.hash)"
}
}
}

View File

@ -5,28 +5,27 @@ import (
"alpha.dagger.io/alpine" "alpha.dagger.io/alpine"
"alpha.dagger.io/dagger" "alpha.dagger.io/dagger"
"alpha.dagger.io/git"
"alpha.dagger.io/os" "alpha.dagger.io/os"
) )
repo: git.#Repository & { repo: #Repository & {
remote: "https://github.com/blocklayerhq/acme-clothing.git" remote: "https://github.com/blocklayerhq/acme-clothing.git"
ref: "master" ref: "master"
keepGitDir: true keepGitDir: true
} }
repoSubDir: git.#Repository & { repoSubDir: #Repository & {
remote: "https://github.com/dagger/examples.git" remote: "https://github.com/dagger/examples.git"
ref: "main" ref: "main"
subdir: "todoapp" subdir: "todoapp"
keepGitDir: true keepGitDir: true
} }
branch: git.#CurrentBranch & { branch: #CurrentBranch & {
repository: repo repository: repo
} }
tagsList: git.#Tags & { tagsList: #Tags & {
repository: repo repository: repo
} }
@ -79,7 +78,7 @@ TestCurrentTags: os.#Container & {
// Test fetching a private repo // Test fetching a private repo
TestPAT: dagger.#Input & {dagger.#Secret} TestPAT: dagger.#Input & {dagger.#Secret}
privateRepo: git.#Repository & { privateRepo: #Repository & {
remote: "https://github.com/dagger/dagger.git" remote: "https://github.com/dagger/dagger.git"
ref: "main" ref: "main"
keepGitDir: true keepGitDir: true

View File

@ -54,6 +54,12 @@ import (
// Safely mount secrets (in cleartext) as non-persistent files // Safely mount secrets (in cleartext) as non-persistent files
secret: [string]: dagger.#Secret secret: [string]: dagger.#Secret
// Write file in the container
files: [string]: {
content: string
mode: int | *0o644
}
// Mount persistent cache directories // Mount persistent cache directories
cache: [string]: true cache: [string]: true
@ -86,6 +92,13 @@ import (
"dir": dir "dir": dir
} }
}, },
for dest, file in files {
op.#WriteFile & {
content: file.content
mode: file.mode
"dest": dest
}
},
// Execute main command with volumes // Execute main command with volumes
if command != "" { if command != "" {
op.#Exec & { op.#Exec & {

View File

@ -40,7 +40,11 @@ setup() {
} }
@test "git" { @test "git" {
dagger -e git up # Fetch repo
dagger -e git-repo up
# Commit & push
dagger -e git-commit up
} }
@test "os.#Container" { @test "os.#Container" {