Skip to content

Commit

Permalink
Test new approach using reusable worfklows
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRuiz7 committed Dec 21, 2023
1 parent a07eae3 commit 2d74353
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 27 deletions.
68 changes: 42 additions & 26 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@ name: Build packages
# - Run manually
on:
workflow_dispatch:
# TODO dynamic matrix or filtering complete matrix based on inputs
# inputs:
# distribution:
# description: 'One of [ "tar", "rpm", "deb", "all" ]'
# default: 'all'
# required: false
# type: string
# architecture:
# description: 'One of [ "x64", "arm64", "all" ]'
# default: 'x64'
# required: true
# type: string

# TODO para que esto funcione con reusable workflows hay que moverlos a variables
# TODO para pasar el nombre del paquete del stage build al stage assemble hay que usar outputs

# ==========================
# Bibliography
# ==========================
#
# * Reusable workflows: limitations
# | https://docs.github.com/en/actions/using-workflows/reusing-workflows#limitations
# * Using matrix in reusable workflows:
# | https://docs.github.com/en/actions/using-workflows/reusing-workflows#using-a-matrix-strategy-with-a-reusable-workflow
# * Reading input from the called workflow
# | https://docs.github.com/en/enterprise-cloud@latest/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_callinputs

# Used to run locally using https://github.com/nektos/act
env:
ACT:
VERSION: 2.11.0
SNAPSHOT: false
PLATFORM: linux
BUILD: bash scripts/build.sh
ASSEMBLE: bash scripts/assemble.sh
ACT: # Used to run locally using https://github.com/nektos/act

