Skip to content

Commit

Permalink
Update Jars to GH release & Bump version (#240)
Browse files Browse the repository at this point in the history
* Update release file paths

* Update upload to GH release

* Change GH release action to avoid drafts and file checks

* Remove release notes

* Remove automatic bump version

* Remove unused workflow_dispatch trigger

* Bump version to 0.1.1-rc1

* chore(Dockerfile): make gradle build args configurable

* chore(earthly): add release folder and Earthfile itself to .earthlyignore

* chore(Earthfile): refactor to make use of our upstream earthly functions

* chore(gha): refactor publish job

* chore(gha): fix typo in regex check

* chore(Earthfile): make the docker-tag-n-push function wait for the builds to finish

* chore(gha): enable docker pushes

* chore(gha): reworked the publish job to trigger only if tag matches gradle, changed tag trigger to match semver (removing 'v' prefix) and re-enable on-demand builds for SNAPSHOT versions

* chore(gha): remove the use or RELEASE_TAG from earthly, simplifying the workflow file

* chore(gha): disable docker push to try out the new refactor

* chore(gha): fix typo

* chore(gha): commit correct version to gradle.properties

* chore(gha): commit version including suffix to gradle.properties

* chore(gha): fix trigger version to accept suffixed semvers

* chore(gha): enable debug

* chore(gha): add concurrency group

* chore(gha): fix typo in SNAPSHOT check

* chore: added minimal CODEOWNERS file for github workflows and docker assets

* chore(gha): re-enable docker pushes

---------

Co-authored-by: Roberto C. Morano <[email protected]>
  • Loading branch information
satran004 and rcmorano authored Oct 9, 2024
1 parent 6a93e0f commit e03e882
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 139 deletions.
2 changes: 2 additions & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ docker-compose*
.gitignore
LICENSE
.vscode
release
Earthfile
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.github/* @rcmorano @satran004
Earthfile @rcmorano @satran004
Dockerfile @rcmorano @satran004
154 changes: 79 additions & 75 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Publish artifacts and create Github releases
on:
push:
tags:
- 'v*'
- '[0-9]+.[0-9]+.[0-9]+'
- '[0-9]+.[0-9]+.[0-9]+*'
workflow_dispatch:

env:
Expand All @@ -13,6 +14,11 @@ env:
PRIVATE_MVN_REGISTRY_URL: ${{ secrets.GITLAB_MAVEN_REGISTRY_URL }}
PRIVATE_MVN_REGISTRY_USER: ${{ secrets.GITLAB_PKG_REGISTRY_USER }}
PRIVATE_MVN_REGISTRY_PASS: ${{ secrets.GITLAB_PKG_REGISTRY_TOKEN }}
DOCKER_PUSH: true

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
publish:
Expand All @@ -26,100 +32,98 @@ jobs:
with:
token: ${{ secrets.COMMIT_PAT_TOKEN }}

- name: ⛮ cf-gha-baseline
uses: cardano-foundation/cf-gha-workflows/./actions/cf-gha-baseline@main
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PRIVATE_DOCKER_REGISTRY_URL: ${{ env.PRIVATE_DOCKER_REGISTRY_URL }}
PRIVATE_DOCKER_REGISTRY_USER: ${{ env.PRIVATE_DOCKER_REGISTRY_USER }}
PRIVATE_DOCKER_REGISTRY_PASS: ${{ env.PRIVATE_DOCKER_REGISTRY_PASS }}
HUB_DOCKER_COM_USER: ${{ secrets.HUB_DOCKER_COM_USER }}
HUB_DOCKER_COM_PASS: ${{ secrets.HUB_DOCKER_COM_PASS }}
DOCKER_REGISTRIES: "${{ secrets.DOCKER_REGISTRIES }}"

- name: Set extra environment and metadata
id: metadata
run: |
GIT_SHORT_COMMIT=${GITHUB_SHA::7}
DOCKER_IMAGE_NAME=$(basename ${GITHUB_REPOSITORY})
GRADLE_PROPERTIES_VERSION=$(grep version gradle.properties | awk '{print $NF}')
# if a tag triggered the version, use the tag to set the version in gradle.properties
if [ "${GITHUB_REF_TYPE}" == "tag" ]
then
TRIGGERING_TAG=${GITHUB_REF#refs/tags/v}
DOCKER_IMAGES_EXTRA_TAGS="${TRIGGERING_TAG} latest"
echo "TRIGGERING_TAG=${TRIGGERING_TAG}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
echo "DOCKER_IMAGES_EXTRA_TAGS=${DOCKER_IMAGES_EXTRA_TAGS}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
sed -i "s|^version.*|version = ${TRIGGERING_TAG}|" gradle.properties
fi
ARTIFACT_VERSION=$(grep version gradle.properties | awk '{print $NF}')
NEW_ARTIFACT_SNAPSHOT_VERSION=$(echo $ARTIFACT_VERSION | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')
echo "GITHUB_EVENT_NAME: ${GITHUB_EVENT_NAME}"
echo "ARTIFACT_VERSION=${ARTIFACT_VERSION}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
echo "NEW_ARTIFACT_SNAPSHOT_VERSION=${NEW_ARTIFACT_SNAPSHOT_VERSION}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
echo "GIT_SHORT_COMMIT=${GIT_SHORT_COMMIT}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
echo "GITHUB_RUN_ID=${GITHUB_RUN_ID}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
# if a tag triggered the build, use the tag to set the version in gradle.properties
case "${GITHUB_EVENT_NAME}" in
push)
if [ "${TRIGGERING_REF}" == "tag" ]
then
# fail if tag differs from what's set for gradle
if [ "${TAG_NAME}" != "${GRADLE_PROPERTIES_VERSION}" ]
then
echo "[!] Tag name doesn't match gradle.properties version. Please commit the proper version and retag."
exit 1
fi
echo "DOCKER_IMAGES_EXTRA_TAGS=${ARTIFACT_VERSION}-${GIT_SHORT_COMMIT} ${ARTIFACT_VERSION}-${GIT_SHORT_COMMIT}-${GITHUB_RUN_ID} ${DOCKER_IMAGES_EXTRA_TAGS}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
# Add 'latest' tag to the list of tags to push
EARTHLY_DOCKER_IMAGES_EXTRA_TAGS="${EARTHLY_DOCKER_IMAGES_EXTRA_TAGS} ${TAG_NAME} latest"
- name: Docker Login (private registry)
uses: docker/login-action@v2
with:
registry: ${{ env.PRIVATE_DOCKER_REGISTRY_URL }}
username: ${{ env.PRIVATE_DOCKER_REGISTRY_USER }}
password: ${{ env.PRIVATE_DOCKER_REGISTRY_PASS }}
# Set target docker registries to every registry in the secret
echo "DOCKER_REGISTRIES=${{ secrets.DOCKER_REGISTRIES }}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
- name: Docker Login (hub.docker.com)
uses: docker/login-action@v2
with:
username: ${{ secrets.HUB_DOCKER_COM_USER }}
password: ${{ secrets.HUB_DOCKER_COM_PASS }}
# Set the next snapshot version that will be auto-commited to the main branch
NEXT_SNAPSHOT_VERSION=$(echo $GRADLE_PROPERTIES_VERSION | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')
echo "NEXT_SNAPSHOT_VERSION=${NEXT_SNAPSHOT_VERSION}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
fi
;;
workflow_dispatch)
if ! [[ "${GRADLE_PROPERTIES_VERSION}" =~ .*-SNAPSHOT$ ]]
then
echo "[!] gradle.properties version is not a SNAPSHOT version. Please fix it and relaunch this job."
exit 1
fi
# Set target docker registries to only use the private registry
echo "DOCKER_REGISTRIES=${PRIVATE_DOCKER_REGISTRY_URL}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
EARTHLY_DOCKER_IMAGES_EXTRA_TAGS="${EARTHLY_DOCKER_IMAGES_EXTRA_TAGS} ${GRADLE_PROPERTIES_VERSION}"
;;
*)
echo "[!] Unsupported event name for this workflow: ${GITHUB_EVENT_NAME}. Please check with the workflow maintainer :)"
exit 1
esac
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- uses: earthly/actions-setup@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
version: "0.8.6"

- name: 🌍 earthly (docker+mavn build and push)
env:
EARTHLY_BUILD_ARGS: DOCKER_IMAGES_EXTRA_TAGS=${{ steps.metadata.outputs.DOCKER_IMAGES_EXTRA_TAGS }},DOCKER_REGISTRIES=${{ secrets.DOCKER_REGISTRIES }}
echo "GRADLE_PROPERTIES_VERSION=${GRADLE_PROPERTIES_VERSION}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
echo "EARTHLY_DOCKER_IMAGES_EXTRA_TAGS=${EARTHLY_DOCKER_IMAGES_EXTRA_TAGS}" | tee -a "$GITHUB_ENV" | tee -a "$GITHUB_OUTPUT"
- name: 🌍 earthly - gradle build
run: |
if [ "${GITHUB_REF_TYPE}" == "tag" ]
then
earthly +docker-publish --RELEASE_TAG=${ARTIFACT_VERSION}
else
earthly +docker-publish
fi
- name: Preprare release notes
if: github.ref_type == 'tag'
id: release_notes
earthly +gradle-build
- name: 🌍 earthly - docker build and push
run: |
cat > release-notes.md <<EOF
# PLACEHODLER
EOF
- name: Create Github release
earthly +docker-publish \
--PUSH=${DOCKER_PUSH} \
--DOCKER_REGISTRIES="${DOCKER_REGISTRIES}" \
--DOCKER_IMAGES_EXTRA_TAGS="${EARTHLY_DOCKER_IMAGES_EXTRA_TAGS}"
- name: Create Github release and upload jar files
if: github.ref_type == 'tag'
uses: softprops/action-gh-release@v2
with:
draft: true
fail_on_unmatched_files: true
body_path: release-notes.md
prerelease: true
files: |
build/*
release/*
- name: Bump versions
if: github.ref_type == 'tag'
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git fetch --all --tags
sed -i "s|^version.*|version = ${NEW_ARTIFACT_SNAPSHOT_VERSION}-SNAPSHOT|" gradle.properties
git commit gradle.properties -m "chore(gradle): Bump version to ${NEW_ARTIFACT_SNAPSHOT_VERSION}-SNAPSHOT (automated commit by gha run id $GITHUB_RUN_ID)"
sed -i "s|image: \${CF_LEDGER_SYNC_DOCKER_IMAGE:-cardanofoundation/cf-ledger-sync:latest}| image: \${CF_LEDGER_SYNC_DOCKER_IMAGE:-cardanofoundation/cf-ledger-sync:${ARTIFACT_VERSION}}|" docker-compose.yaml
git commit docker-compose.yaml -m "chore(docker-compose): Bump image version to ${ARTIFACT_VERSION} (automated commit by gha run id $GITHUB_RUN_ID)"
# Commit to main if it contains the tag, otherwise to whatever first branch contains the tag
# Bump version only if tag's commit belongs to main branch
if [ $(git for-each-ref | grep ^${GIT_SHORT_COMMIT} | grep refs.remotes.origin | sed 's|.*refs/remotes/origin/\(.*\)|\1|' | grep -c ^main$) -gt 0 ]
then
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git fetch --all --tags
sed -i "s|^version.*|version = ${NEW_ARTIFACT_SNAPSHOT_VERSION}-SNAPSHOT|" gradle.properties
git commit gradle.properties -m "chore(gradle): Bump version to ${NEW_ARTIFACT_SNAPSHOT_VERSION}-SNAPSHOT (automated commit by gha run id $GITHUB_RUN_ID)"
sed -i "s|image: \${CF_LEDGER_SYNC_DOCKER_IMAGE:-cardanofoundation/cf-ledger-sync:latest}| image: \${CF_LEDGER_SYNC_DOCKER_IMAGE:-cardanofoundation/cf-ledger-sync:${ARTIFACT_VERSION}}|" docker-compose.yaml
git commit docker-compose.yaml -m "chore(docker-compose): Bump image version to ${ARTIFACT_VERSION} (automated commit by gha run id $GITHUB_RUN_ID)"
TAG_BRANCH=main
else
TAG_BRANCH=$(git for-each-ref | grep ^${GIT_SHORT_COMMIT} | grep refs.remotes.origin | head -n1 | sed 's|.*refs/remotes/origin/\(.*\)|\1|')
git push origin HEAD:${TAG_BRANCH}
fi
git push origin HEAD:${TAG_BRANCH}
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM eclipse-temurin:21-jdk AS build
ARG GRADLE_BUILD_ARGS=clean build -x test
WORKDIR /app
COPY . /app
RUN ./gradlew clean build -x test
RUN ./gradlew ${GRADLE_BUILD_ARGS}

FROM eclipse-temurin:21-jdk AS ledger-sync
WORKDIR /app
Expand Down
129 changes: 67 additions & 62 deletions Earthfile
Original file line number Diff line number Diff line change
@@ -1,99 +1,104 @@
VERSION 0.8

ARG --global ALL_BUILD_TARGETS="ledger-sync aggregation streamer scheduler"
IMPORT --allow-privileged github.com/cardano-foundation/cf-gha-workflows/./earthfiles/functions:main AS functions

ARG --global DOCKER_IMAGES_TARGETS="ledger-sync aggregation streamer scheduler"

ARG --global DOCKER_IMAGE_PREFIX="cf-ledger-sync"
ARG --global DOCKER_IMAGES_EXTRA_TAGS=""
ARG --global DOCKER_REGISTRIES="hub.docker.com"
ARG --global HUB_DOCKER_COM_ORG=cardanofoundation
ARG --global DOCKER_REGISTRIES=""
ARG --global PUSH=false

all:
LOCALLY
ARG RELEASE_TAG
FOR image_target IN $ALL_BUILD_TARGETS
BUILD +${image_target} --RELEASE_TAG=${RELEASE_TAG}
END
ARG --global GRADLE_BUILD_ARGS="clean build -PskipSigning=true --stacktrace"

docker-publish:
ARG EARTHLY_GIT_SHORT_HASH
ARG RELEASE_TAG
all:
WAIT
BUILD +all --RELEASE_TAG=${RELEASE_TAG}
BUILD +gradle-build
END
LOCALLY
LET IMAGE_NAME = ""
FOR registry IN $DOCKER_REGISTRIES
FOR image_target IN $ALL_BUILD_TARGETS
IF [ "$image_target" = "ledger-sync" ]
SET IMAGE_NAME = ${DOCKER_IMAGE_PREFIX}
ELSE
SET IMAGE_NAME = ${DOCKER_IMAGE_PREFIX}-${image_target}
END
IF [ ! -z "$DOCKER_IMAGES_EXTRA_TAGS" ]
FOR image_tag IN $DOCKER_IMAGES_EXTRA_TAGS
IF [ "$registry" = "hub.docker.com" ]
RUN docker tag ${IMAGE_NAME}:latest ${HUB_DOCKER_COM_ORG}/${IMAGE_NAME}:${image_tag}
RUN docker push ${HUB_DOCKER_COM_ORG}/${IMAGE_NAME}:${image_tag}
ELSE
RUN docker tag ${IMAGE_NAME}:latest ${registry}/${IMAGE_NAME}:${image_tag}
RUN docker push ${registry}/${IMAGE_NAME}:${image_tag}
END
END
END
IF [ "$registry" = "hub.docker.com" ]
RUN docker tag ${IMAGE_NAME}:latest ${HUB_DOCKER_COM_ORG}/${IMAGE_NAME}:${EARTHLY_GIT_SHORT_HASH}
RUN docker push ${HUB_DOCKER_COM_ORG}/${IMAGE_NAME}:${EARTHLY_GIT_SHORT_HASH}
ELSE
RUN docker tag ${IMAGE_NAME}:latest ${registry}/${IMAGE_NAME}:${EARTHLY_GIT_SHORT_HASH}
RUN docker push ${registry}/${IMAGE_NAME}:${EARTHLY_GIT_SHORT_HASH}
END
END
FOR image_target IN $DOCKER_IMAGES_TARGETS
BUILD +$image_target --PUSH=$PUSH
END

docker-publish:
BUILD +all --PUSH=$PUSH

TEMPLATED_DOCKERFILE_BUILD:
FUNCTION
ARG DOCKERFILE_TARGET
ARG DOCKER_IMAGE_NAME
ARG RELEASE_TAG
FROM DOCKERFILE -f Dockerfile --target ${DOCKERFILE_TARGET} .
FROM DOCKERFILE --build-arg GRADLE_BUILD_ARGS=${GRADLE_BUILD_ARGS} -f Dockerfile --target ${DOCKERFILE_TARGET} .
SAVE IMAGE ${DOCKER_IMAGE_NAME}:latest
IF [ ! -z "$RELEASE_TAG" ]
RUN mv /app/*jar /app/${DOCKERFILE_TARGET}-${RELEASE_TAG}.jar
RUN md5sum /app/*jar > /app/${DOCKERFILE_TARGET}-${RELEASE_TAG}.jar.md5sum


gradle-build:
FROM DOCKERFILE \
--build-arg GRADLE_BUILD_ARGS="${GRADLE_BUILD_ARGS}" \
-f Dockerfile --target build .
ARG RELEASE_TAG=$(grep version gradle.properties | awk '{print $NF}')
LET APP_JAR_PATH=""
LET APP_JAR_NAME=""
RUN mkdir -p /release
FOR target IN $DOCKER_IMAGES_TARGETS
IF [ $target = "ledger-sync" ]
SET APP_JAR_PATH="/app/application/build/libs"
SET APP_JAR_NAME=${DOCKER_IMAGE_PREFIX}-${RELEASE_TAG}.jar
ELSE
SET APP_JAR_PATH="/app/${target}-app/build/libs"
SET APP_JAR_NAME=${DOCKER_IMAGE_PREFIX}-${target}-${RELEASE_TAG}.jar
END
RUN mv ${APP_JAR_PATH}/*jar /release/${APP_JAR_NAME}
RUN md5sum /release/${APP_JAR_NAME} > /release/${APP_JAR_NAME}.md5sum
END
SAVE ARTIFACT /app/* AS LOCAL build/
SAVE ARTIFACT /release/* AS LOCAL release/

ledger-sync:
ARG EARTHLY_TARGET_NAME
ARG RELEASE_TAG
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
WAIT
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}
END
DO functions+DOCKER_TAG_N_PUSH \
--PUSH=$PUSH \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX} \
--RELEASE_TAG=${RELEASE_TAG}
--DOCKER_IMAGES_EXTRA_TAGS="${DOCKER_IMAGES_EXTRA_TAGS}"

aggregation:
ARG EARTHLY_TARGET_NAME
ARG RELEASE_TAG
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
WAIT
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}-${EARTHLY_TARGET_NAME}
END
DO functions+DOCKER_TAG_N_PUSH \
--PUSH=$PUSH \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}-${EARTHLY_TARGET_NAME} \
--RELEASE_TAG=${RELEASE_TAG}
--DOCKER_IMAGES_EXTRA_TAGS="${DOCKER_IMAGES_EXTRA_TAGS}"

streamer:
ARG EARTHLY_TARGET_NAME
ARG RELEASE_TAG
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
WAIT
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}-${EARTHLY_TARGET_NAME}
END
DO functions+DOCKER_TAG_N_PUSH \
--PUSH=$PUSH \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}-${EARTHLY_TARGET_NAME} \
--RELEASE_TAG=${RELEASE_TAG}
--DOCKER_IMAGES_EXTRA_TAGS="${DOCKER_IMAGES_EXTRA_TAGS}"

scheduler:
ARG EARTHLY_TARGET_NAME
ARG RELEASE_TAG
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
WAIT
DO +TEMPLATED_DOCKERFILE_BUILD \
--DOCKERFILE_TARGET=${EARTHLY_TARGET_NAME} \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}-${EARTHLY_TARGET_NAME}
END
DO functions+DOCKER_TAG_N_PUSH \
--PUSH=$PUSH \
--DOCKER_IMAGE_NAME=${DOCKER_IMAGE_PREFIX}-${EARTHLY_TARGET_NAME} \
--RELEASE_TAG=${RELEASE_TAG}
--DOCKER_IMAGES_EXTRA_TAGS="${DOCKER_IMAGES_EXTRA_TAGS}"

docker-compose-up:
LOCALLY
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group = org.cardanofoundation
version = 0.1.1-SNAPSHOT
version = 0.0.0-dummy-SNAPSHOT

0 comments on commit e03e882

Please sign in to comment.