diff --git a/docs/reference/universe/git.md b/docs/reference/universe/git.md index f9107ac7..59990600 100644 --- a/docs/reference/universe/git.md +++ b/docs/reference/universe/git.md @@ -10,6 +10,28 @@ Git operations 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 Get the name of the current checked out branch or tag @@ -26,19 +48,29 @@ Get the name of the current checked out branch or tag | ------------- |:-------------: |:-------------: | |*name* | `string` |Git branch name | +## git.#Image + +### git.#Image Inputs + +_No input._ + +### git.#Image Outputs + +_No output._ + ## git.#Repository A git repository ### git.#Repository Inputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*remote* | `string` |Git remote. Example: `"https://github.com/dagger/dagger"` | -|*ref* | `string` |Git ref: can be a commit, tag or branch. Example: "main" | -|*subdir* | `*null \| string` |(optional) Subdirectory | -|*authToken* | `dagger.#Secret` |(optional) Add Personal Access Token | -|*authHeader* | `dagger.#Secret` |(optional) Add OAuth Token | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*remote* | `string` |Git remote link | +|*ref* | `string` |Git ref: can be a commit, tag or branch. Example: "main" | +|*subdir* | `*null \| string` |(optional) Subdirectory | +|*authToken* | `dagger.#Secret` |(optional) Add Personal Access Token | +|*authHeader* | `dagger.#Secret` |(optional) Add OAuth Token | ### git.#Repository Outputs diff --git a/stdlib/.dagger/env/git/.gitignore b/stdlib/.dagger/env/git-commit/.gitignore similarity index 100% rename from stdlib/.dagger/env/git/.gitignore rename to stdlib/.dagger/env/git-commit/.gitignore diff --git a/stdlib/.dagger/env/git-commit/values.yaml b/stdlib/.dagger/env/git-commit/values.yaml new file mode 100644 index 00000000..b68716d0 --- /dev/null +++ b/stdlib/.dagger/env/git-commit/values.yaml @@ -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 diff --git a/stdlib/.dagger/env/git-repo/.gitignore b/stdlib/.dagger/env/git-repo/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/stdlib/.dagger/env/git-repo/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/stdlib/.dagger/env/git/values.yaml b/stdlib/.dagger/env/git-repo/values.yaml similarity index 70% rename from stdlib/.dagger/env/git/values.yaml rename to stdlib/.dagger/env/git-repo/values.yaml index 164b56f2..c07ef0da 100644 --- a/stdlib/.dagger/env/git/values.yaml +++ b/stdlib/.dagger/env/git-repo/values.yaml @@ -1,6 +1,6 @@ plan: - package: ./git/tests -name: git + package: ./git/tests/git +name: git-repo inputs: TestPAT: secret: ENC[AES256_GCM,data:7s1tSIpIDNBhAFupdjb7KtPbjKrCd5tXupr3RQF2N3Xu5XGuTZMgoQ==,iv:I+SVYLnjgMffvNg6BMB6m1lj+VVH5sDK0aIEAWPcyLY=,tag:TcfJ6LVps8dXVZGZy3T2ew==,type:str] @@ -19,8 +19,8 @@ sops: TmhJNisyamw3d244aGVJSEVFVUVLZGsKvd+nowA0CLXQbdvyI4J0lBjs9vdISWlo gGvR49uul3Z8raVWXFUzsyQ8xTvYNg0ovynFG2KdagSKr1DlhKMBEQ== -----END AGE ENCRYPTED FILE----- - lastmodified: "2021-08-26T13:44:11Z" - mac: ENC[AES256_GCM,data:ttmpbzhrVFEGh/oJF4TtMvf99rutPBbzp9cIaqakIl+5nxqOkuAakgvf7IIMBG235zdyMvIXZZh6NLYG51PZA1hKNMg5Pqqba9GOSvFCHasWzNJ3pi5SLBGD02ivDfkSMbEHeOCUhnG1X4LxkYL9j+fb4tQt1Btv1hiIAcIa+eY=,iv:WxuW+0yJYtNqAB0y1nji9c3lzn4Pftir8uZojcdphng=,tag:yvcIJxkuqOmCfXoyEnGWow==,type:str] + lastmodified: "2021-09-01T08:40:49Z" + mac: ENC[AES256_GCM,data:UgryTszQf7YgFfXL8YhzJekPIOymtd8adyijOp6AQW8JGXu989JtlXeUKvLpRkbyjZD8+oTTX1iK7Ssj/hJKgo0yklLc7O9sSQGhxjo/3WYTcKUcHd5Tj9yoZp+RFH1qZxpnkAexZoM2rwOT9FAhdV3/AJV2ekY2Qe3JbrGJr6s=,iv:DXiIE1oNDPGhXcL2aPXA2TcvY6Jee4mFypq3bELnAx4=,tag:qfHMnpGr6yFLSQHqIvyglA==,type:str] pgp: [] encrypted_suffix: secret version: 3.7.1 diff --git a/stdlib/git/commit.cue b/stdlib/git/commit.cue new file mode 100644 index 00000000..2b424a27 --- /dev/null +++ b/stdlib/git/commit.cue @@ -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 +} diff --git a/stdlib/git/git.cue b/stdlib/git/git.cue index 8a12177d..5a7e338e 100644 --- a/stdlib/git/git.cue +++ b/stdlib/git/git.cue @@ -9,8 +9,7 @@ import ( // A git repository #Repository: { - // Git remote. - // Example: `"https://github.com/dagger/dagger"` + // Git remote link remote: dagger.#Input & {string} // Git ref: can be a commit, tag or branch. diff --git a/stdlib/git/image.cue b/stdlib/git/image.cue new file mode 100644 index 00000000..38da5a78 --- /dev/null +++ b/stdlib/git/image.cue @@ -0,0 +1,9 @@ +package git + +import ( + "alpha.dagger.io/alpine" +) + +#Image: alpine.#Image & { + package: git: true +} diff --git a/stdlib/git/tests/commit/commit.cue b/stdlib/git/tests/commit/commit.cue new file mode 100644 index 00000000..90908c5b --- /dev/null +++ b/stdlib/git/tests/commit/commit.cue @@ -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)" + } + } +} diff --git a/stdlib/git/tests/git.cue b/stdlib/git/tests/git/git.cue similarity index 90% rename from stdlib/git/tests/git.cue rename to stdlib/git/tests/git/git.cue index 3f00ca4b..ca7a675c 100644 --- a/stdlib/git/tests/git.cue +++ b/stdlib/git/tests/git/git.cue @@ -5,28 +5,27 @@ import ( "alpha.dagger.io/alpine" "alpha.dagger.io/dagger" - "alpha.dagger.io/git" "alpha.dagger.io/os" ) -repo: git.#Repository & { +repo: #Repository & { remote: "https://github.com/blocklayerhq/acme-clothing.git" ref: "master" keepGitDir: true } -repoSubDir: git.#Repository & { +repoSubDir: #Repository & { remote: "https://github.com/dagger/examples.git" ref: "main" subdir: "todoapp" keepGitDir: true } -branch: git.#CurrentBranch & { +branch: #CurrentBranch & { repository: repo } -tagsList: git.#Tags & { +tagsList: #Tags & { repository: repo } @@ -79,7 +78,7 @@ TestCurrentTags: os.#Container & { // Test fetching a private repo TestPAT: dagger.#Input & {dagger.#Secret} -privateRepo: git.#Repository & { +privateRepo: #Repository & { remote: "https://github.com/dagger/dagger.git" ref: "main" keepGitDir: true diff --git a/stdlib/os/container.cue b/stdlib/os/container.cue index 4e537205..20f57fe6 100644 --- a/stdlib/os/container.cue +++ b/stdlib/os/container.cue @@ -54,6 +54,12 @@ import ( // Safely mount secrets (in cleartext) as non-persistent files secret: [string]: dagger.#Secret + // Write file in the container + files: [string]: { + content: string + mode: int | *0o644 + } + // Mount persistent cache directories cache: [string]: true @@ -86,6 +92,13 @@ import ( "dir": dir } }, + for dest, file in files { + op.#WriteFile & { + content: file.content + mode: file.mode + "dest": dest + } + }, // Execute main command with volumes if command != "" { op.#Exec & { diff --git a/stdlib/universe.bats b/stdlib/universe.bats index add1cc19..b0470e94 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -40,7 +40,11 @@ setup() { } @test "git" { - dagger -e git up + # Fetch repo + dagger -e git-repo up + + # Commit & push + dagger -e git-commit up } @test "os.#Container" {