Publish #52
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: Publish | |
on: | |
workflow_dispatch: | |
inputs: | |
tag: | |
description: "tag: The tag to use for this release" | |
required: true | |
default: v0.1.0 | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
update_formula_and_create_macos_bottle: | |
runs-on: macos-latest | |
steps: | |
- uses: Homebrew/actions/setup-homebrew@master | |
- run: rm -rf {*,.*} || true | |
- id: add_our_tap | |
run: brew --version && brew tap konveyor/move2kube && brew tap | |
- uses: actions/checkout@v2 | |
with: | |
repository: konveyor/move2kube | |
ref: ${{ github.event.inputs.tag }} | |
- id: get_sha | |
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT | |
- id: info | |
uses: konveyor/get-env-info@v1 | |
- run: rm -rf {*,.*} || true | |
- uses: actions/checkout@v2 | |
with: | |
ref: main | |
- run: brew test-bot --only-cleanup-before | |
- id: new_formula | |
name: create a new formula file for the tag | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
function normalizeForRubyClassName(x) { | |
return x.replace(/-\w/g, (c) => c.slice(1).toUpperCase()).replace(/\./g,'') | |
} | |
const fs = require('fs'); | |
const formula_file = './Formula/move2kube.rb'; | |
const tag = '${{ github.event.inputs.tag }}'.slice(1); // remove the v in v0.1.0 | |
const new_formula_file = './Formula/move2kube@' + tag + '.rb'; | |
const formula_file_data = fs.readFileSync(formula_file).toString('utf-8'); | |
const lines = formula_file_data.split('\n'); | |
const i1 = lines.findIndex(line => line.trim().startsWith('stable')); // Find the stable block | |
const i2 = i1 + lines.slice(i1).findIndex(line => line.trim().startsWith('tag')); // Find the tag in the stable block | |
const i3 = i2 + lines.slice(i2).findIndex(line => line.trim().startsWith('depends_on "go@')); // Find the Golang version line | |
lines[0] = `class Move2kubeAT${normalizeForRubyClassName(tag)} < Formula`; | |
lines[i2] = ' tag: "${{ github.event.inputs.tag }}",'; | |
lines[i2+1] = ' revision: "${{ steps.get_sha.outputs.sha }}"'; | |
lines[i3] = ' depends_on "go@${{ steps.info.outputs.go_version }}" => :build'; | |
const new_formula_file_data = lines.join('\n'); | |
fs.writeFileSync(new_formula_file, new_formula_file_data); | |
return new_formula_file; | |
- run: brew test-bot --only-setup | |
- run: ls -R | |
- run: cat ${{ steps.new_formula.outputs.result }} | |
- id: get_new_formula_filename | |
run: | | |
MY_NEW_FORMULA_NAME=$(basename ${{ steps.new_formula.outputs.result }}) | |
echo "filename=$MY_NEW_FORMULA_NAME" >> $GITHUB_OUTPUT | |
MY_DIR_PATH="/usr/local/Homebrew/Library/Taps/konveyor/homebrew-move2kube/Formula/" | |
echo "dirpath=${MY_DIR_PATH}" >> $GITHUB_OUTPUT | |
echo "outpath=${MY_DIR_PATH}${MY_NEW_FORMULA_NAME}" >> $GITHUB_OUTPUT | |
- run: ls ${{ steps.get_new_formula_filename.outputs.dirpath }} | |
- run: cp ${{ steps.new_formula.outputs.result }} ${{ steps.get_new_formula_filename.outputs.outpath }} || true | |
- run: ls ${{ steps.get_new_formula_filename.outputs.dirpath }} | |
- run: brew install --formula --build-bottle ${{ steps.get_new_formula_filename.outputs.outpath }} | |
# https://docs.brew.sh/Manpage#install-options-formulacask- | |
- run: brew bottle --json --root-url='https://github.com/konveyor/homebrew-move2kube/releases/download/${{ github.event.inputs.tag }}' ${{ steps.get_new_formula_filename.outputs.outpath }} | |
# https://docs.brew.sh/Manpage#bottle-options-installed_formulafile- | |
- uses: actions/upload-artifact@main | |
with: | |
name: bottles | |
path: | | |
${{fromJSON(steps.new_formula.outputs.result)}} | |
*.bottle.* | |
create_linux_bottle_then_tag_and_release: | |
needs: [update_formula_and_create_macos_bottle] | |
runs-on: ubuntu-latest | |
steps: | |
- uses: Homebrew/actions/setup-homebrew@master | |
- run: rm -rf {*,.*} || true | |
- uses: actions/checkout@v2 | |
with: | |
ref: main | |
- run: brew test-bot --only-cleanup-before | |
- id: new_formula | |
name: get new formula filename | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const tag = '${{ github.event.inputs.tag }}'.slice(1); | |
const new_formula_file = './Formula/move2kube@' + tag + '.rb'; | |
return new_formula_file; | |
- run: brew test-bot --only-setup | |
- uses: actions/download-artifact@v2 | |
with: | |
name: bottles | |
- run: ls -R | |
- run: cat ${{ steps.new_formula.outputs.result }} | |
- id: get_new_formula_filename | |
run: | | |
MY_NEW_FORMULA_NAME=$(basename ${{ steps.new_formula.outputs.result }}) | |
echo "filename=$MY_NEW_FORMULA_NAME" >> $GITHUB_OUTPUT | |
MY_DIR_PATH="/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/konveyor/homebrew-move2kube/Formula/" | |
echo "dirpath=${MY_DIR_PATH}" >> $GITHUB_OUTPUT | |
echo "outpath=${MY_DIR_PATH}${MY_NEW_FORMULA_NAME}" >> $GITHUB_OUTPUT | |
- run: ls ${{ steps.get_new_formula_filename.outputs.dirpath }} | |
- run: cp ${{ steps.new_formula.outputs.result }} ${{ steps.get_new_formula_filename.outputs.outpath }} || true | |
- run: ls ${{ steps.get_new_formula_filename.outputs.dirpath }} | |
- run: brew install --formula --build-bottle ${{ steps.get_new_formula_filename.outputs.outpath }} | |
# https://docs.brew.sh/Manpage#install-options-formulacask- | |
- run: brew bottle --json --root-url='https://github.com/konveyor/homebrew-move2kube/releases/download/${{ github.event.inputs.tag }}' ${{ steps.get_new_formula_filename.outputs.outpath }} | |
# https://docs.brew.sh/Manpage#bottle-options-installed_formulafile- | |
- name: update the bottle block in the formula | |
run: brew bottle --merge --write --no-commit *.json | |
- name: remove double hyphen from filenames | |
run: | | |
mkdir temp/ | |
mv -- *.bottle.tar.gz temp/ | |
for file_path in temp/*; do | |
file_name=$(basename "$file_path") | |
new_file_name="${file_name//--/-}" | |
mv "$file_path" "$new_file_name" | |
done | |
rmdir temp/ | |
- name: get info | |
run: curl -Lo releaseinfo.json 'https://move2kube.konveyor.io/releaseinfo.json' | |
- id: check | |
name: update the stable formula | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const tag = '${{ github.event.inputs.tag }}'; | |
const fs = require('fs'); | |
const rel_info = JSON.parse(fs.readFileSync('releaseinfo.json')); | |
console.log('release info', rel_info); | |
if(rel_info.current.release !== tag) return console.log('not updating stable formula'); | |
console.log('updating the stable formula'); | |
core.setOutput('proceed', true); | |
core.setOutput('version', tag.slice(1)); | |
- run: rm releaseinfo.json && echo '${{ steps.check.outputs.proceed }}' '${{ steps.check.outputs.version }}' | |
- if: ${{ steps.check.outputs.proceed }} | |
id: update-archives | |
run: | | |
for file in *.bottle.tar.gz | |
do | |
tar -xzf "$file" | |
mv 'move2kube@${{ steps.check.outputs.version }}' move2kube | |
new_name="${file//@*-/-}" | |
tar -czf "$new_name" move2kube/ | |
hash="$(sha256sum "$new_name" | awk '{print $1}')" | |
echo "new_name ${new_name} hash ${hash}" | |
echo "${new_name}=${hash}" >> $GITHUB_OUTPUT | |
rm -rf move2kube/ | |
done | |
ls ./*.tar.gz | |
- if: ${{ steps.check.outputs.proceed }} | |
name: update formula | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const names_to_hashes = Object.entries(${{ toJSON(steps.update-archives.outputs) }}); | |
console.log('names_to_hashes', names_to_hashes); | |
const filename = '${{ fromJSON(steps.new_formula.outputs.result) }}'; | |
const platform_regex = /:any_skip_relocation, ([^\s]+):\s*"[0-9a-f]+"$/; | |
const fs = require('fs'); | |
const find_hash = x => { | |
const platform = platform_regex.exec(x)[1]; | |
const name_and_hash = names_to_hashes.find(([k, v]) => k.includes(platform)); | |
if(name_and_hash) return name_and_hash[1]; | |
core.setFailed(`failed to find the hash of the .tar.gz file for the platform ${platform}`); | |
return 'deadbeef'; | |
}; | |
const change_hash = x => x.replace(/"[0-9a-f]+"$/, `"${find_hash(x)}"`); | |
const condition = x => /^\s+sha256 cellar.*"[0-9a-f]+"$/.test(x); | |
const lines = fs.readFileSync(filename).toString().split('\n').map(line => condition(line) ? change_hash(line) : line); | |
lines[0] = 'class Move2kube < Formula'; | |
fs.writeFileSync('./Formula/move2kube.rb', lines.join('\n')); | |
- if: ${{ steps.check.outputs.proceed }} | |
run: ls && cat ./Formula/move2kube.rb | |
- uses: EndBug/add-and-commit@v6 | |
with: | |
add: '["${{ fromJSON(steps.new_formula.outputs.result) }}", "./Formula/move2kube.rb"]' | |
author_name: move2kube | |
author_email: move2kube@gmail.com | |
branch: main | |
cwd: "." | |
message: "chore: update formula for new release. run_id ${{ github.run_id }}" | |
pull_strategy: "--ff-only" | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- id: get_sha | |
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT | |
- uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const tag = '${{ github.event.inputs.tag }}'; | |
const sha = '${{ steps.get_sha.outputs.sha }}'; | |
const release_title = `Move2Kube ${tag}`; | |
const release_body = 'This release contains the prebuilt bottles for homebrew'; | |
let tag_exists = false; | |
try { | |
const resp = await github.rest.git.getRef({...context.repo, ref: `tags/${tag}`}); | |
tag_exists = true; | |
core.info(`the tag ${tag} already exists on ${resp.data.object.type} ${resp.data.object.sha}`); | |
} catch(err) { | |
if(err.status !== 404){ | |
throw err; | |
} | |
} | |
if(tag_exists) { | |
core.info(`deleting the tag ${tag}`); | |
const resp = await github.rest.git.deleteRef({...context.repo, ref: `tags/${tag}`}); | |
} | |
core.info(`creating the tag ${tag} on the commit ${sha}`); | |
github.rest.git.createRef({ | |
...context.repo, | |
ref: `refs/tags/${tag}`, | |
sha | |
}); | |
// wait a few seconds to let the tag get created | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
await sleep(2000); | |
await github.rest.repos.createRelease({ | |
...context.repo, | |
tag_name: tag, | |
name: release_title, | |
body: release_body, | |
draft: false, | |
prerelease: false, | |
}); | |
- name: upload release assets | |
uses: konveyor/upload-release-action@v3 | |
with: | |
repo_token: ${{ secrets.GITHUB_TOKEN }} | |
tag: ${{ github.event.inputs.tag }} | |
file: "*.bottle.tar.gz" | |
file_glob: true | |
overwrite: true |