Maven / GitLab CI Basics Interview Questions
What is the 'script', 'before_script', and 'after_script' keywords and how do they differ?
These three keywords define the shell commands that run during a job. They execute in sequence but have different scopes and failure behaviour.
| Keyword | When it runs | Fails the job if it fails? | Scope |
|---|---|---|---|
| before_script | Before the main script | Yes | Can be set globally or per-job |
| script | The job's main work | Yes | Per-job only (required) |
| after_script | After script (and before_script) | No (always runs) | Can be set globally or per-job |
# Global before_script runs before every job's script default: before_script: - echo "Global setup" build-job: stage: build before_script: - echo "Job-specific setup (overrides global)" # overrides the default script: - npm ci - npm run build after_script: - echo "Cleanup - always runs even if script fails"
Key behaviours:
before_scriptdefined in a job overrides (not appends to) the globalbefore_scriptafter_scriptruns in a separate shell context fromscript- environment variable changes made inscriptare not visible inafter_scriptafter_scriptruns even when the job fails or times out, making it ideal for cleanup tasks
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
