Skip to content

Commit 89d547f

Browse files
committed
saving updates to workflow creation
1 parent ae93a58 commit 89d547f

3 files changed

Lines changed: 103 additions & 23 deletions

File tree

src/templates/azure-devops/extract-pipeline.ts

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,63 @@ variables:
3838
- group: apim-common
3939
4040
steps:
41+
- checkout: self
42+
persistCredentials: true
43+
fetchDepth: 2
44+
4145
- task: UseNode@1
4246
displayName: 'Setup Node.js'
4347
inputs:
44-
versionSpec: '22.x'
48+
version: '22.x'
4549
46-
- script: npm ci
50+
- script: |
51+
if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then
52+
npm ci
53+
else
54+
npm install --no-audit --no-fund
55+
fi
4756
displayName: 'Install dependencies'
4857
58+
- bash: |
59+
echo "##vso[task.setvariable variable=RESOURCE_GROUP]\${{ parameters.resourceGroup }}"
60+
echo "##vso[task.setvariable variable=SERVICE_NAME]\${{ parameters.serviceName }}"
61+
displayName: 'Set parameters as variables'
62+
63+
- bash: |
64+
RESOURCE_GROUP='$(RESOURCE_GROUP)'
65+
SERVICE_NAME='$(SERVICE_NAME)'
66+
SERVICE_CONNECTION='$(AZURE_SERVICE_CONNECTION)'
67+
SUBSCRIPTION_ID='$(AZURE_SUBSCRIPTION_ID)'
68+
69+
is_unresolved() {
70+
local value="$1"
71+
case "$value" in
72+
""|\$\(*\)) return 0 ;;
73+
*) return 1 ;;
74+
esac
75+
}
76+
77+
if is_unresolved "$RESOURCE_GROUP"; then
78+
echo "##vso[task.logissue type=error]RESOURCE_GROUP was not resolved. Verify apim-common defines APIM_RESOURCE_GROUP and is authorized for this pipeline."
79+
exit 2
80+
fi
81+
82+
if is_unresolved "$SERVICE_NAME"; then
83+
echo "##vso[task.logissue type=error]SERVICE_NAME was not resolved. Verify apim-common defines APIM_SERVICE_NAME and is authorized for this pipeline."
84+
exit 2
85+
fi
86+
87+
if is_unresolved "$SERVICE_CONNECTION"; then
88+
echo "##vso[task.logissue type=error]AZURE_SERVICE_CONNECTION was not resolved. Verify apim-common defines AZURE_SERVICE_CONNECTION and is authorized for this pipeline."
89+
exit 2
90+
fi
91+
92+
if is_unresolved "$SUBSCRIPTION_ID"; then
93+
echo "##vso[task.logissue type=error]AZURE_SUBSCRIPTION_ID was not resolved. Verify apim-common defines AZURE_SUBSCRIPTION_ID and is authorized for this pipeline."
94+
exit 2
95+
fi
96+
displayName: 'Validate required variables'
97+
4998
- task: AzureCLI@2
5099
displayName: 'Run APIM Extract (All APIs)'
51100
condition: eq('\${{ parameters.CONFIGURATION_YAML_PATH }}', 'Extract All APIs')
@@ -54,11 +103,25 @@ steps:
54103
scriptType: 'bash'
55104
scriptLocation: 'inlineScript'
56105
inlineScript: |
57-
npx apiops extract \\
58-
--resource-group \${{ parameters.resourceGroup }} \\
59-
--service-name \${{ parameters.serviceName }} \\
106+
SUBSCRIPTION_ID='$(AZURE_SUBSCRIPTION_ID)'
107+
108+
is_unresolved() {
109+
local value="$1"
110+
case "$value" in
111+
""|\$\(*\)) return 0 ;;
112+
*) return 1 ;;
113+
esac
114+
}
115+
116+
if is_unresolved "$SUBSCRIPTION_ID"; then
117+
echo "##vso[task.logissue type=error]AZURE_SUBSCRIPTION_ID was not resolved. Ensure apim-common is authorized and defines AZURE_SUBSCRIPTION_ID."
118+
exit 2
119+
fi
120+
npx @peterhauge/apiops-cli extract \\
121+
--resource-group "$(RESOURCE_GROUP)" \\
122+
--service-name "$(SERVICE_NAME)" \\
60123
--output ${config.artifactDir} \\
61-
--subscription-id $(AZURE_SUBSCRIPTION_ID)
124+
--subscription-id "$SUBSCRIPTION_ID"
62125
63126
- task: AzureCLI@2
64127
displayName: 'Run APIM Extract (With Configuration)'
@@ -68,12 +131,26 @@ steps:
68131
scriptType: 'bash'
69132
scriptLocation: 'inlineScript'
70133
inlineScript: |
71-
npx apiops extract \\
72-
--resource-group \${{ parameters.resourceGroup }} \\
73-
--service-name \${{ parameters.serviceName }} \\
134+
SUBSCRIPTION_ID='$(AZURE_SUBSCRIPTION_ID)'
135+
136+
is_unresolved() {
137+
local value="$1"
138+
case "$value" in
139+
""|\$\(*\)) return 0 ;;
140+
*) return 1 ;;
141+
esac
142+
}
143+
144+
if is_unresolved "$SUBSCRIPTION_ID"; then
145+
echo "##vso[task.logissue type=error]AZURE_SUBSCRIPTION_ID was not resolved. Ensure apim-common is authorized and defines AZURE_SUBSCRIPTION_ID."
146+
exit 2
147+
fi
148+
npx @peterhauge/apiops-cli extract \\
149+
--resource-group "$(RESOURCE_GROUP)" \\
150+
--service-name "$(SERVICE_NAME)" \\
74151
--output ${config.artifactDir} \\
75152
--filter configuration.extractor.yaml \\
76-
--subscription-id $(AZURE_SUBSCRIPTION_ID)
153+
--subscription-id "$SUBSCRIPTION_ID"
77154
78155
- task: PublishPipelineArtifact@1
79156
displayName: 'Publish artifacts'

