GitHub Actions Demo Pipeline #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: GitHub Actions Demo Pipeline | |
on: | |
pull_request: | |
branches: [ main ] | |
workflow_dispatch: | |
jobs: | |
unit-test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Repo Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: 'main' | |
- name: Python Setup | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.10' | |
- name: Install Dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r src/requirements.txt | |
- name: Run Tests | |
run: | | |
python -m unittest discover -s unit-tests -p "tests.py" | |
repo-tag: | |
runs-on: ubuntu-latest | |
needs: unit-test | |
outputs: | |
version: ${{ steps.export-version.outputs.VERSION }} | |
steps: | |
- name: Repo Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: 'main' | |
fetch-depth: 0 | |
- name: Export Version | |
id: export-version | |
run: | | |
VERSION=$(cat version.txt) | |
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | |
- name: Tag Repo | |
run: | | |
git config --global user.name "github-actions" | |
git config --global user.email "[email protected]" | |
git tag -a "v${{ steps.export-version.outputs.VERSION }}" -m "Version ${{ steps.export-version.outputs.VERSION }} Repo Tag" | |
git push origin "v${{ steps.export-version.outputs.VERSION }}" | |
docker-build-and-push: | |
runs-on: ubuntu-latest | |
needs: repo-tag | |
env: | |
TAG: ${{ needs.repo-tag.outputs.version }} | |
steps: | |
- name: Repo Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: "v${{ env.TAG }}" | |
- name: Docker Buildx Setup | |
uses: docker/setup-buildx-action@v3 | |
- name: GHCR Setup | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Repo Lowercase | |
run: | |
echo "REPO=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}" | |
- name: Docker Build | |
run: | | |
docker build -t ghcr.io/${{ env.REPO }}/gh-actions-demo:${{ env.TAG }} . | |
- name: Docker Push | |
run: | | |
docker push ghcr.io/${{ env.REPO }}/gh-actions-demo:${{ env.TAG }} | |
version-bump: | |
runs-on: ubuntu-latest | |
needs: docker-build-and-push | |
steps: | |
- name: Repo Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: 'main' | |
- name: Patch Version Bump | |
id: patch-version-bump | |
run: | | |
VERSION=$(cat version.txt) | |
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" | |
PATCH=$((PATCH + 1)) | |
NEW_VERSION="$MAJOR.$MINOR.$PATCH" | |
echo $NEW_VERSION > version.txt | |
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT | |
- name: Commit and Push | |
run: | | |
git config --global user.name "github-actions" | |
git config --global user.email "[email protected]" | |
git add version.txt | |
git commit -m "Bump version to ${{ steps.patch-version-bump.outputs.NEW_VERSION }}" | |
git push origin main |