📖 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
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
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
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
Hint
Use echo and shell parameter expansion ${ref#refs/tags/} to print the tag name.
Practice
(1/5)
1. What is the main purpose of using tags in Git for deployment triggers?
easy
A. To merge multiple branches into one
B. To delete old branches automatically
C. To create new branches for development
D. To mark specific versions of code for deployment
Solution
Step 1: Understand what tags represent in Git
Tags are labels that mark specific points in Git history, usually used for releases or versions.
Step 2: Connect tags to deployment triggers
Deployment systems listen for tag pushes to know when to deploy a specific version of the code.
Final Answer:
To mark specific versions of code for deployment -> Option D
Quick Check:
Tags mark versions = C [OK]
Hint: Tags label versions for deployment triggers [OK]
Common Mistakes:
Confusing tags with branches
Thinking tags delete branches
Assuming tags create new branches
2. Which Git command correctly creates a tag named v1.0 and pushes it to the remote repository?
easy
A. git create tag v1.0 && git push tags
B. git push tag v1.0
C. git tag v1.0 && git push origin v1.0
D. git tag create v1.0 && git push origin tags
Solution
Step 1: Identify the correct command to create a tag
The command git tag v1.0 creates a tag named v1.0 locally.
Step 2: Identify how to push the tag to remote
git push origin v1.0 pushes the specific tag to the remote repository.
Final Answer:
git tag v1.0 && git push origin v1.0 -> Option C
Quick Check:
Create tag + push tag = A [OK]
Hint: Use 'git tag' then 'git push origin tagname' [OK]
Common Mistakes:
Using 'git push tag' which is invalid
Trying 'git create tag' which is not a Git command
Pushing 'tags' instead of specific tag name
3. Given this GitHub Actions snippet, what triggers the deployment workflow?
on:
push:
tags:
- 'release-*'
medium
A. Any push of tags starting with 'release-'
B. Any push to branches named 'release-*'
C. Any push to any branch
D. Any push of tags named exactly 'release-*'
Solution
Step 1: Understand the trigger syntax
The on.push.tags with pattern 'release-*' listens for tag pushes starting with 'release-'.
Step 2: Differentiate tags from branches
This does not trigger on branches, only on tags matching the pattern.
Final Answer:
Any push of tags starting with 'release-' -> Option A
Quick Check:
Push tags matching pattern = A [OK]
Hint: Tags trigger with 'push.tags' and pattern [OK]
Common Mistakes:
Confusing tags with branches in triggers
Thinking exact tag name match is required
Assuming all pushes trigger workflow
4. You set this in your GitHub Actions workflow:
on:
push:
tags:
- 'v*'
But pushing tag release1 does NOT trigger the workflow. Why?
medium
A. The tag name does not match the pattern 'v*'
B. Tags cannot trigger workflows in GitHub Actions
C. The workflow file is missing from the repository
D. The push event only triggers on branches, not tags
Solution
Step 1: Analyze the tag pattern
The pattern 'v*' matches tags starting exactly with 'v', like 'v1.0', but 'release1' starts with 'release', so it does not match.
Step 2: Confirm tag triggers work
GitHub Actions supports tag push triggers, so the problem is pattern mismatch.
Final Answer:
The tag name does not match the pattern 'v*' -> Option A
Quick Check:
Pattern mismatch blocks trigger = B [OK]
Hint: Check tag name matches trigger pattern exactly [OK]
Common Mistakes:
Assuming any tag triggers workflow
Thinking tags can't trigger workflows
Ignoring pattern matching rules
5. You want to deploy only when a tag matching deploy-* is pushed. Which GitHub Actions trigger configuration is correct?
hard
A. on:
push:
tags:
- 'release-*'
B. on:
push:
tags:
- 'deploy-*'
C. on:
tag_push:
pattern: 'deploy-*'
D. on:
push:
branches:
- 'deploy-*'
Solution
Step 1: Identify correct event for tag push
The push event with tags key listens for tag pushes matching patterns.
Step 2: Match the pattern to desired tags
The pattern 'deploy-*' matches tags starting with 'deploy-', exactly what is needed.
Step 3: Eliminate incorrect options
on:
push:
branches:
- 'deploy-*' listens to branches, not tags. on:
tag_push:
pattern: 'deploy-*' uses invalid syntax. on:
push:
tags:
- 'release-*' matches wrong tag pattern.
Final Answer:
on:
push:
tags:
- 'deploy-*' -> Option B
Quick Check:
Push tags with correct pattern = D [OK]
Hint: Use 'push.tags' with correct pattern for deployment triggers [OK]
Common Mistakes:
Using branches instead of tags for deployment triggers
Using invalid event names like 'tag_push'
Mismatching tag patterns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.