Skip to content

Commit 4f128f4

Browse files
committed
Version 0.2.2: added support for all build options
1 parent 1a70cc2 commit 4f128f4

File tree

9 files changed

+250
-22
lines changed

9 files changed

+250
-22
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,36 @@ This tasks runs the `bicep build` command with an input path containing `.bicep`
3131

3232
This task takes only one `sourceDirectory` parameter input which is the path where the bicep file/files reside (can be a glob, or a single file).
3333

34-
## Sample YAML with the task
34+
## Sample YAML with the task (for multiple files)
3535

3636
```yaml
3737
steps:
3838
- task: BicepBuild@0
3939
inputs:
40+
process: 'multiple'
4041
sourceDirectory: '.\bicep_files\*.bicep'
42+
stdout: false # Note if stdout is true 'outputDirectory' will not be interpreted
43+
outputDirectory: '.\bicep_files\out' # Only when 'stdout' is false or not defined
44+
```
45+
46+
## Sample YAML with the task (for single file)
47+
48+
```yaml
49+
steps:
50+
- task: BicepBuild@0
51+
inputs:
52+
process: 'single'
53+
sourceFile: '.\bicep_files\sample1.bicep'
54+
stdout: false # Note if stdout is true 'outputDirectory' will not be interpreted
55+
outputFile: '.\bicep_files\sample1.out.json' # Only when 'stdout' is false or not defined and 'outputDirectory' is empty or not defined
4156
```
4257

4358
# Local Development
4459

