|
1 | | -#!/bin/bash |
2 | | - |
3 | | -# This script creates a tag for the repository and pushes it to the remote |
4 | | -# repository. The remote repository is configured to trigger a GitHub Actions |
5 | | -# workflow when a new tag is pushed, which will create a release on GitHub |
6 | | -# using the tag. |
7 | | - |
8 | | -# Usage: ./release-lab-instructions <version-number> ["Optional tag commit message"] |
9 | | - |
10 | | -# Check for the correct number of input parameters |
11 | | -if [ "$#" -lt 1 ]; then |
12 | | - echo "Error: Incorrect number of arguments." |
13 | | - echo "Usage: $0 <version-number> [\"tag commit message\"]" |
14 | | - echo "Example: $0 v1.0.1 \"Initial release\"" |
15 | | - echo "If no commit message is specified, the default editor will be opened." |
16 | | - exit 1 |
17 | | -fi |
| 1 | +#!/usr/bin/env bash |
| 2 | +set -Eeuo pipefail |
| 3 | + |
| 4 | +# release-lab-instructions.sh |
| 5 | +# Skapar och pushar en annoterad tagg: lab_<version> |
| 6 | +# GitHub Actions-workflow som lyssnar på "lab_*" bygger PDF. |
| 7 | + |
| 8 | +REMOTE="origin" |
| 9 | + |
| 10 | +usage() { |
| 11 | + cat <<'USAGE' |
| 12 | +Usage: |
| 13 | + release-lab-instructions.sh -v <version> [-m "message"] [-r <remote>] [-y] |
| 14 | +
|
| 15 | +Options: |
| 16 | + -v Versionssträng (t.ex. 2025-09-01 eller v1.2.3). REQ |
| 17 | + -m Taggmeddelande (annoterad tag). Om utelämnas öppnas editor. |
| 18 | + -r Git remote (default: origin) |
| 19 | + -y Svara "ja" på alla frågor (överskriv tagg utan att fråga) |
| 20 | + -h Visa denna hjälp |
| 21 | +
|
| 22 | +Exempel: |
| 23 | + ./release-lab-instructions.sh -v v1.0.1 |
| 24 | + ./release-lab-instructions.sh -v 2025-09-01 -m "HT25 lab release" |
| 25 | + ./release-lab-instructions.sh -v v2 -r origin -y |
| 26 | +USAGE |
| 27 | +} |
| 28 | + |
| 29 | +confirm() { |
| 30 | + local prompt="${1:-Är du säker? (y/N) }" |
| 31 | + read -r -p "$prompt" ans |
| 32 | + [[ "$ans" == "y" || "$ans" == "Y" ]] |
| 33 | +} |
18 | 34 |
|
| 35 | +delete_tag_if_exists() { |
| 36 | + local tag="$1" |
| 37 | + if git rev-parse "$tag" >/dev/null 2>&1; then |
| 38 | + echo "Taggen $tag finns redan lokalt." |
| 39 | + if $ASSUME_YES || confirm "Vill du ersätta taggen $tag? (y/N) "; then |
| 40 | + git tag -d "$tag" || true |
| 41 | + if git ls-remote --tags "$REMOTE" | grep -q "refs/tags/$tag$"; then |
| 42 | + git push "$REMOTE" --delete "$tag" || true |
| 43 | + fi |
| 44 | + else |
| 45 | + echo "Avbryter." |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + else |
| 49 | + # Om den inte finns lokalt, kolla endast remote |
| 50 | + if git ls-remote --tags "$REMOTE" | grep -q "refs/tags/$tag$"; then |
| 51 | + echo "Taggen $tag finns på remote." |
| 52 | + if $ASSUME_YES || confirm "Vill du ersätta taggen $tag på remote? (y/N) "; then |
| 53 | + git push "$REMOTE" --delete "$tag" || true |
| 54 | + else |
| 55 | + echo "Avbryter." |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | + fi |
| 59 | + fi |
| 60 | +} |
19 | 61 |
|
20 | | -# Define the tag and message |
21 | | -TAG="lab_$1" |
22 | | -MESSAGE="Lab instructions $1" |
23 | | - |
24 | | -# Check if the tag already exists |
25 | | -if git rev-parse "$TAG" >/dev/null 2>&1; then |
26 | | - echo "Error: Tag $TAG already exists." |
27 | | - |
28 | | - # Ask user if they want to replace the existing tag |
29 | | - read -p "Do you want to replace the existing tag? (y/n) " answer |
30 | | - case $answer in |
31 | | - [Yy]* ) |
32 | | - # Delete the tag locally |
33 | | - git tag -d "$TAG" |
34 | | - # Delete the tag from the remote |
35 | | - git push origin --delete "$TAG" |
36 | | - ;; |
37 | | - * ) |
38 | | - echo "Operation aborted." |
39 | | - exit 1 |
40 | | - ;; |
41 | | - esac |
| 62 | +VERSION="" |
| 63 | +MESSAGE="" |
| 64 | +ASSUME_YES=false |
| 65 | + |
| 66 | +while getopts ":v:m:r:yh" opt; do |
| 67 | + case "$opt" in |
| 68 | + v) VERSION="$OPTARG" ;; |
| 69 | + m) MESSAGE="$OPTARG" ;; |
| 70 | + r) REMOTE="$OPTARG" ;; |
| 71 | + y) ASSUME_YES=true ;; |
| 72 | + h) usage; exit 0 ;; |
| 73 | + \?) echo "Okänd flagga: -$OPTARG"; usage; exit 1 ;; |
| 74 | + :) echo "Flagga -$OPTARG kräver ett värde."; usage; exit 1 ;; |
| 75 | + esac |
| 76 | +done |
| 77 | + |
| 78 | +if [[ -z "$VERSION" ]]; then |
| 79 | + echo "Fel: -v <version> krävs." |
| 80 | + usage |
| 81 | + exit 1 |
42 | 82 | fi |
43 | 83 |
|
44 | | -# Create the annotated tag |
45 | | -if [ -z "$2" ]; then |
46 | | - # If no message is provided, open the editor to allow the user to enter a message |
47 | | - git tag -a "$TAG" |
48 | | -else |
49 | | - # If a message is provided, use it directly |
50 | | - git tag -a "$TAG" -m "$2" |
| 84 | +TAG="lab_${VERSION}" |
| 85 | + |
| 86 | +# Hämta taggar |
| 87 | +git fetch --tags "$REMOTE" >/dev/null 2>&1 || true |
| 88 | + |
| 89 | +echo "Sammanfattning:" |
| 90 | +echo " Version: $VERSION" |
| 91 | +echo " Tagg: $TAG" |
| 92 | +echo " Remote: $REMOTE" |
| 93 | +$ASSUME_YES || confirm "Skapa och pusha taggen nu? (y/N) " || { echo "Avbrutet."; exit 1; } |
| 94 | + |
| 95 | +# Ta bort befintlig tagg (lokalt/remote) vid behov |
| 96 | +delete_tag_if_exists "$TAG" |
| 97 | + |
| 98 | +# Om inget -m: öppna editor för att skriva ett meddelande |
| 99 | +if [[ -z "$MESSAGE" ]]; then |
| 100 | + tmpfile="$(mktemp)" |
| 101 | + { |
| 102 | + echo "Lab instructions $VERSION" |
| 103 | + echo |
| 104 | + echo "(Skriv ditt meddelande ovan. Ta inte bort sista raden.)" |
| 105 | + echo "---" |
| 106 | + } > "$tmpfile" |
| 107 | + "${GIT_EDITOR:-${VISUAL:-vi}}" "$tmpfile" |
| 108 | + MESSAGE="$(sed '/^---$/,$d' "$tmpfile" | sed -e '${/^$/d}')" |
| 109 | + rm -f "$tmpfile" |
| 110 | + [[ -z "$MESSAGE" ]] && MESSAGE="Lab instructions $VERSION" |
51 | 111 | fi |
52 | 112 |
|
53 | | -# Push the tag to the remote repository |
54 | | -git push origin "$TAG" |
| 113 | +# Skapa annoterad tagg |
| 114 | +git tag -a "$TAG" -m "$MESSAGE" |
| 115 | + |
| 116 | +# Pusha |
| 117 | +git push "$REMOTE" "$TAG" |
55 | 118 |
|
56 | | -echo "Tag $TAG created and pushed successfully." |
| 119 | +echo "Klart! Taggen $TAG skapades och pushades till $REMOTE." |
0 commit comments