Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worflows #2

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
name: Docker image build and push; Generate Manifest; Create release tag

on:
workflow_dispatch

env:
REGISTRY: ghcr.io

defaults:
run:
shell: bash

jobs:
compare_image_versions:
name: Compare image versions
runs-on: ubuntu-22.04
outputs:
proceed_with_module_creation: ${{ steps.compare_versions.outputs.proceed }}
module_path: ${{ steps.get_current_docker_version.outputs.module_path }}
version: ${{ steps.get_current_docker_version.outputs.version }}
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Prepare repository name
id: prepare-repository-name
run: |
repository=$REGISTRY/${{ github.repository }}
echo "repository=${repository,,}" >> $GITHUB_OUTPUT

- name: Get docker version from container registry
id: get_version_from_container_reg
run: |
export GHCR_TOKEN=$(echo ${{ github.token }} | base64)
echo "version=$(curl -H "Authorization: Bearer $GHCR_TOKEN" https://ghcr.io/v2/sap/cap-operator-manager/tags/list | jq '.tags[]' | sort -V | tail -n 2 | head -n 1)" >> $GITHUB_OUTPUT

- name: Print docker version from container registry
run: "echo ${{ steps.get_version_from_container_reg.outputs.version }}"

- name: Get current docker version
id: get_current_docker_version
run: |
echo "version=$(yq eval '.images[0].newTag' config/default/kustomization.yaml)" >> $GITHUB_OUTPUT
echo "module_path=$(yq eval '.images[0].newName' config/default/kustomization.yaml)" >> $GITHUB_OUTPUT

- name: Print current docker module path and version
run: |
echo ${{ steps.get_current_docker_version.outputs.module_path }}
echo ${{ steps.get_current_docker_version.outputs.version }}

- name: Download semver tool
run: |
wget https://raw.githubusercontent.com/fsaintjacques/semver-tool/master/src/semver && \
chmod +x semver

- name: Compare versions
id: compare_versions
run: |
if [[ '${{ steps.get_version_from_container_reg.outputs.version }}' -eq '' ]]
then
echo "No version available in artifactory, we can proceed."
echo "proceed=true" >> $GITHUB_OUTPUT
else
case $(./semver compare ${{ steps.get_version_from_container_reg.outputs.version }} ${{ steps.get_current_docker_version.outputs.version }}) in
-1)
echo "Current version is higher than artifactory version, we can proceed."
echo "proceed=true" >> $GITHUB_OUTPUT
;;
0)
echo "Current version is same as the artifactory version, we won't proceed."
echo "proceed=false" >> $GITHUB_OUTPUT
;;
1)
echo "Current version is lower than artifactory version, we won't proceed."
echo "proceed=false" >> $GITHUB_OUTPUT
;;
esac
fi

docker_build_and_push:
name: Docker build and push
runs-on: ubuntu-22.04
needs: [compare_image_versions]
if: needs.compare_image_versions.outputs.proceed_with_module_creation == 'true'
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Extract metadata (tags, labels) for Docker
id: extract-metadata
uses: docker/metadata-action@v4
with:
images: ${{ needs.compare_image_versions.outputs.module_path }}
tags: |
type=pep440,pattern={{version}},value=${{ needs.compare_image_versions.outputs.version }}

- name: Build and push docker image
id: docker_build
uses: docker/build-push-action@v4
with:
file: Dockerfile
platforms: linux/amd64,linux/arm64
context: .
cache-from: |
type=gha,scope=sha-${{ github.sha }}
type=gha,scope=${{ github.ref_name }}
type=gha,scope=${{ github.base_ref || 'main' }}
type=gha,scope=main
cache-to: |
type=gha,scope=sha-${{ github.sha }},mode=max
type=gha,scope=${{ github.ref_name }},mode=max
push: true
tags: ${{ steps.extract-metadata.outputs.tags }}
labels: ${{ steps.extract-metadata.outputs.labels }}

- name: Print Outputs
run: |
echo "Module path: ${{ needs.compare_image_versions.outputs.module_path }}"
echo "Tags: ${{ steps.extract-metadata.outputs.tags }}"
echo "Labels: ${{ steps.extract-metadata.outputs.labels }}"

generate_manifests:
uses: ./.github/workflows/generate-manifest.yml
needs: [compare_image_versions, docker_build_and_push]
if: needs.compare_image_versions.outputs.proceed_with_module_creation == 'true'

create_release_tag:
name: Create release tag
runs-on: ubuntu-22.04
needs: [compare_image_versions, docker_build_and_push]
if: needs.compare_image_versions.outputs.proceed_with_module_creation == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Determine target commit
id: get_target_commit
run: |
sha=$(git rev-parse HEAD)
echo "Target commit: $sha"
echo "sha=$sha" >> $GITHUB_OUTPUT

- name: Wait for check suites to complete
uses: sap-contributions/await-check-suites@master
with:
ref: ${{ steps.get_target_commit.outputs.sha }}
intervalSeconds: 10
timeoutSeconds: 1800
failStepIfUnsuccessful: true
appSlugFilter: github-actions

