Skip to content

Code factorisation to reduce duplication #5176

Code factorisation to reduce duplication

Code factorisation to reduce duplication #5176

Workflow file for this run

name: Centos7 CI (push and/or release)
on:
release:
types: [ created ]
push:
branches:
- develop
- dependabot/**
- feature/reserve_model_lot3_scalian
schedule:
- cron: '21 2 * * *'
workflow_call:
inputs:
run-tests:
required: true
type: string
target_branch:
required: true
type: string
env:
GITHUB_TOKEN: ${{ github.token }}
IS_RELEASE: ${{ github.event_name == 'workflow_dispatch' }}
IS_PUSH: ${{ github.event_name == 'push' }}
REF: ${{ inputs.target_branch =='' && github.ref_name || inputs.target_branch}}
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
#sparse checkout -- only needed files
- uses: actions/checkout@v6
with:
fetch-depth: 0
sparse-checkout: |
docker
src/vcpkg.json
ref: ${{ env.REF }}
- name: set env variables -- DockerFiles
run: |
echo "DOCKERFILE=$(pwd)/docker/Dockerfile" >> $GITHUB_ENV
echo "DOCKERDIR=$(pwd)/docker" >> $GITHUB_ENV
- name: check if DockerFiles has changed
run: |
# Check for changes in the target path
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD -- docker/AntaresDeps)
if [ -n "$CHANGED_FILES" ]; then
echo "publish_image=true" >> $GITHUB_ENV
else
echo "publish_image=false" >> $GITHUB_ENV
echo "No changes in DockerFiles, skipping image build and push."
fi
- name: Log in to Docker Hub
if: ${{ env.publish_image == 'true' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
#TODO for security issues, create and use DOCKERHUB_TOKEN
#password: ${{ secrets.DOCKERHUB_TOKEN }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
if: ${{ env.publish_image == 'true' }}
uses: docker/build-push-action@v6
with:
context: ${{ env.DOCKERDIR }}
file: ${{ env.DOCKERDIR }}/AntaresDeps
push: true
tags: antaresrte/antaressystemdeps:latest
- name: create vcpkg cache dir
run: |
echo "VCPKG_CACHE_DIR=$GITHUB_WORKSPACE/vcpkg_cache" >> $GITHUB_ENV
mkdir -p ${{ github.workspace }}/vcpkg_cache
- name: Restore vcpkg binary dir from cache
id: cache-vcpkg-binary
uses: actions/cache/restore@v4
with:
path: ${{ github.workspace }}/vcpkg_cache
key: vcpkg-cache-centos7-${{ hashFiles('src/vcpkg.json', '.git/modules/vcpkg/HEAD') }}
# Allows to restore a cache when deps have only partially changed (like adding a dependency)
restore-keys: vcpkg-cache-centos7-
- name: Install ccache
run: sudo apt-get install -y ccache
- name: Restore ccache cache
id: cache
uses: actions/cache/restore@v4
with:
path: ${{ github.workspace }}/.ccache
key: centos7-on-${{ runner.os }}-${{ github.ref_name }}
restore-keys: |
centos7-on-${{ runner.os }}-
- name: Configure ccache
run: |
ccache --set-config=cache_dir=${{ github.workspace }}/.ccache
ccache --set-config=max_size=500M
ccache --set-config=compression=true
ccache -p
ccache -z
- name: Build the image and Antares
run: |
ls -latr .ccache/ && \
docker build \
-t antares:centos7 \
--build-arg="BRANCH=${{ env.REF }}" \
--build-arg="NPROC=$(nproc)" \
--build-arg="VCPKG_CACHE_DIR=./vcpkg_cache" \
--build-arg CCACHE_DIR=./.ccache \
--build-arg CCACHE_KEY=centos7-on-${{ runner.os }} \
-f ${{ env.DOCKERFILE }} .
- name: create a container without starting it && retrieve the .tgz
run: |
container_id=$(docker create antares:centos7)
docker cp $container_id:/workspace/Antares_Simulator/_build/archive archive
docker cp $container_id:/workspace/vcpkg_cache ${{ env.VCPKG_CACHE_DIR }}
docker cp $container_id:/workspace/.ccache/. .ccache
ls -la .ccache
docker rm $container_id
- name: Push artifacts
uses: actions/upload-artifact@v5
with:
name: centos-targz
path: archive/*.tar.gz
- name: Publish assets
if: ${{ env.IS_RELEASE == 'true' }}
env:
GITHUB_TOKEN: ${{ github.token }}
tag: ${{ github.event.inputs.release_tag }}
run: |
gh release upload "$tag" archive/*.tar.gz archive/*.rpm
- name: Update continuous-delivery release
if: github.ref == 'refs/heads/develop'
env:
GH_TOKEN: ${{ github.token }}
run: |
mv archive/antares-*-CentOS-7.9.2009.tar.gz archive/antares-cd-CentOS-7.9.2009.tar.gz
mv archive/antares-*-CentOS-7.9.2009.rpm archive/antares-cd-CentOS-7.9.2009.rpm
gh release upload --clobber continuous-delivery archive/*.tar.gz archive/*.rpm
- name: Cache vcpkg binary dir
if: always()
id: save-cache-vcpkg-binary
uses: actions/cache/save@v4
with:
path: ${{ github.workspace }}/vcpkg_cache
key: vcpkg-cache-centos7-${{ hashFiles('src/vcpkg.json', '.git/modules/vcpkg/HEAD') }}
- name: Display ccache statistics
run: ccache -s
# Delete the old cache on hit to emulate a cache update. See
# https://github.com/actions/cache/issues/342.
- name: Delete old cache
continue-on-error: true
env:
GH_TOKEN: ${{ github.token }}
if: steps.cache.outputs.cache-hit
# Using `--repo` makes it so that this step doesn't require checking out the
# repo first.
run: gh cache delete --repo ${{ github.repository }} ${{ steps.cache.outputs.cache-primary-key }}
- name: Save ccache cache
uses: actions/cache/save@v4
with:
path: ${{ github.workspace }}/.ccache
key: ${{ steps.cache.outputs.cache-primary-key }}