diff --git a/.github/workflows/build-java-push-aws-ecr.yaml b/.github/workflows/build-java-push-aws-ecr.yaml new file mode 100644 index 0000000..17378c1 --- /dev/null +++ b/.github/workflows/build-java-push-aws-ecr.yaml @@ -0,0 +1,69 @@ +name: Build and deploy Docker images to AWS ECR (Elastic Container Registry) + +on: + push: + branches: + - main + workflow_dispatch: + +permissions: + id-token: write + contents: write + +env: + ECR_REGISTRY: ${{ secrets.ECR_ARTIFACT_REGISTRY }} +# IMAGE_TAG: ${{ github.sha }} + IMAGE_NAME: ecmsp-project/${{ github.event.repository.name }} + +jobs: + build_deploy: + runs-on: ubuntu-latest + + steps: + - + name: Check out code + uses: actions/checkout@v3 + + - + name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: '21' + distribution: 'temurin' + cache: 'maven' + + - + name: Update Maven snapshots + run: mvn -U dependency:resolve + + - + name: Configure AWS credentials via OIDC + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::529236942244:role/github-actions-ecr + aws-region: eu-central-1 + + - + name: Login to Amazon ECR + uses: aws-actions/amazon-ecr-login@v2 + + - + name: Release new version + id: release + run: | + bash ./scripts/git_update.sh -v patch + + - + name: Add app version (Optional) + run: echo ${{ steps.release.outputs.new-tag }} > version.txt + + - + name: Build, tag, and push backend Docker image to AWS ECR + env: + IMAGE_TAG: ${{ steps.release.outputs.new-tag }} + run: | + docker build \ + -t $ECR_REGISTRY/$IMAGE_NAME:$IMAGE_TAG \ + -f ./Dockerfile . + + docker push $ECR_REGISTRY/$IMAGE_NAME:$IMAGE_TAG \ No newline at end of file diff --git a/.github/workflows/build-java-push-gcp-registry.yaml b/.github/workflows/build-java-push-gcp-registry.yaml deleted file mode 100644 index 2447053..0000000 --- a/.github/workflows/build-java-push-gcp-registry.yaml +++ /dev/null @@ -1,68 +0,0 @@ -name: Build and deploy Docker images to GCP Artifact Registry - -on: - push: - branches: - - main - workflow_dispatch: - -env: - GCP_REGISTRY: ${{ secrets.ECMSP_GCP_ARTIFACT_REGISTRY }} - IMAGE_TAG: ${{ github.sha }} - IMAGE_NAME: ${{ github.event.repository.name }} - -jobs: - build_deploy: - runs-on: ubuntu-latest - - steps: - - - name: Check out code - uses: actions/checkout@v3 - - - - name: Set up JDK 21 - uses: actions/setup-java@v3 - with: - java-version: '21' - distribution: 'temurin' - cache: 'maven' - - - - name: Update Maven snapshots - run: mvn -U dependency:resolve - - - - name: Authenticate to Google Cloud - uses: google-github-actions/auth@v1 - with: - credentials_json: ${{ secrets.ECMSP_REGISTRY_PUSHER_GCP_SA_KEY }} - - - - name: Setup gcloud CLI - uses: google-github-actions/setup-gcloud@v2 - with: - version: "latest" - project_id: ecmsp - - - - name: Install gcloud auth - run: gcloud components install gke-gcloud-auth-plugin - - - - name: Configure Docker for Artifact Registry - run: gcloud auth configure-docker europe-west1-docker.pkg.dev - - - - name: Build, tag, and push backend Docker image to GCP Artifact Registry - run: | - docker build \ - -t $GCP_REGISTRY/$IMAGE_NAME:$IMAGE_TAG \ - -t $GCP_REGISTRY/$IMAGE_NAME:latest \ - -f ./Dockerfile . - - docker push $GCP_REGISTRY/$IMAGE_NAME:$IMAGE_TAG - docker push $GCP_REGISTRY/$IMAGE_NAME:latest - - - diff --git a/.github/workflows/build-test-java-project.yaml b/.github/workflows/build-test-java-project.yaml index 406cfdf..ce01948 100644 --- a/.github/workflows/build-test-java-project.yaml +++ b/.github/workflows/build-test-java-project.yaml @@ -1,8 +1,10 @@ name: Build and test Java project on: - pull_request: - workflow_dispatch: + pull_request: + branches: + - main + workflow_dispatch: jobs: build_test: diff --git a/scripts/git_update.sh b/scripts/git_update.sh new file mode 100644 index 0000000..e1d617b --- /dev/null +++ b/scripts/git_update.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +VERSION="" + +# get parameters +while getopts v: flag +do + case "${flag}" in + v) VERSION=${OPTARG};; + esac +done + +# get highest tag number, and add v0.1.0 if doesn't exist +git fetch --prune --unshallow 2>/dev/null +CURRENT_VERSION=`git describe --abbrev=0 --tags 2>/dev/null` + +if [[ $CURRENT_VERSION == '' ]] +then + CURRENT_VERSION='v0.1.0' +fi +echo "Current Version: $CURRENT_VERSION" + +# replace . with space so can split into an array +CURRENT_VERSION_PARTS=(${CURRENT_VERSION//./ }) + +# get number parts +VNUM1=${CURRENT_VERSION_PARTS[0]} +VNUM2=${CURRENT_VERSION_PARTS[1]} +VNUM3=${CURRENT_VERSION_PARTS[2]} + +if [[ $VERSION == 'major' ]] +then + VNUM1=v$((VNUM1+1)) +elif [[ $VERSION == 'minor' ]] +then + VNUM2=$((VNUM2+1)) +elif [[ $VERSION == 'patch' ]] +then + VNUM3=$((VNUM3+1)) +else + echo "No version type (https://semver.org/) or incorrect type specified, try: -v [major, minor, patch]" + exit 1 +fi + +# create new tag +NEW_TAG="$VNUM1.$VNUM2.$VNUM3" +echo "($VERSION) updating $CURRENT_VERSION to $NEW_TAG" + +# get current hash and see if it already has a tag +GIT_COMMIT=`git rev-parse HEAD` +NEEDS_TAG=`git describe --contains $GIT_COMMIT 2>/dev/null` + +# only tag if no tag already +if [ -z "$NEEDS_TAG" ]; then + echo "Tagged with $NEW_TAG" + git tag $NEW_TAG + git push --tags + git push +else + echo "Already a tag on this commit" +fi + +echo "new-tag=$NEW_TAG" >> $GITHUB_OUTPUT + +exit 0