- name: Create release
id: create_release
uses: ncipollo/release-action@v1
with:
tag: cap-operator-manager/v${{ needs.compare_image_versions.outputs.version }}
commit: ${{ steps.get_target_commit.outputs.sha }}
makeLatest: true
prerelease: false
allowUpdates: false
27 changes: 27 additions & 0 deletions .github/workflows/generate-manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This job will generate manifest
name: Generate Manifests

on:
workflow_call:

defaults:
run:
shell: bash

jobs:
generate-manifest:
name: Generate manifest
runs-on: ubuntu-22.04

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Download kustomize cli
run: |
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
chmod +x kustomize

- name: Generate Manifests
run: |
./kustomize build config/default/
173 changes: 173 additions & 0 deletions .github/workflows/publish-helm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
name: Publish Helm

on:
workflow_dispatch

env:
HELM_VERSION: v3.11.3
REGISTRY: ghcr.io
CHART_DIRECTORY: chart
PACKAGES: sap/cap-operator-helm
RELEASE_TAG_PREFIX: cap-operator-helm

jobs:
create_release_tag:
name: Create release tag
runs-on: ubuntu-22.04
outputs:
tag: ${{ steps.get_tag.outputs.tag }}
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- name: Checkout
uses: actions/checkout@v3

- uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}

- name: Determine target commit
id: get_target_commit
run: |
sha=$(git rev-parse HEAD)
echo "Target commit: $sha"
echo "sha=$sha" >> $GITHUB_OUTPUT

- name: Get tag
id: get_tag
run: |
chart_version=$(yq .version $CHART_DIRECTORY/Chart.yaml)
echo "tag=$RELEASE_TAG_PREFIX/$chart_version" >> $GITHUB_OUTPUT

- name: Wait for check suites to complete
uses: sap-contributions/await-check-suites@master
with:
ref: ${{ steps.get_target_commit.outputs.sha }}
intervalSeconds: 10
timeoutSeconds: 1800
failStepIfUnsuccessful: true
appSlugFilter: github-actions

- name: Create release
id: create_release
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.get_tag.outputs.tag }}
commit: ${{ steps.get_target_commit.outputs.sha }}
makeLatest: true
prerelease: false
allowUpdates: false

publish-to-pages:
name: Publish chart to github pages
runs-on: ubuntu-22.04
needs: create_release_tag
permissions:
contents: write
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.publish-index.outputs.page_url }}

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Create helm-index branch if missing
run: |
if ! git rev-parse --verify remotes/origin/helm-index &>/dev/null; then
git switch --orphan helm-index
git config user.name "${{ vars.WORKFLOW_USER_NAME }}"
git config user.email "${{ vars.WORKFLOW_USER_EMAIL }}"
git commit --allow-empty -m "Initial commit"
git push --set-upstream origin helm-index
fi

- name: Checkout repository
uses: actions/checkout@v3
with:
ref: helm-index
path: index

- uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}

- name: Setup Pages
uses: actions/configure-pages@v3

- name: Create package
run: |
chart_version=$(yq .version $CHART_DIRECTORY/Chart.yaml)
helm package --version $chart_version $CHART_DIRECTORY

- name: Create index
run: |
chart_version=$(yq .version $CHART_DIRECTORY/Chart.yaml)
helm repo index --url ${{ github.server_url }}/${{ github.repository }}/releases/download/${{ needs.create_release_tag.outputs.tag }} --merge ./index/index.yaml .
mv index.yaml index
cd index
git config user.name "${{ vars.WORKFLOW_USER_NAME }}"
git config user.email "${{ vars.WORKFLOW_USER_EMAIL }}"
git add index.yaml
git commit -m "Release ${{ needs.create_release_tag.outputs.tag }}"
git push

- name: Upload package
run: |
upload_url="${{ needs.create_release_tag.outputs.upload_url }}"
upload_url=${upload_url%%\{*\}}
chart_name=$(yq .name $CHART_DIRECTORY/Chart.yaml)
chart_version=$(yq .version $CHART_DIRECTORY/Chart.yaml)
file=$chart_name-$chart_version.tgz
echo "Uploading $file to $upload_url ..."
curl -sSf \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "Content-Type: $(file -b --mime-type $file)" \
--data-binary @$file \
"$upload_url?name=$(basename $file)"

- name: Upload index
uses: actions/upload-pages-artifact@v2
with:
path: index

- name: Publish index
id: publish-index
uses: actions/deploy-pages@v2

publish-to-packages:
name: Publish chart to github packages
needs: create_release_tag
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}

- name: Create package
run: |
chart_version=$(yq .version $CHART_DIRECTORY/Chart.yaml)
helm package --version $chart_version $CHART_DIRECTORY

- name: Login to the OCI registry
run: |
helm --registry-config $RUNNER_TEMP/helm-config.json registry login $REGISTRY -u ${{ github.actor }} --password-stdin <<< ${{ github.token }}

- name: Upload package
run: |
chart_name=$(yq .name $CHART_DIRECTORY/Chart.yaml)
chart_version=$(yq .version $CHART_DIRECTORY/Chart.yaml)
file=$chart_name-$chart_version.tgz
repository=$REGISTRY/$PACKAGES
helm --registry-config $RUNNER_TEMP/helm-config.json push $file oci://${repository,,}
Loading