src/templates/github-actions/publish-workflow.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function generatePublishWorkflow(config: PublishWorkflowConfig): string {
1414
const envChoices = config.environments.map((env) => ` - ${env}`).join('\n');
1515

1616
const envJobs = config.environments.map((env, idx) => {
17+
const envUpper = env.toUpperCase();
1718
const autoDeployComment = idx === 0
1819
? ` # To enable automatic deployment on push to main, uncomment the condition below:
1920
# if: github.event.inputs.ENVIRONMENT == '${env}' || github.event_name == 'push'`
@@ -54,30 +55,31 @@ ${autoDeployComment}
5455
tokenPrefix: '{#['
5556
tokenSuffix: ']#}'
5657
files: '["configuration.${env}.yaml"]'
57-
env:
58-
# Map pipeline secrets/variables to environment variables so that
59-
# {#[TOKEN_NAME]#} placeholders in configuration.${env}.yaml are replaced
60-
# with their actual values before the publish step runs. Example:
61-
# MY_SECRET: \${{ secrets.MY_SECRET }}
58+
# Example token mapping for ${env} (uncomment and customize when needed):
59+
# env:
60+
# MY_SECRET: \${{ secrets.MY_SECRET_${envUpper} }}
61+
# ANOTHER_TOKEN: \${{ secrets.ANOTHER_TOKEN_${envUpper} }}
6262
6363
- name: Publish to ${env} (incremental - last commit only)
6464
if: \${{ github.event.inputs.COMMIT_ID_CHOICE != 'publish-all-artifacts-in-repo' }}
6565
run: |
6666
npx apiops publish \\
6767
--subscription-id \${{ secrets.AZURE_SUBSCRIPTION_ID }} \\
68-
--resource-group \${{ secrets.APIM_RESOURCE_GROUP_${env.toUpperCase()} }} \\
69-
--service-name \${{ secrets.APIM_SERVICE_NAME_${env.toUpperCase()} }} \\
68+
--resource-group \${{ secrets.APIM_RESOURCE_GROUP_${envUpper} }} \\
69+
--service-name \${{ secrets.APIM_SERVICE_NAME_${envUpper} }} \\
7070
--source ${config.artifactDir} \\
71+
--overrides configuration.${env}.yaml \\
7172
--commit-id \${{ needs.get-commit.outputs.commit_id }}
7273
7374
- name: Publish to ${env} (all artifacts)
7475
if: \${{ github.event.inputs.COMMIT_ID_CHOICE == 'publish-all-artifacts-in-repo' }}
7576
run: |
7677
npx apiops publish \\
7778
--subscription-id \${{ secrets.AZURE_SUBSCRIPTION_ID }} \\
78-
--resource-group \${{ secrets.APIM_RESOURCE_GROUP_${env.toUpperCase()} }} \\
79-
--service-name \${{ secrets.APIM_SERVICE_NAME_${env.toUpperCase()} }} \\
80-
--source ${config.artifactDir}
79+
--resource-group \${{ secrets.APIM_RESOURCE_GROUP_${envUpper} }} \\
80+
--service-name \${{ secrets.APIM_SERVICE_NAME_${envUpper} }} \\
81+
--source ${config.artifactDir} \\
82+
--overrides configuration.${env}.yaml
8183
`;
8284
}).join('\n');
8385

tests/unit/templates/azure-devops/extract-pipeline.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('azure-devops/extract-pipeline', () => {
4949
it('should include Node.js setup step', () => {
5050
const pipeline = generateExtractPipeline({ artifactDir: './apim-artifacts' });
5151
expect(pipeline).toContain('UseNode@1');
52-
expect(pipeline).toContain("versionSpec: '22.x'");
52+
expect(pipeline).toContain("version: '22.x'");
5353
});
5454

5555
it('should include npm ci step', () => {
@@ -78,7 +78,8 @@ describe('azure-devops/extract-pipeline', () => {
7878

7979
it('should include subscription-id flag', () => {
8080
const pipeline = generateExtractPipeline({ artifactDir: './apim-artifacts' });
81-
expect(pipeline).toContain('--subscription-id $(AZURE_SUBSCRIPTION_ID)');
81+
expect(pipeline).toContain("SUBSCRIPTION_ID='$(AZURE_SUBSCRIPTION_ID)'");
82+
expect(pipeline).toContain('--subscription-id "$SUBSCRIPTION_ID"');
8283
});
8384

8485
it('should publish pipeline artifacts', () => {
@@ -99,7 +100,7 @@ describe('azure-devops/extract-pipeline', () => {
99100
it('should use npm ci to install dependencies (uses tgz from package.json)', () => {
100101
const pipeline = generateExtractPipeline({ artifactDir: './apim-artifacts' });
101102
expect(pipeline).toContain('npm ci');
102-
expect(pipeline).toContain('npx apiops extract');
103+
expect(pipeline).toContain('npx @peterhauge/apiops-cli extract');
103104
});
104105
});
105106
});

0 commit comments

Comments
 (0)