4560
**Note:** [Bicep](https://github.com/Azure/bicep) must be installed in the local machine. [TypeScript](https://www.typescriptlang.org/download) must be also installed as a global package (`npm i typescript -g`).
4661

47-
1. Run `npm install` in the `src` directory.
48-
2. Run `tsc` in the `src` directory.
62+
1. Run `npm install` in the root directory.
63+
2. Run `npm run build` in the root directory.
4964
3. Define the needed agent environment parameters:
5065

5166
```powershell
@@ -59,20 +74,30 @@ export AGENT_TEMPDIRECTORY="/temp" # Or any other existing directory
5974
export AGENT_TOOLSDIRECTORY="/tools" # Or any other existing directory
6075
```
6176

62-
4. (Optional) Set variables for the tasks:
77+
4. (Optional) Set variables for the tasks (as you want to test):
6378

6479
```powershell
6580
# For PowerShell:
6681
$env:INPUT_VERSION = "0.3.1" # Or any other valid Bicep version
82+
$env:INPUT_PROCESS = "multiple" # Selection between 'multiple' or 'single' file(s) processing
6783
$env:INPUT_SOURCEDIRECTORY = "C:\bicep_files\*.bicep" # Or any other existing directory with bicep file(s)
84+
$env:INPUT_SOURCEFILE = "C:\bicep_files\sample1.bicep" # Or any other existing bicep file
85+
$env:INPUT_STDOUT = $false # To print the output to standard output (stdout) or not
86+
$env:INPUT_OUTPUTDIRECTORY = "C:\bicep_files\out" # Or any other existing directory to store the json generated file(s)
87+
$env:INPUT_OUTPUTFILE = "C:\bicep_files\sample1.out.json" # Or any other path/filename to store the generated file
6888
```
6989
```bash
7090
# For bash:
7191
export INPUT_VERSION="0.3.1" # Or any other valid Bicep version
92+
export INPUT_PROCESS = "multiple" # Selection between 'multiple' or 'single' file(s) processing
7293
export INPUT_SOURCEDIRECTORY="C:\bicep_files\*.bicep" # Or any other existing directory with bicep file(s)
94+
export INPUT_SOURCEFILE = "C:\bicep_files\sample1.bicep" # Or any other existing bicep file
95+
export INPUT_STDOUT = false # To print the output to standard output (stdout) or not
96+
export INPUT_OUTPUTDIRECTORY = "C:\bicep_files\out" # Or any other existing directory to store the json generated file(s)
97+
export INPUT_OUTPUTFILE = "C:\bicep_files\sample1.out.json" # Or any other path/filename to store the generated file
7398
```
7499

75-
5. Run `node install/index.js` and `node run/index.js` to execute the two tasks.
100+
5. Run `node src/install/index.js` and `node src/build/index.js` to execute the two tasks.
76101

77102
*Note:* the `bicep_files` directory containing `.bicep` files are only for development and testing purposes.
78103

azure-pipelines.yml

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,44 @@ stages:
1212
displayName: 'Install Bicep CLI test'
1313

1414
- task: BicepBuild@0
15-
displayName: 'Compile Sample .bicep files test'
15+
displayName: 'Compile Sample .bicep files test (defaults)'
1616
inputs:
1717
sourceDirectory: 'bicep_files/**.bicep'
18+
19+
- task: BicepBuild@0
20+
displayName: 'Compile Sample .bicep files test (with output directory)'
21+
inputs:
22+
process: 'multiple'
23+
sourceDirectory: 'bicep_files/**.bicep'
24+
outputDirectory: 'bicep_files/out'
25+
26+
- task: BicepBuild@0
27+
displayName: 'Compile Sample .bicep files test (with stdout output)'
28+
inputs:
29+
process: 'multiple'
30+
sourceDirectory: 'bicep_files/**.bicep'
31+
stdout: true
32+
33+
- task: BicepBuild@0
34+
displayName: 'Compile Sample .bicep file test (defaults)'
35+
inputs:
36+
process: 'single'
37+
sourceFile: './bicep_files/sample1.bicep'
38+
39+
- task: BicepBuild@0
40+
displayName: 'Compile Sample .bicep file test (with output filename)'
41+
inputs:
42+
process: 'single'
43+
sourceFile: './bicep_files/sample1.bicep'
44+
stdout: false
45+
outputFile: './bicep_files/sample1.out.json'
46+
47+
- task: BicepBuild@0
48+
displayName: 'Compile Sample .bicep file test (with stdout output)'
49+
inputs:
50+
process: 'single'
51+
sourceFile: './bicep_files/sample1.bicep'
52+
stdout: true
1853

1954
- script: 'ls -la bicep_files'
2055
displayName: 'List source and generated files'
@@ -40,10 +75,45 @@ stages:
4075
displayName: 'Install Bicep CLI test'
4176

4277
- task: BicepBuild@0
43-
displayName: 'Compile Sample .bicep files test'
78+
displayName: 'Compile Sample .bicep files test (defaults)'
4479
inputs:
4580
sourceDirectory: 'bicep_files/**.bicep'
4681

82+
- task: BicepBuild@0
83+
displayName: 'Compile Sample .bicep files test (with output directory)'
84+
inputs:
85+
process: 'multiple'
86+
sourceDirectory: 'bicep_files\**.bicep'
87+
outputDirectory: 'bicep_files\out'
88+
89+
- task: BicepBuild@0
90+
displayName: 'Compile Sample .bicep files test (with stdout output)'
91+
inputs:
92+
process: 'multiple'
93+
sourceDirectory: 'bicep_files\**.bicep'
94+
stdout: true
95+
96+
- task: BicepBuild@0
97+
displayName: 'Compile Sample .bicep file test (defaults)'
98+
inputs:
99+
process: 'single'
100+
sourceFile: '.\bicep_files\sample1.bicep'
101+
102+
- task: BicepBuild@0
103+
displayName: 'Compile Sample .bicep file test (with output filename)'
104+
inputs:
105+
process: 'single'
106+
sourceFile: '.\bicep_files\sample1.bicep'
107+
stdout: false
108+
outputFile: '.\bicep_files\sample1.out.json'
109+
110+
- task: BicepBuild@0
111+
displayName: 'Compile Sample .bicep file test (with stdout output)'
112+
inputs:
113+
process: 'single'
114+
sourceFile: '.\bicep_files\sample1.bicep'
115+
stdout: true
116+
47117
- script: 'dir bicep_files'
48118
displayName: 'List source and generated files'
49119

@@ -68,9 +138,45 @@ stages:
68138
displayName: 'Install Bicep CLI test'
69139

70140
- task: BicepBuild@0
71-
displayName: 'Compile Sample .bicep files test'
141+
displayName: 'Compile Sample .bicep files test (defaults)'
142+
inputs:
143+
sourceDirectory: 'bicep_files/**.bicep'
144+
145+
- task: BicepBuild@0
146+
displayName: 'Compile Sample .bicep files test (with output directory)'
147+
inputs:
148+
process: 'multiple'
149+
sourceDirectory: 'bicep_files/**.bicep'
150+
outputDirectory: 'bicep_files/out'
151+
152+
- task: BicepBuild@0
153+
displayName: 'Compile Sample .bicep files test (with stdout output)'
72154
inputs:
155+
process: 'multiple'
73156
sourceDirectory: 'bicep_files/**.bicep'
157+
stdout: true
158+
159+
- task: BicepBuild@0
160+
displayName: 'Compile Sample .bicep file test (defaults)'
161+
inputs:
162+
process: 'single'
163+
sourceFile: './bicep_files/sample1.bicep'
164+
165+
- task: BicepBuild@0
166+
displayName: 'Compile Sample .bicep file test (with output filename)'
167+
inputs:
168+
process: 'single'
169+
sourceFile: './bicep_files/sample1.bicep'
170+
stdout: false
171+
outputFile: './bicep_files/sample1.out.json'
172+
173+
- task: BicepBuild@0
174+
displayName: 'Compile Sample .bicep file test (with stdout output)'
175+
inputs:
176+
process: 'single'
177+
sourceFile: './bicep_files/sample1.bicep'
178+
stdout: true
179+
74180

75181
- script: 'ls -la bicep_files'
76182
displayName: 'List source and generated files'

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-devops-bicep-task",
3-
"version": "1.0.0",
3+
"version": "0.2.2",
44
"description": "Tasks for installing Bicep CLI and running Bicep CLI build commands",
55
"main": "index.js",
66
"scripts": {

src/install/task.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"category": "Azure Pipelines",
1616
"version": {
1717
"Major": 0,
18-
"Minor": 1,
19-
"Patch": 3
18+
"Minor": 2,
19+
"Patch": 2
2020
},
2121
"instanceNameFormat": "Install Bicep CLI",
2222
"inputs": [

src/run/index.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as path from 'path';
2+
import * as fs from 'fs';
23
import { platform } from 'os';
34
import * as taskLib from 'azure-pipelines-task-lib/task';
45
import * as toolLib from 'azure-pipelines-tool-lib/tool';
@@ -11,9 +12,57 @@ export function getFilesList(sourceDirectory: string): string[] {
1112
async function run() {
1213
taskLib.setResourcePath(path.join(__dirname, 'task.json'));
1314
try {
14-
const sourceDirectory: string | undefined = taskLib.getInput('sourceDirectory', true);
15-
if (!sourceDirectory) {
16-
throw new Error("The variable 'sourceDirectory' is mandatory.");
15+
const processType: string | undefined = taskLib.getInput('process', false);
16+
const sourceDirectory: string | undefined = taskLib.getInput('sourceDirectory', false);
17+
const sourceFile: string | undefined = taskLib.getInput('sourceFile', false);
18+
const outputDirectory: string | undefined = taskLib.getInput('outputDirectory', false);
19+
const outputFile: string | undefined = taskLib.getInput('outputFile', false);
20+
const stdoutEnabled: boolean | undefined = taskLib.getBoolInput('stdout', false);
21+
22+
const additionalArgsByInputs: string[] = [];
23+
24+
if (!sourceDirectory && processType === 'multiple') {
25+
throw new Error("The variable 'sourceDirectory' is mandatory when process is 'multiple'.");
26+
}
27+
28+
if (!sourceFile && processType === 'single') {
29+
throw new Error("The variable 'sourceFile' is mandatory when process is 'single'.");
30+
}
31+
32+
let files: string[] = [];
33+
if (sourceDirectory && processType === 'multiple') {
34+
files = getFilesList(sourceDirectory);
35+
} else if (sourceFile && processType === 'single') {
36+
files.push(sourceFile);
37+
} else {
38+
throw new Error(
39+
"The variable 'sourceDirectory' is mandatory when process is 'multiple' and 'sourceFile' when process is 'single'.",
40+
);
41+
}
42+
43+
if (stdoutEnabled) {
44+
taskLib.debug('Results will be printed to standard output');
45+
additionalArgsByInputs.push('--stdout');
46+
} else if (outputFile && processType === 'single') {
47+
additionalArgsByInputs.push('--outfile');
48+
additionalArgsByInputs.push(outputFile);
49+
const outputFilePath = path.dirname(outputFile);
50+
if (!fs.existsSync(outputFilePath)) {
51+
taskLib.debug(`Output filepath not exists. Creating folder: '${outputFilePath}'`);
52+
fs.mkdirSync(outputFilePath, { recursive: true });
53+
}
54+
} else if (outputDirectory) {
55+
taskLib.debug(`Output files will be stored in '${outputDirectory}'`);
56+
additionalArgsByInputs.push('--outdir');
57+
additionalArgsByInputs.push(outputDirectory);
58+
if (!fs.existsSync(outputDirectory)) {
59+
taskLib.debug(`Output directory not exists. Creating folder: '${outputDirectory}'`);
60+
fs.mkdirSync(outputDirectory, { recursive: true });
61+
}
62+
} else {
63+
taskLib.debug(
64+
`No output directory specified... Output files will be stored in source directory: '${sourceDirectory}'`,
65+
);
1766
}
1867

1968
let bicepTool: any;
@@ -41,13 +90,12 @@ async function run() {
4190
' before this task or ensure Bicep is installed and available in PATH in the agent',
4291
);
4392
}
44-
const files = getFilesList(sourceDirectory);
4593

4694
taskLib.debug('Running Bicep build...');
4795

4896
if (bicepTool) {
4997
files.forEach((file: string) => {
50-
const args = ['build', file];
98+
const args = ['build', file, ...additionalArgsByInputs];
5199
const bicepProcess = taskLib.tool(bicepTool).arg(args).execSync();
52100

53101
if (bicepProcess.code !== 0) {

src/run/task.json

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,67 @@
1515
"category": "Azure Pipelines",
1616
"version": {
1717
"Major": 0,
18-
"Minor": 1,
19-
"Patch": 3
18+
"Minor": 2,
19+
"Patch": 2
2020
},
2121
"instanceNameFormat": "Run Bicep CLI build command",
2222
"inputs": [
23+
{
24+
"name": "process",
25+
"type": "pickList",
26+
"label": "Directory of files or single file",
27+
"defaultValue": "multiple",
28+
"required": true,
29+
"helpMarkDown": "Choose between an input directory of .bicep files to process or a single file",
30+
"options": {
31+
"multiple": "Directory",
32+
"single": "Single file"
33+
},
34+
"properties": {
35+
"EditableOptions": "False"
36+
}
37+
},
2338
{
2439
"type": "filePath",
2540
"name": "sourceDirectory",
2641
"label": "Source Directory for .bicep file(s)",
2742
"defaultValue": "$(System.DefaultWorkingDirectory)",
2843
"required": true,
29-
"helpMarkDown": "The source directory for .bicep file(s) to compile"
44+
"helpMarkDown": "The source directory for .bicep file(s) to compile",
45+
"visibleRule": "process = multiple"
46+
},
47+
{
48+
"type": "filePath",
49+
"name": "sourceFile",
50+
"label": "Source .bicep file",
51+
"defaultValue": "",
52+
"required": true,
53+
"helpMarkDown": "The source .bicep file to compile",
54+
"visibleRule": "process = single"
55+
},
56+
{
57+
"type": "boolean",
58+
"name": "stdout",
59+
"label": "Print the output to standard output (stdout)",
60+
"required": false,
61+
"default": false,
62+
"helpMarkDown": "To print the output to standard output (stdout) or not"
63+
},
64+
{
65+
"type": "filePath",
66+
"name": "outputDirectory",
67+
"label": "Output directory for .json generated file(s) by Bicep",
68+
"required": false,
69+
"visibleRule": "stdout = false",
70+
"helpMarkDown": "The output directory for .json generated file(s) by Bicep (will have preference over 'filePath')"
71+
},
72+
{
73+
"type": "filePath",
74+
"name": "outputFile",
75+
"label": "Output filename for the .json generated file by Bicep",
76+
"required": false,
77+
"visibleRule": "stdout = false && process = single",
78+
"helpMarkDown": "The output filename for the .json generated file by Bicep"
3079
}
3180
],
3281
"execution": {

0 commit comments

Comments
 (0)