Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reusable workflows #2

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/update-carter-nuget-package-references.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Update Carter NuGet package references

on:
workflow_call:
inputs:
includePrerelease:
required: true
type: boolean
description: Whether to include prerelease versions when searching for the latest version of the Carter NuGet package

commitAndPush:
required: true
type: boolean
description: Whether to commit and push the changes; Useful to test the process by looking at the resulting Git diff

jobs:
update-carter-package-references:
name: Update Carter NuGet package references
runs-on: ubuntu-latest

steps:
- name: Check out this repository
uses: actions/checkout@v3
with:
path: self

- name: Check out the CarterCommunity/build repository
uses: actions/checkout@v3
with:
repository: CarterCommunity/build
path: build

- name: Update package references with latest version from NuGet
run: >-
build/update-carter-package-references/run.ps1
-RootDirectory (Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'self')
-IncludePrerelease:$${{ inputs.includePrerelease }}
shell: pwsh

- name: Show Git diff
working-directory: self
run: git diff

# https://github.com/marketplace/actions/checkout#push-a-commit-using-the-built-in-token
- if: inputs.commitAndPush
name: Commit changes and push if necessary
working-directory: self
run: |
GIT_CHANGES=$(git status --porcelain)
if [ "$GIT_CHANGES" == "" ]; then
echo "No changes to commit"
else
echo "Committing and pushing changes"
git config user.name Jonathan Channon
git config user.email jonathan.channon@gmail.com
git add .
git commit -m "Update Carter NuGet package references"
git push
fi
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Reusable bits

This repository contains actions that can be reused in other repositories.
This repository contains actions and workflows that can be reused in other repositories.

It currently includes:

9 changes: 8 additions & 1 deletion update-carter-package-references/README.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,13 @@ The files in this directory can help set up a workflow in other repositories to
### Steps

1. Create a new branch in the target repository.
1. Copy the [`workflow.yml`](./workflow.yml) file in this directory as the `.github/workflows/update-carter-package-references.yml` file in the target repository.
1. Copy the [`workflow.yml`](./workflow.yml) file in this directory to the `.github/workflows` directory in the target repository.
1. Open a pull request.
1. When merged, you will find a new action in the target repository.

### How it works

This process takes advantage of [reusable workflows in GitHub Actions](https://docs.github.com/en/actions/using-workflows/reusing-workflows), which avoids having to duplicate logic across several repositories.

The workflow defined in the `workflow.yml` file calls the workflow located in the [`.github/workflows/update-carter-nuget-package-references.yml`](../.github/workflows/update-carter-nuget-package-references.yml) file in this repository, which contains all the logic.
If in the future, we need to make adjustments to the logic, we'll only have to update a single file, instead of having to go through all the repositories where we might have copied that logic.
44 changes: 4 additions & 40 deletions update-carter-package-references/workflow.yml
Original file line number Diff line number Diff line change
@@ -16,43 +16,7 @@ on:

jobs:
update-carter-package-references:
name: Update Carter NuGet package references
runs-on: ubuntu-latest

steps:
- name: Check out this repository
uses: actions/checkout@v3
with:
path: self

- name: Check out the CarterCommunity/.github repository
uses: actions/checkout@v3
with:
repository: CarterCommunity/.github
path: .github

- name: Update package references with latest version from NuGet
run: >-
.github/update-carter-package-references/run.ps1
-RootDirectory (Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'self')
-IncludePrerelease:$${{ github.event.inputs.includePrerelease }}
shell: pwsh

- name: Show Git diff
run: git diff

# https://github.com/marketplace/actions/checkout#push-a-commit-using-the-built-in-token
- if: github.event.inputs.commitAndPush
name: Commit changes and push if necessary
run: |
GIT_CHANGES=$(git status --porcelain)
if [ "$GIT_CHANGES" == "" ]; then
echo "No changes to commit"
else
echo "Committing and pushing changes"
git config user.name Jonathan Channon
git config user.email jonathan.channon@gmail.com
git add .
git commit -m "Update Carter NuGet package references"
git push
fi
uses: CarterCommunity/build/.github/workflows/update-carter-package-references.yml@main
with:
includePrerelease: ${{ github.event.inputs.includePrerelease }}
commitAndPush: ${{ github.event.inputs.commitAndPush }}