Add check to see if pyproject package version is as expected #121
Workflow file for this run
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: CI/CD | |
on: | |
push: | |
tags: [v*] | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
permissions: read-all | |
# https://github.com/marketplace/actions/docker-layer-caching | |
jobs: | |
check-pyproject-version: | |
runs-on: ubuntu-latest | |
# User controlled input needs to be santitised beforehand e.g., by adding an | |
# intermediate env var to prevent the workflow being exposed to a critical | |
# command injection attack | |
env: | |
PR_BODY: "${{ github.event.pull_request.body }}" | |
steps: | |
- name: Determine new tag version from PR body | |
id: determine_next_tag_from_pr | |
uses: climatepolicyradar/get-next-tag-from-pr-body@v2 | |
with: | |
pr_body: "${{ env.PR_BODY }}" | |
pr_number: "${{ github.event.pull_request.number }}" | |
- name: Next tag found? | |
run: | | |
if [[ -z "${{ steps.determine_next_tag_from_pr.outputs.new_tag }}" ]]; then | |
exit 1; | |
fi | |
- uses: actions/checkout@v4 | |
- name: Check pyproject.toml package version matches | |
run: | | |
if [ ! -f pyproject.toml ]; then | |
echo "pyproject.toml file not found"; | |
exit 1; | |
fi | |
package_version=$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) | |
new_tag="${{ steps.determine_next_tag_from_pr.outputs.new_tag }}" | |
new_tag=${new_tag#v} | |
if [[ -z "${package_version}" ]]; then | |
echo "Package version in pyproject.toml file missing"; | |
exit 1; | |
elif [[ -z "${new_tag}" ]]; then | |
echo "Could not determine next tag"; | |
exit 1; | |
elif [[ "${new_tag}" == 'Skip' ]]; then | |
exit 0; | |
elif [[ "${package_version}" != "${new_tag}" ]]; then | |
echo "Package version in pyproject.toml file outdated. Based on the auto-semvering option selected, package version should be '${new_tag}'"; | |
exit 1; | |
fi | |
code-quality: | |
permissions: | |
# For trunk to post annotations | |
checks: write | |
# For repo checkout | |
contents: read | |
uses: climatepolicyradar/reusable-workflows/.github/workflows/python-precommit-validator.yml@v1 | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install latest Docker Compose | |
uses: ndeloof/[email protected] | |
with: | |
legacy: false | |
- uses: actions/checkout@v4 | |
- name: Configure test env variables | |
run: cp .env.example .env | |
- name: Build docker compose | |
run: make build_dev | |
- name: Run Unit & Integration Tests | |
run: make test | |
build: | |
if: ${{ ! startsWith(github.ref, 'refs/tags') }} | |
runs-on: ubuntu-latest | |
needs: | |
- code-quality | |
- test | |
- check-pyproject-version | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Build | |
run: make build | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: eu-west-1 | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Publish initial image based on branch to ECR | |
env: | |
DOCKER_REGISTRY: ${{ secrets.DOCKER_REGISTRY }} | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
shell: bash | |
run: | | |
if [[ "${GITHUB_REF}" == "refs/heads"* ]]; then | |
branch="${GITHUB_REF/refs\/heads\//}" | |
if [[ "${branch}" = "main" ]]; then | |
docker_tag=latest | |
docker tag navigator-admin-backend "$ECR_REGISTRY/navigator-admin-backend:${docker_tag}" | |
docker push "$ECR_REGISTRY/navigator-admin-backend:${docker_tag}" | |
fi | |
elif [[ "${GITHUB_REF}" != "refs/tags"* ]]; then | |
echo "Assuming '${GITHUB_HEAD_REF}' is a branch" | |
if [[ -n "${GITHUB_HEAD_REF}" ]]; then | |
branch="$(echo ${GITHUB_HEAD_REF}| tr -c '[0-9,A-Z,a-z]' '-')" | |
timestamp=$(date --utc -Iseconds | cut -c1-19 | tr -c '[0-9]T\n' '-') | |
short_sha=${GITHUB_SHA:0:8} | |
docker_tag="${branch}-${timestamp}-${short_sha}" | |
docker tag navigator-admin-backend "$ECR_REGISTRY/navigator-admin-backend:${docker_tag}" | |
docker push "$ECR_REGISTRY/navigator-admin-backend:${docker_tag}" | |
fi | |
fi | |
manual-semver: | |
needs: | |
- code-quality | |
- test | |
if: ${{ startsWith(github.ref, 'refs/tags') }} | |
uses: climatepolicyradar/reusable-workflows/.github/workflows/semver.yml@v1 | |
secrets: inherit | |
with: | |
repo-name: navigator-admin-backend | |
semver-tag: main-${GITHUB_SHA::8} | |
tag: | |
needs: build | |
permissions: | |
contents: write | |
uses: climatepolicyradar/reusable-workflows/.github/workflows/tag.yml@v1 | |
with: | |
repo-name: navigator-admin-backend | |
semver-tag: main-${GITHUB_SHA::8} | |
secrets: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
DOCKER_REGISTRY: ${{ secrets.DOCKER_REGISTRY }} | |
release: | |
needs: tag | |
if: ${{ needs.tag.outputs.new_tag != 'Skip' }} | |
permissions: | |
contents: write | |
uses: climatepolicyradar/reusable-workflows/.github/workflows/release.yml@v1 | |
with: | |
new_tag: ${{ needs.tag.outputs.new_tag }} |