0
0
Gitdevops~15 mins

Deployment triggers from tags in Git - Mini Project: Build & Apply

Choose your learning style9 modes available
Deployment triggers from tags
📖 Scenario: You work in a team that uses Git for version control. Your team wants to automatically deploy the application only when a new tag is created. Tags are like labels on specific points in the project history, often used to mark releases.To make deployment automatic, you will write a simple Git hook script that triggers deployment only when a tag is pushed.
🎯 Goal: Create a Git hook script that triggers deployment only when a new tag is pushed to the repository.
📋 What You'll Learn
Create a Git hook script file named post-receive in the .git/hooks/ directory
Add a variable ref to capture the reference name from standard input
Write a condition to check if the ref starts with refs/tags/
Print Deploying tag: <tag_name> when a tag is pushed
💡 Why This Matters
🌍 Real World
Git hooks are used in real projects to automate tasks like deployment when code is pushed. Using tags to trigger deployment helps release stable versions automatically.
💼 Career
Understanding Git hooks and deployment triggers is important for DevOps roles to automate software delivery and improve team workflows.
Progress0 / 4 steps
1
Create the Git hook script file
Create a file named post-receive inside the .git/hooks/ directory with the first line as the shebang #!/bin/sh.
Git
Need a hint?

The first line of a shell script is usually #!/bin/sh to specify the shell interpreter.

2
Capture the reference name from standard input
Add a line to read the old revision, new revision, and reference name into variables oldrev, newrev, and ref using read oldrev newrev ref.
Git
Need a hint?

Use the read command to get input from standard input into variables.

3
Check if the reference is a tag
Write an if statement that checks if the variable ref starts with refs/tags/ using case "$ref" in refs/tags/*) syntax.
Git
Need a hint?

Use a case statement to match the pattern refs/tags/* for tags.

4
Print deployment message for the tag
Inside the case block for refs/tags/*, add a echo command that prints Deploying tag: <tag_name> where <tag_name> is the tag name extracted by removing the refs/tags/ prefix from ref using shell parameter expansion ${ref#refs/tags/}.
Git
Need a hint?

Use echo and shell parameter expansion ${ref#refs/tags/} to print the tag name.