Skip to content

Bugfix example-build-deploy-dotnet-iss.yml #198

Bugfix example-build-deploy-dotnet-iss.yml

Bugfix example-build-deploy-dotnet-iss.yml #198

name: Update starter workflows
on:
push:
branches: [trunk]
paths:
- '.github/workflows/example-*.yml'
- '.github/workflows/update-starter-workflows.yml'
pull_request:
branches: [trunk]
paths:
- '.github/workflows/example-*.yml'
- '.github/workflows/update-starter-workflows.yml'
env:
REPOSITORY: 'core-github-actions-templates'
concurrency:
group: '${{ github.workflow }}-${{ github.ref }}'
cancel-in-progress: true
jobs:
update-starter-workflows:
name: Update starter workflows
runs-on: elvia-runner
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
path: ${{ env.REPOSITORY }}
- name: Get GitHub App token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.GH_APP_ID }}
private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Checkout .github repository
uses: actions/checkout@v4
with:
path: 'dotgithub'
repository: '3lvia/.github'
token: ${{ steps.app-token.outputs.token }}
- name: Update workflows-templates
shell: bash
run: |
mkdir -p dotgithub/workflow-templates
for workflow_path in ${{ env.REPOSITORY }}/.github/workflows/example-*.yml; do
echo "Processing $workflow_path"
file_name=$(basename "$workflow_path")
file_name_no_prefix="${file_name#example-}"
printf "file_name: %s\n" "$file_name"
printf "file_name_no_prefix: %s\n" "$file_name_no_prefix"
new_workflow_path="dotgithub/workflow-templates/$file_name_no_prefix"
cp "$workflow_path" "$new_workflow_path"
sed -i "s/ APPLICATION_NAME: '.*'/ APPLICATION_NAME: '<your application name here>'/g" "$new_workflow_path"
sed -i "s/ SYSTEM_NAME: '.*'/ SYSTEM_NAME: '<your system name here>'/g" "$new_workflow_path"
sed -i "s/ HELM_VALUES_FILE: '.*'/ HELM_VALUES_FILE: '.github\/deploy\/values.yml'/g" "$new_workflow_path"
sed -i "s/ PROJECT_FILE: '.*'/ PROJECT_FILE: '<your project file path here>'/g" "$new_workflow_path"
sed -i "s/# pull_request:/ pull_request:/g" "$new_workflow_path"
sed -i 's/# branches:/ branches:/g' "$new_workflow_path"
sed -i 's/branches: \[trunk\]/branches: \[$default-branch\]/g' "$new_workflow_path"
sed -i "s/checkout: 'false'.*\$//g" "$new_workflow_path"
# We disable Trivy uploading report in testing, so reenable here.
sed -i "s/trivy-upload-report: 'false'/trivy-upload-report: 'true'/g" "$new_workflow_path"
# We checkout core repo to test on demo apps, so we remove this part from the examples.
perl -0777 -i -pe 's/# START REMOVE FROM EXAMPLE.*?# END REMOVE FROM EXAMPLE\n//gms' "$new_workflow_path"
# Special case for ISS example
sed -i 's/branches: \[too-complicated-to-setup-tests-for-this-so-we-dont\]/branches: \[$default-branch\]/g' "$new_workflow_path"
# Special case for replacing name of concurrency group for demo-api-go, since it is deployed twice (go.mod and Dockerfile).
# TODO: loop and don't hardcode envs three times
sed -i "s/group: 'dev-.*--this-group-is-replaced-in-final-example-needs-this-to-not-deploy-demo-api-go-simultaneously-in-two-examples'/group: '${{ '\${{ github.workflow }}-\${{ github.ref }}-deploy-dev' }}'/g" "$new_workflow_path"
sed -i "s/group: 'test-.*--this-group-is-replaced-in-final-example-needs-this-to-not-deploy-demo-api-go-simultaneously-in-two-examples'/group: '${{ '\${{ github.workflow }}-\${{ github.ref }}-deploy-test' }}'/g" "$new_workflow_path"
sed -i "s/group: 'prod-.*--this-group-is-replaced-in-final-example-needs-this-to-not-deploy-demo-api-go-simultaneously-in-two-examples'/group: '${{ '\${{ github.workflow }}-\${{ github.ref }}-deploy-prod' }}'/g" "$new_workflow_path"
new_workflow_properties_file="dotgithub/workflow-templates/${file_name_no_prefix%.yml}.properties.json"
if [ ! -f "$new_workflow_properties_file" ]; then
echo "Did not find properties file $new_workflow_properties_file, creating new properties file for workflow $new_workflow_path."
new_workflow_name=$(yq -r '.name' "$new_workflow_path")
cat << EOF > "$new_workflow_properties_file"
{
"name": "$new_workflow_name",
"description": "$new_workflow_name"
}
EOF
else
echo "Properties file $new_workflow_properties_file already exists for $new_workflow_path, will not overwrite."
fi
done
- name: Get examples diff
if: github.event_name == 'pull_request'
id: get-diff
run: |
git add workflow-templates
examples_files_changed=$(git diff --name-only --staged workflow-templates | base64 -w0)
examples_diff=$(git diff --staged workflow-templates | base64 -w0)
echo "examples-files-changed=$examples_files_changed" >> "$GITHUB_OUTPUT"
echo "examples-diff=$examples_diff" >> "$GITHUB_OUTPUT"
working-directory: 'dotgithub'
- name: Add comment with diff
if: github.event_name == 'pull_request' && steps.get-diff.outputs.examples-files-changed != ''
uses: actions/github-script@v7
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const header = "## 📝 Starter Workflow Templates Update\n";
const examplesFilesChanged = atob('${{ steps.get-diff.outputs.examples-files-changed }}');
const examplesDiff = atob('${{ steps.get-diff.outputs.examples-diff }}');
const botComment = comments.find(
(comment) =>
comment.user.type === "Bot" &&
comment.body.includes(header)
);
const body = `${header}
The starter workflow templates have been updated. Please review the changes below.\n
**Files changed:**\n
${examplesFilesChanged}
**<details><summary>Full diff:</summary>**
\`\`\`\n
${examplesDiff}
\`\`\`
</details>
`;
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
- name: Commit changes
shell: bash
if: github.event_name == 'push'
run: |
if [[ -z "$(git status --porcelain)" ]]; then
echo 'No changes to commit.'
exit 0
fi
git config user.email '${{ vars.GH_APP_USER_EMAIL }}'
git config user.name '${{ vars.GH_APP_USERNAME }}'
git add workflow-templates
git commit -m 'Update starter workflows'
for git_file in $(git ls-files); do
cat "$git_file"
done
git push
working-directory: 'dotgithub'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}