|
| 1 | +namespace :release do |
| 2 | + desc "Tasks to run on a new branch when a new branch is created" |
| 3 | + task :new_branch do |
| 4 | + require 'pathname' |
| 5 | + |
| 6 | + branch = ENV["RELEASE_BRANCH"] |
| 7 | + if branch.nil? || branch.empty? |
| 8 | + STDERR.puts "ERROR: You must set the env var RELEASE_BRANCH to the proper value." |
| 9 | + exit 1 |
| 10 | + end |
| 11 | + |
| 12 | + current_branch = `git rev-parse --abbrev-ref HEAD`.chomp |
| 13 | + if current_branch == "master" |
| 14 | + STDERR.puts "ERROR: You cannot do new branch tasks from the master branch." |
| 15 | + exit 1 |
| 16 | + end |
| 17 | + |
| 18 | + root = Pathname.new(__dir__).join("../..") |
| 19 | + |
| 20 | + branch_number = branch[0].ord - 96 |
| 21 | + rpm_repo_name = "#{branch_number}-#{branch}" |
| 22 | + |
| 23 | + # Modify README |
| 24 | + readme = root.join("README.md") |
| 25 | + content = readme.read |
| 26 | + readme.write(content.sub(/^(default: )\w+/, "\\1latest-#{branch}")) |
| 27 | + |
| 28 | + # Modify operator README |
| 29 | + operator_readme = root.join("manageiq-operator", "README.md") |
| 30 | + content = operator_readme.read |
| 31 | + operator_readme.write(content.gsub(%r{(/manageiq-operator:)\w+}, "\\1latest-#{branch}")) |
| 32 | + |
| 33 | + # Modify CR |
| 34 | + cr = root.join("manageiq-operator", "pkg", "helpers", "miq-components", "cr.go") |
| 35 | + content = cr.read |
| 36 | + cr.write(content.sub(/(cr\.Spec\.OrchestratorImageTag.+?\n\s+return ")\w+(")/, "\\1latest-#{branch}\\2")) |
| 37 | + |
| 38 | + # Modify types |
| 39 | + types = root.join("manageiq-operator", "pkg", "apis", "manageiq", "v1alpha1", "manageiq_types.go") |
| 40 | + content = types.read |
| 41 | + types.write(content.sub(/(tag used for the orchestrator and worker deployments \(default: )\w+(\))/, "\\1latest-#{branch}\\2")) |
| 42 | + |
| 43 | + # Modify deploy operator.yaml |
| 44 | + deploy_operator = root.join("manageiq-operator", "deploy", "operator.yaml") |
| 45 | + content = deploy_operator.read |
| 46 | + deploy_operator.write(content.sub(%r{(docker.io/manageiq/manageiq-operator:)\w+}, "\\1latest-#{branch}")) |
| 47 | + |
| 48 | + # Modify deploy CRD |
| 49 | + deploy_crd = root.join("manageiq-operator", "deploy", "crds", "manageiq.org_manageiqs_crd.yaml") |
| 50 | + content = deploy_crd.read |
| 51 | + deploy_crd.write(content.sub(/(tag used for the orchestrator and worker deployments\n\s+\(default: )\w+(\))/, "\\1latest-#{branch}\\2")) |
| 52 | + |
| 53 | + # Modify deploy CSV |
| 54 | + deploy_csv = root.join("manageiq-operator", "deploy", "olm-catalog", "manageiq-operator", "manifests", "manageiq-operator.clusterserviceversion.yaml") |
| 55 | + content = deploy_csv.read |
| 56 | + deploy_csv.write(content.sub(%r{(docker.io/manageiq/manageiq-operator:)\w+}, "\\1latest-#{branch}")) |
| 57 | + |
| 58 | + # Modify catalog CRD |
| 59 | + catalog_crd = root.join("manageiq-operator", "deploy", "olm-catalog", "manageiq-operator", "manifests", "manageiq.org_manageiqs_crd.yaml") |
| 60 | + content = catalog_crd.read |
| 61 | + catalog_crd.write(content.sub(/(tag used for the orchestrator and worker deployments\n\s+\(default: )\w+(\))/, "\\1latest-#{branch}\\2")) |
| 62 | + |
| 63 | + # Modify bin/build |
| 64 | + build_script = root.join("bin", "build") |
| 65 | + content = build_script.read |
| 66 | + content.sub!(/^(TAG=)\w+/, "\\1latest-#{branch}") |
| 67 | + content.sub!(/(BUILD_REF:-)\w+(\})/, "\\1#{branch}]\\2") |
| 68 | + build_script.write(content) |
| 69 | + |
| 70 | + # Modify bin/remove_images |
| 71 | + remove_script = root.join("bin", "remove_images") |
| 72 | + content = remove_script.read |
| 73 | + remove_script.write(content.sub(/^(TAG=)\w+/, "\\1latest-#{branch}")) |
| 74 | + |
| 75 | + # Modify base Dockerfile |
| 76 | + base_dockerfile = root.join("images", "manageiq-base", "Dockerfile") |
| 77 | + content = base_dockerfile.read |
| 78 | + content.sub!(/^(ARG BUILD_REF=)\w+/, "\\1#{branch}") |
| 79 | + content.sub!(%r{(/rpm.manageiq.org/release/)\d+-\w+}, "\\1#{rpm_repo_name}") |
| 80 | + content.sub!(%r{(/el8/noarch/manageiq-release-)\d+\.\d+-\d+}, "\\1#{branch_number}.0-1") |
| 81 | + content.sub!(/(manageiq-)\d+-\w+(-nightly)/, "\\1#{rpm_repo_name}\\2") |
| 82 | + base_dockerfile.write(content) |
| 83 | + |
| 84 | + # Modify Dockerfiles |
| 85 | + dockerfiles = %w[manageiq-base-worker manageiq-webserver-worker manageiq-ui-worker manageiq-orchestrator].map do |worker| |
| 86 | + root.join("images", worker, "Dockerfile").tap do |dockerfile| |
| 87 | + content = dockerfile.read |
| 88 | + dockerfile.write(content.sub(/^(ARG FROM_TAG=)\w+/, "\\1latest-#{branch}")) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + # Commit |
| 93 | + files_to_update = [readme, operator_readme, cr, types, deploy_operator, deploy_crd, deploy_csv, catalog_crd, build_script, remove_script, base_dockerfile, *dockerfiles] |
| 94 | + exit $?.exitstatus unless system("git add #{files_to_update.join(" ")}") |
| 95 | + exit $?.exitstatus unless system("git commit -m 'Changes for new branch #{branch}'") |
| 96 | + |
| 97 | + puts |
| 98 | + puts "The commit on #{current_branch} has been created." |
| 99 | + puts "Run the following to push to the upstream remote:" |
| 100 | + puts |
| 101 | + puts "\tgit push upstream #{current_branch}" |
| 102 | + puts |
| 103 | + end |
| 104 | + |
| 105 | + desc "Tasks to run on the master branch when a new branch is created" |
| 106 | + task :new_branch_master do |
| 107 | + require 'pathname' |
| 108 | + |
| 109 | + branch = ENV["RELEASE_BRANCH"] |
| 110 | + if branch.nil? || branch.empty? |
| 111 | + STDERR.puts "ERROR: You must set the env var RELEASE_BRANCH to the proper value." |
| 112 | + exit 1 |
| 113 | + end |
| 114 | + |
| 115 | + next_branch = ENV["RELEASE_BRANCH_NEXT"] |
| 116 | + if next_branch.nil? || next_branch.empty? |
| 117 | + STDERR.puts "ERROR: You must set the env var RELEASE_BRANCH_NEXT to the proper value." |
| 118 | + exit 1 |
| 119 | + end |
| 120 | + |
| 121 | + current_branch = `git rev-parse --abbrev-ref HEAD`.chomp |
| 122 | + if current_branch != "master" |
| 123 | + STDERR.puts "ERROR: You cannot do master branch tasks from a non-master branch (#{current_branch})." |
| 124 | + exit 1 |
| 125 | + end |
| 126 | + |
| 127 | + root = Pathname.new(__dir__).join("../..") |
| 128 | + |
| 129 | + next_branch_number = next_branch[0].ord - 96 |
| 130 | + rpm_repo_name = "#{next_branch_number}-#{next_branch}" |
| 131 | + |
| 132 | + # Modify base Dockerfile |
| 133 | + base_dockerfile = root.join("images", "manageiq-base", "Dockerfile") |
| 134 | + content = base_dockerfile.read |
| 135 | + content.sub!(%r{(/rpm.manageiq.org/release/)\d+-\w+}, "\\1#{rpm_repo_name}") |
| 136 | + content.sub!(%r{(/el8/noarch/manageiq-release-)\d+\.\d+-\d+}, "\\1#{next_branch_number}.0-1") |
| 137 | + content.sub!(/(manageiq-)\d+-\w+(-nightly)/, "\\1#{rpm_repo_name}\\2") |
| 138 | + base_dockerfile.write(content) |
| 139 | + |
| 140 | + # Commit |
| 141 | + files_to_update = [base_dockerfile] |
| 142 | + exit $?.exitstatus unless system("git add #{files_to_update.join(" ")}") |
| 143 | + exit $?.exitstatus unless system("git commit -m 'Changes after new branch #{branch}'") |
| 144 | + |
| 145 | + puts |
| 146 | + puts "The commit on #{current_branch} has been created." |
| 147 | + puts "Run the following to push to the upstream remote:" |
| 148 | + puts |
| 149 | + puts "\tgit push upstream #{current_branch}" |
| 150 | + puts |
| 151 | + end |
| 152 | +end |
0 commit comments