jobs:
build:
Expand All @@ -31,10 +51,10 @@ jobs:
DISTRIBUTION: [tar, rpm, deb]
ARCHITECTURE: [x64, arm64]
outputs:
package_name: ${{ steps.get_package_name.outputs.package_name }}
package: ${{ steps.r_build.outputs.package }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 11
Expand All @@ -43,23 +63,19 @@ jobs:
uses: gradle/gradle-build-action@v2.9.0

- name: Run `build.sh`
run: |
$BUILD -v $VERSION -s $SNAPSHOT -p $PLATFORM -a ${{ matrix.ARCHITECTURE }} -d ${{ matrix.DISTRIBUTION }}
# The package name is stored in the artifacts/artifact_name.txt file
- name: Read package name
id: get_package_name
run: |
echo $(ls -la)
echo "package_name=$(cat artifacts/artifact_name.txt)" >> $GITHUB_OUTPUT
echo "$(cat artifacts/artifact_name.txt)"
- name: Upload artifact
uses: actions/upload-artifact@v3
id: r_build
uses: ./.github/workflows/r_build.yml
with:
architecture: ${{ matrix.ARCHITECTURE }}
distribution: ${{ matrix.DISTRIBUTION }}

- name: Run `assemble.sh`
uses: ./.github/workflows/r_assemble.yml
with:
name: ${{ steps.get_package_name.outputs.package_name }}
path: artifacts/dist/${{ steps.get_package_name.outputs.package_name }}
if-no-files-found: error
architecture: ${{ matrix.ARCHITECTURE }}
distribution: ${{ matrix.DISTRIBUTION }}
package: ${{ needs.build.outputs.package }}
# TODO comprobar si usa la misma máquina y hay acceso a ./artifacts/dist

# assemble:
# # needs: [build]
Expand All @@ -79,7 +95,7 @@ jobs:
# # ARCHITECTURE: [ x64, arm64 ]
# steps:
# - name: Download artifact
# uses: actions/download-artifact@v3
# uses: actions/download-artifact@v4
# with:
# name: wazuh-indexer-*${{ matrix.ARCHITECTURE }}*${{ matrix.DISTRIBUTION }}
# # name: ${{ needs.build.outputs.package_name }}
Expand Down
69 changes: 69 additions & 0 deletions .github/workflows/r_assemble.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Assemble (reusable)

# This workflow runs when any of the following occur:
# - Run from another workflow
on:
workflow_call:
inputs:
distribution:
description: 'One of [ "tar", "rpm", "deb" ]'
default: 'rpm'
required: false
type: string
architecture:
description: 'One of [ "x64", "arm64" ]'
default: 'x64'
required: false
type: string
package:
required: true
type: string
# outputs:
# package:
# description: "The package's name"
# value: ${{ jobs.build.outputs.package }}


jobs:
build:
# runs-on: ${{ inputs.architecture }}
runs-on: ubuntu-latest
# Permissions to upload the package
permissions:
packages: write
contents: read
# outputs:
# package: ${{ steps.get_name.outputs.name }}
steps:
- name: Read artifacts
run: |
ls -l ./artifacts/dist
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: ${{ inputs.package }}
path: ${{ inputs.architecture }}/${{ inputs.distribution }}

# - name: Run `assemble.sh`
# run: |
# bash scripts/assemble.sh -v ${{ vars.OPENSEARCH_VERSION }} -p linux -a ${{ inputs.architecture }} -d ${{ inputs.distribution }}

# # The package name is stored in the artifacts/artifact_name.txt file.
# # The variable name is generated dynamically: rpm_x64
# - name: Set package name
# id: get_name
# run: |
# echo $(ls -la)
# # ${{ inputs.distribution }}_${{ inputs.architecture }}
# echo "name=$(cat artifacts/artifact_name.txt)" >> $GITHUB_OUTPUT
# echo "$(cat artifacts/artifact_name.txt)"

# - name: Upload artifact
# uses: actions/upload-artifact@v4
# with:
# name: ${{ steps.get_name.outputs.name }}
# path: ${{ inputs.architecture }}/${{ inputs.distribution }}
# if-no-files-found: error


65 changes: 65 additions & 0 deletions .github/workflows/r_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Build (reusable)

# This workflow runs when any of the following occur:
# - Run from another workflow
on:
workflow_call:
inputs:
distribution:
description: 'One of [ "tar", "rpm", "deb" ]'
default: 'rpm'
required: false
type: string
architecture:
description: 'One of [ "x64", "arm64" ]'
default: 'x64'
required: false
type: string
outputs:
package:
description: "The package's name"
value: ${{ jobs.build.outputs.package }}

# TODO version must to be read from the VERSION file
# TODO revision must be an input
env:
version: 4.9.0
revision: 1

jobs:
build:
# runs-on: ${{ inputs.architecture }}
runs-on: ubuntu-latest
# Permissions to upload the package
permissions:
packages: write
contents: read
outputs:
package: ${{ steps.get_name.outputs.name }}
steps:
- name: Run `build.sh`
run: |
bash scripts/build.sh -v ${{ vars.OPENSEARCH_VERSION }} -s false -p linux -a ${{ inputs.architecture }} -d ${{ inputs.distribution }}
# The package name is stored in the artifacts/artifact_name.txt file.
# The variable name is generated dynamically: rpm_x64
- name: Set package name
id: get_name
run: |
echo $(ls -la)
# ${{ inputs.distribution }}_${{ inputs.architecture }}
echo "name=$(cat artifacts/artifact_name.txt)" >> $GITHUB_OUTPUT
echo "$(cat artifacts/artifact_name.txt)"
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
# name: wazuh-indexer-min_${{ env.version }}-${{ env.revision }}_${{ inputs.architecture }}_${{ github.sha }}.${{ inputs.distribution }}
# example: x64/deb/wazuh-indexer_4.8.0-rc1_x64_ff98475f.deb
# TODO x64 -> deb: amd64, rpm: x86_64
name: ${{ steps.get_name.outputs.name }}
path: ${{ inputs.architecture }}/${{ inputs.distribution }}
if-no-files-found: error



4 changes: 3 additions & 1 deletion scripts/assemble.sh
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,12 @@ function assemble_rpm() {

# Move the root folder, copy the package and clean.
cd ../../..
cp "${TMP_DIR}/RPMS/${SUFFIX}/wazuh-indexer-${version}-1.${SUFFIX}.${EXT}" "${OUTPUT}/dist/"
package_name="wazuh-indexer-${version}-1.${SUFFIX}.${EXT}"
cp "${TMP_DIR}/RPMS/${SUFFIX}/${package_name}" "${OUTPUT}/dist/"
echo "Cleaning temporary ${TMP_DIR} folder"
rm -r "${TMP_DIR}"
echo "After execution, shell path is $(pwd)"
echo "${package_name}" > "${OUTPUT}/artifact_name.txt"
}

case $SUFFIX.$EXT in
Expand Down

0 comments on commit 2d74353

Please sign in to comment.