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

feat: select target to build depending on the requirement of the modified files #1471

Merged
merged 1 commit into from
Jan 16, 2024
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
42 changes: 32 additions & 10 deletions .github/workflows/plugins-update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,50 @@ jobs:
distribution: 'temurin'
java-version: 17

- run: echo "::set-output name=stdout::$(./bin/update-plugins.sh)\n"
id: update-plugins
name: Update plugins

- uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a
id: generate-token
with:
app_id: ${{ secrets.JENKINS_ADMIN_APP_ID }}
private_key: ${{ secrets.JENKINS_ADMIN_APP_PRIVKEY }}

- name: Create Pull Request
id: cpr
- run: echo "::set-output name=stdout::$(./bin/update-plugins.sh plugins-infra.ci.jenkins.io.txt)\n"
id: update-infra-ci-plugins
name: Update infra-ci plugins

- name: Create infra.ci.jenkins.io Pull Request
id: cpr-infra-ci
uses: peter-evans/create-pull-request@153407881ec5c347639a548ade7d8ad1d6740e38 #v5
with:
commit-message: 'chore(deps): update infra.ci.jenkins.io plugins'
signoff: false
title: 'chore(deps): update infra.ci.jenkins.io plugins'
author: jenkins-infra-updatecli[bot] <178728+jenkins-infra-updatecli[bot]@users.noreply.github.com>
committer: jenkins-infra-updatecli[bot] <178728+jenkins-infra-updatecli[bot]@users.noreply.github.com>
branch: update-infra-ci-plugins
token: ${{ steps.generate-token.outputs.token }}
labels: dependencies,infra-ci-plugins
body: |
Changes:
```diff
${{ steps.update-plugins.outputs.stdout }}
```

- run: echo "::set-output name=stdout::$(./bin/update-plugins.sh plugins-weekly.ci.jenkins.io.txt)\n"
id: update-weekly-ci-plugins
name: Update weekly-ci plugins

- name: Create weekly.ci.jenkins.io Pull Request
id: cpr-weekly-ci
uses: peter-evans/create-pull-request@153407881ec5c347639a548ade7d8ad1d6740e38 #v5
with:
commit-message: 'chore(deps): update plugins'
commit-message: 'chore(deps): update weekly.ci.jenkins.io plugins'
signoff: false
title: 'chore(deps): update plugins'
title: 'chore(deps): update weekly.ci.jenkins.io plugins'
author: jenkins-infra-updatecli[bot] <178728+jenkins-infra-updatecli[bot]@users.noreply.github.com>
committer: jenkins-infra-updatecli[bot] <178728+jenkins-infra-updatecli[bot]@users.noreply.github.com>
branch: update-plugins
branch: update-weekly-ci-plugins
token: ${{ steps.generate-token.outputs.token }}
labels: dependencies,plugins
labels: dependencies,weekly-ci-plugins
body: |
Changes:
```diff
Expand Down
40 changes: 31 additions & 9 deletions Jenkinsfile_k8s
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
properties([buildDiscarder(logRotator(daysToKeepStr: '15'))])
pipeline {
agent { label 'jnlp-linux-arm64' }
options {
buildDiscarder(logRotator(daysToKeepStr: '15'))
}
stages {
stage('Checkout') {
steps {
checkout scm
script {
// Identify the target to build depending on the requirement of the modified files (empty if none)
def target = sh(script: 'bin/select-target-from-modified-files.sh', returnStdout: true).trim()

buildDockerAndPublishImage('jenkins-weekly', [
useContainer: false,
automaticSemanticVersioning: true,
gitCredentials: 'github-app-infra',
targetplatforms: 'linux/amd64,linux/arm64',
nextVersionCommand: 'echo "$(jx-release-version -next-version=semantic:strip-prerelease)-$(grep "FROM jenkins" Dockerfile | cut -d: -f2 | cut -d- -f1)"',
dockerBakeFile: 'docker-bake.hcl',
])
if (target) {
echo "= Building ${target} target"
buildDockerAndPublishImage('jenkins-weekly', [
useContainer: false,
automaticSemanticVersioning: true,
gitCredentials: 'github-app-infra',
targetplatforms: 'linux/amd64,linux/arm64',
nextVersionCommand: 'echo "$(jx-release-version -next-version=semantic:strip-prerelease)-$(grep "FROM jenkins" Dockerfile | cut -d: -f2 | cut -d- -f1)"',
dockerBakeFile: 'docker-bake.hcl',
dockerBakeTarget: target,
])
} else {
echo 'INFO: none of the modified file(s) required a build.'
}
}
}
}
}
}
42 changes: 42 additions & 0 deletions bin/select-target-from-modified-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

set -x

# List of files requiring a build
files_requiring_a_build=("cst.yaml" "docker-bake.hcl" "Dockerfile" "Jenkinsfile_k8s" "plugins-infra.ci.jenkins.io.txt" "plugins-weekly.ci.jenkins.io.txt" "logos/beekeeper.png" "logos/buttler_jenkins_is_the_way.png" "logos/buttler_stay_safe.png")

# List of modified files which reference depends if in a pull request or not
reference="origin/main..HEAD"
if [[ "${CHANGE_ID}" == "" ]]; then
reference="HEAD^..HEAD"
fi
modified_files=($(git --no-pager diff "${reference}" --name-only))

# Find the intersection between them
intersection=""
for file in "${modified_files[@]}"; do
if [[ "${files_requiring_a_build[@]}" =~ "${file}" ]]; then
intersection="${intersection}${file}"
fi
done

# Target everything by default
target="default"

# No target if there is no file requiring a build
if [[ "${intersection}" == "" ]]; then
echo ""
exit 0
fi

# Target infra-ci if only its plugins list has been modified
if [[ "${intersection}" == "plugins-infra.ci.jenkins.io.txt" ]]; then
target="infra-ci"
fi

# Target weekly-ci if only its plugins list has been modified
if [[ "${intersection}" == "plugins-weekly.ci.jenkins.io.txt" ]]; then
target="weekly-ci"
fi

echo "${target}"
9 changes: 7 additions & 2 deletions bin/update-plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

set -ex

list="plugins-*.txt"
if [[ "$#" -eq 1 ]]; then
list=$1
fi

cd "$(dirname "$0")" || exit 1

echo "Updating plugins"
Expand All @@ -19,8 +24,8 @@ wget --no-verbose "https://get.jenkins.io/war/${CURRENT_JENKINS_VERSION}/jenkins

cd ../ || exit 1

# Iterate through each txt file starting with "plugins-"
for pluginfile in plugins-*.txt; do
# Iterate through each txt file starting with "plugins-", or the file specified as input
for pluginfile in ${list}; do
if [ -e "${pluginfile}" ]; then
echo "Updating plugins file: ${pluginfile}"

Expand Down