Skip to content

Commit

Permalink
Merge pull request #5 from orange-cloudfoundry/fix-github-actions
Browse files Browse the repository at this point in the history
Fix GitHub actions and resync change with k3s-package-br
  • Loading branch information
o-orand authored Jan 17, 2024
2 parents 18906c6 + 8ebbdc4 commit ab7a6ff
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 21 deletions.
93 changes: 90 additions & 3 deletions .github/workflows/on-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,104 @@ on:
push:
jobs:
build:
name: "Test dev and final release creation"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
id: checkout_repo
uses: actions/checkout@v4
with:
fetch-depth: 0
path: ./create-bosh-release-action
- name: Checkout repository test repo
id: checkout_test_repo
uses: actions/checkout@v4
with:
repository: 'orange-cloudfoundry/create-bosh-release-action-test-boshrelease'
fetch-depth: 0

- name: Test dev release
# ref: v1.0
token: ${{ secrets.CREATE_BOSH_RELEASE_ACTION_TEST_BOSHRELEASE_GIT_TOKEN }}
path: ./create-bosh-release-action-test-boshrelease
- name: Setup test
run: |
git clone create-bosh-release-action-test-boshrelease dev-release-creation-test
git clone create-bosh-release-action-test-boshrelease final-release-creation-test
- name: Test dev release creation
id: test_dev_release
uses: orange-cloudfoundry/create-bosh-release-action@master
uses: ./create-bosh-release-action/ # uses: ./.github/actions/my-private-repo/my-action #https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-an-action-inside-a-different-private-repository-than-the-workflow
with:
target_branch: main
dir: dev-release-creation-test
- name: Generated test dev files status
run: |
ls -lrt
cd dev-release-creation-test
echo "Display dev-release-creation-test(create-bosh-release-action-test-boshrelease) git repo status:"
git status
echo "List existing tags:"
git tag --list
cd ..
- name: Test final release creation existing tag
id: test_final_release
uses: ./create-bosh-release-action/ # uses: ./.github/actions/my-private-repo/my-action #https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-an-action-inside-a-different-private-repository-than-the-workflow
with:
repository: orange-cloudfoundry/create-bosh-release-action-test-boshrelease
token: ${{ secrets.CREATE_BOSH_RELEASE_ACTION_TEST_BOSHRELEASE_GIT_TOKEN }}
target_branch: main
dir: final-release-creation-test
tag_name: "v0.3.0"
override_existing: "true"
debug: 1
- name: Generated final files status - override
run: |
ls -lrt
cd final-release-creation-test
echo "Display final-release-creation-test(create-bosh-release-action-test-boshrelease) git repo status:"
git status
echo "Checking tgz"
ls -l create-bosh-release-action-test-boshrelease-0.3.0.tgz
echo "List existing tags:"
git tag --list
echo "Ensure tag exist v0.3.0"
git tag --list|grep -E "^v0.3.0$"
cd ..
- name: Get Next Version
id: semver-final-release
run: |
cd create-bosh-release-action-test-boshrelease
latest_tag=$(git describe --tags --abbrev=0)
echo "latest tag found: $latest_tag"
major_minor=$(echo $latest_tag|cut -d'.' -f1-2)
patch=$(echo $latest_tag|cut -d'.' -f3)
next_patch=$((patch + 1))
nextVersion=${major_minor}.$next_patch
echo "nextVersion: $nextVersion"
echo "nextVersion=$nextVersion" >> "$GITHUB_OUTPUT"
cd ..
- name: Test final release creation new tag
id: test_final_new_release
uses: ./create-bosh-release-action/ # uses: ./.github/actions/my-private-repo/my-action #https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-an-action-inside-a-different-private-repository-than-the-workflow
with:
repository: orange-cloudfoundry/create-bosh-release-action-test-boshrelease
token: ${{ secrets.CREATE_BOSH_RELEASE_ACTION_TEST_BOSHRELEASE_GIT_TOKEN }}
target_branch: main
dir: final-release-creation-test
tag_name: "${{ steps.semver-final-release.outputs.nextVersion }}"
override_existing: "false"
debug: 1
- name: Generated final files status - new
env:
NEXT_VERSION: "${{ steps.semver-final-release.outputs.nextVersion }}"
run: |
ls -lrt
cd final-release-creation-test
echo "Display final-release-creation-test(create-bosh-release-action-test-boshrelease) git repo status:"
git status
echo "Checking tgz"
expected_version=${NEXT_VERSION#v}
ls -l create-bosh-release-action-test-boshrelease-$expected_version.tgz
echo "List existing tags:"
git tag --list
echo "Ensure tag exist"
git tag --list|grep -E "^$NEXT_VERSION$"
cd ..
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ Release directory path if not current working directory
required: `false`
default: `.`

### `debug`
Set to 1 to enable debug mode

default: 0
## Outputs

### `file`

Name of the generated release.

### `version`

version of the generated bosh release

### `need_gh_release`

Do we need to create a GitHub release associated to this bosh release

## Example usage

```
Expand All @@ -42,3 +54,5 @@ env:
AWS_BOSH_ACCES_KEY_ID: ${{ secrets.AWS_BOSH_ACCES_KEY_ID }}
AWS_BOSH_SECRET_ACCES_KEY: ${{ secrets.AWS_BOSH_SECRET_ACCES_KEY }}
```

See [GitHub action for extra samples](./.github/workflows/on-commit.yml)
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ name: "bosh-releaser"
description: "Creates new version of bosh release"

inputs:
repository:
description: "Github repository used to publish releases."
required: false
default: ${{github.repository}}
token:
description: "Github token used to push changes after releasing"
required: false
default: ${{github.token}}
target_branch:
description: "Branch name to push release commit"
required: false
Expand All @@ -17,11 +25,16 @@ inputs:
dir:
description: "Release directory path if not current working directory (default: .)"
default: "."
debug:
description: "Set to 1 to enable debug mode"
default: 0
outputs:
file:
description: "generated bosh release filename"
version:
description: "version of the generated bosh release"
need_gh_release:
description: "Do we need to create a github release associated to this bosh release"

runs:
using: "docker"
Expand Down
99 changes: 81 additions & 18 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
version=${GITHUB_REF#refs/tags/}
version=${version#v}
tag_version=v${version}
release=true
elif [[ "$GITHUB_REF" == refs/heads/* ]]; then
version=${GITHUB_REF#refs/heads/}
Expand All @@ -15,9 +16,37 @@ elif [[ "$GITHUB_REF" == refs/pull/* ]]; then
version=pr-${pull_number}
release=false
fi
cd $DIR # We ensure we are in the right directory

echo "*** Inputs ***"
echo " dir: $INPUT_DIR"
echo " repository: $INPUT_REPOSITORY"
echo " token: **redacted**"
echo " override_existing: $INPUT_OVERRIDE_EXISTING"
echo " tag_name: $INPUT_TAG_NAME"
echo " target_branch: $INPUT_TARGET_BRANCH"
echo " debug: $INPUT_DEBUG"

if [[ "$INPUT_DIR" != "." ]];then
cd $INPUT_DIR # We ensure we are in the right directory
git config --global --add safe.directory /github/workspace/$INPUT_DIR
fi

if [ -n "$INPUT_TAG_NAME" ];then
echo "Tag_name detected. Overriding version name and enabling final release"
version=$INPUT_TAG_NAME
version=${version#v}
tag_version=v${version}
release=true
fi


if [ "$INPUT_DEBUG" -ne 0 ];then
echo "Current files before release creation:"
ls -l
fi

PUSH_OPTIONS=""
if [ "${OVERRIDE_EXISTING}" == "true" ];then
if [ "${INPUT_OVERRIDE_EXISTING}" == "true" ];then
PUSH_OPTIONS="$PUSH_OPTIONS --force"
fi

Expand All @@ -26,25 +55,37 @@ if [ "${name}" = "null" ]; then
name=$(yq -r .name config/final.yml)
fi

remote_repo="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${GITHUB_SERVER_URL#https://}/${GITHUB_REPOSITORY}.git"
remote_repo="https://${GITHUB_ACTOR}:${INPUT_TOKEN}@${GITHUB_SERVER_URL#https://}/${INPUT_REPOSITORY}.git"

# configure git
git config --global user.name "actions/bosh-packager@v2"
git config --global user.email "<>"
git config --global --add safe.directory /github/workspace
echo "*** Git global config ***"
git --no-pager config --global --list

# remove existing release if any, and prepare a commit that will be amended next
# Having a single amended commit makes it easier to inspect last commit
# See https://superuser.com/a/360986/299481 for details of the bash array syntax
NEXT_GIT_COMMIT_FLAGS=(-m "cutting release ${version}")
FIRST_FINAL_RELEASE="false"
if [ "${release}" == "true" ]; then
# remove existing release if any
if [ -f releases/"${name}"/"${name}"-"${version}".yml ]; then
echo "removing pre-existing version ${version}"
yq -r "{ \"builds\": (.builds | with_entries(select(.value.version != \"${version}\"))), \"format-version\": .[\"format-version\"]}" < releases/${name}/index.yml > tmp
mv tmp releases/"${name}"/index.yml
rm -f releases/"${name}"/"${name}"-"${version}".yml
git commit -a -m "reset release ${version}"
git add releases/${name}/${name}-${version}.yml releases/${name}/index.yml
git commit -a "${NEXT_GIT_COMMIT_FLAGS[@]}"
NEXT_GIT_COMMIT_FLAGS=(--amend -m "cutting release ${version} overriding existing one")
else
FIRST_FINAL_RELEASE="true"
fi
fi

if [ -n "${AWS_BOSH_ACCES_KEY_ID}" ]; then
echo "Generating AWS config"
cat - > config/private.yml <<EOS
---
blobstore:
Expand All @@ -58,33 +99,55 @@ fi

echo "creating bosh release: ${name}-${version}.tgz"
if [ "${release}" == "true" ]; then
bosh create-release --force --final --version="${version}" --tarball="${name}-${version}".tgz
bosh create-release --final --version="${version}" --tarball="${name}-${version}".tgz --force # --force is required to ignore dev_releases/ dir, created during final release
else
bosh create-release --force --timestamp-version --tarball="${name}-${version}".tgz
fi

NEED_GITHUB_RELEASE="false"
if [ "${release}" == "true" ]; then
echo "pushing changes to git repository"
git add .final_builds
if [ -d .final_builds ];then
git add .final_builds
fi
git add releases/${name}/index.yml
git add releases/${name}/${name}-${version}.yml
git commit -a -m "cutting release ${version}"
RELEASE_FILE_NAME=releases/${name}/${name}-${version}.yml
git add ${RELEASE_FILE_NAME}
# Note: if we had removed the previous release, then we amend the commit.
git commit -a "${NEXT_GIT_COMMIT_FLAGS[@]}"

# Override any existing tag with same version. This may happen if only part of the renovate PRs were merged
git tag -a -m "cutting release ${version}" ${version} $PUSH_OPTIONS
echo "Inspecting staged files to skip commit and push if there is no blob changes in the release"
git show HEAD ${RELEASE_FILE_NAME}
if [[ $FIRST_FINAL_RELEASE == false ]] && ! git show HEAD ${RELEASE_FILE_NAME} | grep sha1 ; then
echo "No sha1 found in diff in ${RELEASE_FILE_NAME}. No blob were modified. Skipping the git push"
ls -al ${RELEASE_FILE_NAME}
cat ${RELEASE_FILE_NAME}
NEED_GITHUB_RELEASE="false"
else
echo "pushing changes to git repository"
# Override any existing tag with same version. This may happen if only part of the renovate PRs were merged
git tag -a -m "cutting release ${tag_version}" ${tag_version} $PUSH_OPTIONS
# In case a renovate PR was merged in between, try to rebase prior to pushing
git pull --rebase ${remote_repo}
if [[ "${INPUT_OVERRIDE_EXISTING}" == "true" ]]; then
# Delete any existing release with same tag. Ignore push failure if no tag exists.
! git push --delete ${remote_repo} ${version}
fi

git pull --rebase ${remote_repo}
if [ "${OVERRIDE_EXISTING}" == "true" ] && git rev-parse "$version" >/dev/null 2>&1; then
# Delete any existing release with same tag. Ignore push failure if no tag exists.
git push --delete ${remote_repo} ${version}
git push ${remote_repo} HEAD:${INPUT_TARGET_BRANCH} --follow-tags # Push branch and tag
NEED_GITHUB_RELEASE="true"
fi
fi

git push ${remote_repo} HEAD:${INPUT_TARGET_BRANCH} --follow-tags # Push branch and tag
if [ "$INPUT_DEBUG" -ne 0 ];then
echo "Current files after release creation:"
ls -l
fi


# make asset readable outside docker image
chmod 644 ${name}-${version}.tgz
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files
echo "file=${name}-${version}.tgz" >> $GITHUB_OUTPUT
echo "version=${version}" >> $GITHUB_OUTPUT
echo "file=${name}-${version}.tgz" >> $GITHUB_OUTPUT
echo "version=${version}" >> $GITHUB_OUTPUT
echo "need_gh_release=${NEED_GITHUB_RELEASE}" >> $GITHUB_OUTPUT

0 comments on commit ab7a6ff

Please sign in to comment.