fix mistake #5
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
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
name: Publish charm | ||
on: | ||
workflow_call: | ||
inputs: | ||
channel: | ||
required: true | ||
type: string | ||
description: 'Destination Channel' | ||
charm-working-directory: | ||
type: string | ||
description: The working directory for the charm | ||
docs-working-directory: | ||
type: string | ||
description: The working directory for the docs | ||
working-directory: | ||
type: string | ||
description: The working directory for jobs | ||
default: "./" | ||
tag-prefix: | ||
type: string | ||
required: false | ||
description: | | ||
Tag prefix, useful when bundling multiple charms in the same repo. | ||
charmcraft-ref: | ||
description: Used in conjunction with charmcraft-repository to pull and build charmcraft from source instead of using snapstore version. | ||
type: string | ||
default: "" | ||
charmcraft-repository: | ||
description: Pull and build charmcraft from source instead of using snapstore version (this means that the `charmcraft-channel` input will be ignored). | ||
type: string | ||
default: "" | ||
charmcraft-channel: | ||
description: Charmcraft channel to use for the integration test | ||
type: string | ||
default: latest/stable | ||
env: | ||
REGISTRY: ghcr.io | ||
OWNER: ${{ github.repository_owner }} | ||
jobs: | ||
branch-up-to-date-check-enabled: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: octokit/[email protected] | ||
id: get-branch-protection | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | ||
with: | ||
route: GET /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks | ||
repo: ${{ github.event.repository.name }} | ||
owner: ${{ github.event.repository.owner.login }} | ||
branch: main | ||
- run: | | ||
if [ ${{ fromJson(steps.get-branch-protection.outputs.data).strict }} != "true" ]; then | ||
echo "::error::Strict checks are not enabled for this repository" | ||
exit 1 | ||
fi | ||
draft-publish-docs: | ||
name: Draft publish docs | ||
needs: [ branch-up-to-date-check-enabled ] | ||
runs-on: ubuntu-22.04 | ||
defaults: | ||
run: | ||
working-directory: ${{ inputs.docs-working-directory || inputs.working-directory }} | ||
steps: | ||
- uses: actions/[email protected] | ||
- name: Search for docs folder | ||
id: docs-exist | ||
run: echo "docs_exist=$([[ -d docs ]] && echo 'True' || echo 'False')" >> $GITHUB_OUTPUT | ||
- name: Publish documentation | ||
if: steps.docs-exist.outputs.docs_exist == 'True' | ||
uses: canonical/discourse-gatekeeper@main | ||
with: | ||
discourse_host: discourse.charmhub.io | ||
discourse_api_username: ${{ secrets.DISCOURSE_API_USERNAME }} | ||
discourse_api_key: ${{ secrets.DISCOURSE_API_KEY }} | ||
dry_run: true | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
charm_dir: ${{ inputs.docs-working-directory || inputs.working-directory }} | ||
get-runner-image: | ||
name: Get runner image | ||
uses: ./.github/workflows/get_runner_image.yaml | ||
with: | ||
working-directory: ${{ inputs.charm-working-directory || inputs.working-directory }} | ||
release-charm-libs: | ||
name: Release charm libs | ||
needs: [ branch-up-to-date-check-enabled ] | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: actions/[email protected] | ||
- if: inputs.charm-working-directory != '' | ||
name: Change directory | ||
run: | | ||
set -x | ||
TEMP_DIR=$(mktemp -d) | ||
cp -rp ${{inputs.charm-working-directory}}/. $TEMP_DIR | ||
rm -rf .* * || : | ||
cp -rp $TEMP_DIR/. . | ||
rm -rf $TEMP_DIR | ||
- uses: canonical/charming-actions/[email protected] | ||
name: Release libs | ||
with: | ||
credentials: ${{ secrets.CHARMHUB_TOKEN }} | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
get-run-id: | ||
name: Get workflow run id | ||
needs: [ branch-up-to-date-check-enabled ] | ||
runs-on: ubuntu-22.04 | ||
outputs: | ||
run-id: ${{ env.RUN_ID }} | ||
steps: | ||
# - name: Get workflow run id | ||
# if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' || github.event_name == 'pull_request' }} | ||
# shell: bash | ||
# run: | | ||
# set -x | ||
# # Get commit info | ||
# TREE_SHA=$(gh api \ | ||
# -H "Accept: application/vnd.github+json" \ | ||
# -H "X-GitHub-Api-Version: 2022-11-28" \ | ||
# /repos/${{ github.repository }}/commits/${GITHUB_SHA} \ | ||
# --jq '.commit.tree.sha') | ||
# # Get workflow run id from this specific tree id, paginate until found | ||
# TOTAL_COUNT=$(gh api \ | ||
# --method GET \ | ||
# -H "Accept: application/vnd.github+json" \ | ||
# -H "X-GitHub-Api-Version: 2022-11-28" \ | ||
# /repos/${{ github.repository }}/actions/runs \ | ||
# -f status=completed \ | ||
# -f event=pull_request | jq ".total_count") | ||
# PER_PAGE=100 | ||
# MAX_PAGES=$(( (TOTAL_COUNT + 99) / $PER_PAGE )) | ||
# PAGE=1 | ||
# while true; do | ||
# RESULT=$( gh api \ | ||
# --method GET \ | ||
# -H "Accept: application/vnd.github+json" \ | ||
# -H "X-GitHub-Api-Version: 2022-11-28" \ | ||
# /repos/${{ github.repository }}/actions/runs \ | ||
# -f page=$PAGE \ | ||
# -f per_page=$PER_PAGE \ | ||
# -f status=completed \ | ||
# -f event=pull_request \ | ||
# --jq "[ | ||
# .workflow_runs[] | ||
# | select(.path == \".github/workflows/integration_test.yaml\") | ||
# | select(.head_commit.tree_id == \"$TREE_SHA\") | ||
# ] | max_by(.updated_at) | .id" | ||
# ) | ||
# if [[ -n $RESULT ]]; then | ||
# RUN_ID=$RESULT | ||
# break | ||
# fi | ||
# if [[ "PAGE" -eq "$MAX_PAGES" ]]; then | ||
# echo "::error::No workflow run id found for specific tree id" | ||
# exit 1 | ||
# fi | ||
# ((PAGE++)) | ||
# done | ||
# echo "RUN_ID=$RUN_ID" >> $GITHUB_ENV | ||
# env: | ||
# GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | ||
# - name: Get current run id | ||
# if: ${{ github.event_name == 'pull_request' }} | ||
# shell: bash | ||
# run: | | ||
# echo "RUN_ID=${{ github.run_id }}" >> $GITHUB_ENV | ||
- name: Get current run id (hardcoded) | ||
shell: bash | ||
run: | | ||
echo "RUN_ID=8524820040" >> $GITHUB_ENV | ||
name: Get images | ||
runs-on: ubuntu-22.04 | ||
outputs: | ||
images: ${{ env.IMAGES }} | ||
steps: | ||
- uses: actions/[email protected] | ||
- name: Get images | ||
working-directory: ${{ inputs.working-directory }} | ||
run: | | ||
set -x | ||
lines=$(find . -type f -name rockcraft.yaml | wc -l) | ||
if [ $lines -ne 0 ]; then | ||
IMAGES=$(find . -type f -name rockcraft.yaml | xargs -l yq '.name' | jq -Rsc '. / "\n" - [""]') | ||
else | ||
IMAGES=$(ls *.Dockerfile 2> /dev/null | sed s/\.Dockerfile// | jq -Rsc '. / "\n" - [""]') | ||
fi | ||
echo "IMAGES=$IMAGES" >> $GITHUB_ENV | ||
publish-images: | ||
name: Publish images to charmhub | ||
runs-on: ${{ needs.get-runner-image.outputs.runs-on }} | ||
needs: [ get-images, get-run-id, get-runner-image] | ||
if: ${{ needs.get-images.outputs.images != '[]' }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
if: inputs.charmcraft-repository != '' | ||
with: | ||
repository: ${{ inputs.charmcraft-repository }} | ||
ref: ${{ inputs.charmcraft-ref }} | ||
path: ./charmcraft | ||
fetch-depth: 0 | ||
- name: Get Charmcraft SHA | ||
if: inputs.charmcraft-repository != '' | ||
id: charmcraft-sha | ||
working-directory: ./charmcraft | ||
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT | ||
- name: Restore Charmcraft Cache | ||
if: inputs.charmcraft-repository != '' | ||
id: restore-charmcraft | ||
uses: actions/cache/restore@v4 | ||
with: | ||
path: ./charmcraft*.snap | ||
key: charmcraft-${{ steps.charmcraft-sha.outputs.sha }} | ||
- name: Install Snapcraft | ||
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && inputs.charmcraft-repository != '' | ||
run: sudo snap install snapcraft --classic | ||
- name: Build Charmcraft | ||
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && inputs.charmcraft-repository != '' | ||
working-directory: ./charmcraft | ||
run: | | ||
snapcraft --use-lxd | ||
cp charmcraft*.snap ../ | ||
- name: Save Charmcraft Cache | ||
uses: actions/cache/save@v4 | ||
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && inputs.charmcraft-repository != '' | ||
with: | ||
path: ./charmcraft*.snap | ||
key: ${{ steps.restore-charmcraft.outputs.cache-primary-key }} | ||
- name: Install charmcraft | ||
if: inputs.charmcraft-repository != '' | ||
run: sudo snap install --dangerous --classic charmcraft*.snap | ||
- name: Clean up | ||
if: inputs.charmcraft-repository != '' | ||
run: rm -rf * | ||
- name: Install charmcraft | ||
if: inputs.charmcraft-repository == '' | ||
run: | | ||
sudo snap install charmcraft --classic --channel ${{ inputs.charmcraft-channel }} | ||
- uses: actions/[email protected] | ||
- if: inputs.charm-working-directory != '' | ||
name: Change directory | ||
run: | | ||
set -x | ||
TEMP_DIR=$(mktemp -d) | ||
cp -rp ${{inputs.charm-working-directory}}/. $TEMP_DIR | ||
rm -rf .* * || : | ||
cp -rp $TEMP_DIR/. . | ||
rm -rf $TEMP_DIR | ||
- name: Get charm name | ||
id: get-charm-name | ||
run: | | ||
set -x | ||
pwd | ||
ls | ||
CHARM_NAME="$([ -f metadata.yaml ] && yq '.name' metadata.yaml || echo UNKNOWN)" | ||
if [ "$CHARM_NAME" == "UNKNOWN" ]; then | ||
CHARM_NAME="$([ -f charmcraft.yaml ] && yq '.name' charmcraft.yaml || echo UNKNOWN)" | ||
fi | ||
echo "CHARM_NAME=$CHARM_NAME">> $GITHUB_ENV | ||
- name: Log in to the Container registry | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Download images artifact | ||
# if: ${{ github.event_name == 'push' }} | ||
run: | | ||
set -x | ||
gh run download ${{ needs.get-run-id.outputs.run-id }} -R ${{ github.repository }} -n ${{ env.CHARM_NAME }}-images | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
# - name: Download images artifact (for testing) | ||
# uses: actions/download-artifact@v4 | ||
# if: ${{ github.event_name == 'pull_request' }} | ||
# with: | ||
# name: ${{ env.CHARM_NAME }}-images | ||
- name: Publish image | ||
env: | ||
CHARMCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: "true" | ||
CHARMCRAFT_AUTH: ${{ secrets.CHARMHUB_TOKEN }} | ||
run: | | ||
set -x | ||
charmcraft expand-extensions | ||
pwd | ||
echo "looking for oci images" | ||
charm=${{ env.CHARM_NAME }} | ||
declare -a resources | ||
declare -a images | ||
if [ -f metadata.yaml ]; then | ||
for resource in $(yq -er '.resources | with_entries(select(.value.type=="oci-image")) | keys | join(" ")' metadata.yaml); do | ||
resources+=("$resource") | ||
done | ||
else | ||
for resource in $(yq -er '.resources | with_entries(select(.value.type=="oci-image")) | keys | join(" ")' <(charmcraft expand-extensions)); do | ||
resources+=("$resource") | ||
done | ||
fi | ||
for image in $(cat ${{ env.CHARM_NAME }}-images); do | ||
images+=("$image") | ||
done | ||
for image in "${images[@]}"; do | ||
resource=$(echo $image | awk -F '/' '{print $NF}' | cut -d ':' -f 1)-image | ||
if [[ " ${resources[@]} " =~ " $resource " ]]; then | ||
docker pull $image | ||
image_id=$(docker images $image --format "{{.ID}}") | ||
charmcraft upload-resource $charm $resource --image=$image_id --verbosity=brief | ||
fi | ||
done | ||
publish-charm: | ||
name: Publish charm to ${{ inputs.channel }} | ||
runs-on: ${{ needs.get-runner-image.outputs.runs-on }} | ||
needs: [get-run-id, get-runner-image, publish-images] | ||
if: ${{ !failure() }} | ||
steps: | ||
- uses: actions/[email protected] | ||
- if: inputs.charm-working-directory != './' | ||
name: Change directory | ||
run: | | ||
set -x | ||
TEMP_DIR=$(mktemp -d) | ||
cp -rp ${{inputs.charm-working-directory}}/. $TEMP_DIR | ||
rm -rf .* * || : | ||
cp -rp $TEMP_DIR/. . | ||
rm -rf $TEMP_DIR | ||
- name: Get charm name | ||
id: get-charm-name | ||
run: | | ||
set -x | ||
CHARM_NAME="$([ -f metadata.yaml ] && yq '.name' metadata.yaml || echo UNKNOWN)" | ||
if [ "$CHARM_NAME" == "UNKNOWN" ]; then | ||
CHARM_NAME="$([ -f charmcraft.yaml ] && yq '.name' charmcraft.yaml || echo UNKNOWN)" | ||
fi | ||
echo "CHARM_NAME=$CHARM_NAME">> $GITHUB_ENV | ||
- name: Download charm artifact | ||
# if: ${{ github.event_name == 'push' }} | ||
run: | | ||
set -x | ||
gh run download ${{ needs.get-run-id.outputs.run-id }} -R ${{ github.repository }} -n ${{ env.CHARM_NAME }}-charm | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
# - name: Download charm artifact (for testing) | ||
# uses: actions/download-artifact@v4 | ||
# if: ${{ github.event_name == 'pull_request' }} | ||
# with: | ||
# name: ${{ env.CHARM_NAME }}-charm | ||
# path: ${{ inputs.charm-working-directory }} | ||
- name: Get charm file | ||
run: echo "CHARM_FILE=$(ls ${{ env.CHARM_NAME }}_*.charm)" >> $GITHUB_OUTPUT | ||
- name: Upload charm to charmhub | ||
uses: canonical/charming-actions/[email protected] | ||
with: | ||
built-charm-path: ${{ env.CHARM_FILE }} | ||
credentials: ${{ secrets.CHARMHUB_TOKEN }} | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
channel: ${{ inputs.channel }} | ||
tag-prefix: ${{ inputs.tag-prefix }} | ||
upload-image: false | ||