Add more thorough custom transformer instructions

This commit is contained in:
Luke Engle
2022-09-20 11:49:00 -07:00
parent ef4b59aea4
commit d58cd99cf1
2 changed files with 49 additions and 14 deletions
+23 -11
View File
@@ -77,7 +77,29 @@ Now you can begin to write the custom transformer. Custom transformers use a DSL
touch transformers.rb && code transformers.rb
```
Next, you will define a `transform` method for the `artifacts.terraform` identifier by adding the following code to `transformers.rb`:
To build this custom transformer, you first need to inspect the `item` keyword to programmatically use the file path of the terraform json in the `actions/upload-artifact@v3` step.
To do this, you will print `item` to the console. You can achieve this by adding the following custom transformer to `transformers.rb`:
```ruby
transform "artifacts.terraform" do |item|
puts "This is the item: #{item}"
end
```
The `transform` method can use any valid ruby syntax and should return a `Hash` that represents the YAML that should be generated for a given step. Valet will use this method to convert a step with the provided identifier and will use the `item` parameter for the original values configured in GitLab.
Now, we can perform a `dry-run` command with the `--custom-transformers` CLI option. The output of the `dry-run` command should look similar to this:
```console
$ gh valet dry-run gitlab --output-dir tmp --namespace valet --project terraform-example --custom-transformers transformers.rb
[2022-09-20 17:47:55] Logs: 'tmp/log/valet-20220920-174755.log'
This is the item: $PLAN_JSON
[2022-09-20 17:47:56] Output file(s):
[2022-09-20 17:47:56] tmp/valet/terraform-example.yml
```
Now that you know the data structure of `item`, you can access the file path programmatically by editing the custom transformer to the following:
```ruby
transform "artifacts.terraform" do |item|
@@ -90,8 +112,6 @@ transform "artifacts.terraform" do |item|
end
```
This method can use any valid ruby syntax and should return a `Hash` that represents the YAML that should be generated for a given step. Valet will use this method to convert a step with the provided identifier and will use the `item` parameter for the original values configured in GitLab.
Now you can perform another `dry-run` command and use the `--custom-transformers` CLI option to provide this custom transformer. Run the following command within your codespace terminal:
```bash
@@ -107,14 +127,6 @@ The converted workflow that is generated by the above command will now use the c
+ path: "$PLAN_JSON"
```
_Note_: If you were unsure what the data structure of `item` was, you could use the following code in the custom transformer to print `item` to the console:
```ruby
transform "artifacts.terraform" do |item|
puts item
end
```
## Custom transformers for environment variables
You can also use custom transformers to edit the values of environment variables in converted workflows. In this example, you will update the `PLAN_JSON` environment variable to be `custom_plan.json` instead of `plan.json`.